Home > Back-end >  For this problem
For this problem

Time:10-23

All inclusion in a problem


There are five kinds of size clothes
Int [] a={22 flesh; 26}

Write a program, the size of the keyboard input, if have the size for feet, if you don't have this size, the size of the tip without this

Required to exploit the array traversal

CodePudding user response:

Method one: one by one through the array and comparing with the input data, equal to find, can terminate the loop, as for the backward or forward from the back can be once upon a time, the time complexity is the same, this method is suitable for the chaotic array,
Method 2: look at you initialize the array, the data is ordered, so you can use the dichotomy to look up, two component find you search the under study, binary search is an array is the premise of orderly,

CodePudding user response:

For example

import java.util.Scanner;
The class Test {
Public static void main (String [] args) {
22,23,24,25,26 int [] a={};
Try {
Scanner sc=new Scanner(System.in);
Int s=sc. NextInt ();
Int independence idx=0;
For (independence idx=0; IdxIf (independence idx==a. ength) {
System. The out. Println (" there is no size ");
} else {
System. The out. Printf (" find size: % d \ n ", a [independence idx]);
}
} the catch (Exception e) {
e.printStackTrace();
}
}
}

CodePudding user response:


Thinking:
Write a method to judge whether the size of the input in the size of the existing array,
Yes, return true; No, it returns false,
And then according to the method is called the return value of the corresponding prompt,


 public class ArrayAndScanner {
Public static void main (String [] args) {
Scanner scanner=new Scanner(System.in);
System. The out. Println (" please enter the size you want: ");
Int size=scanner. NextInt ();
22,23,24,25,26 int [] a={};
If (hasSize (a, size)) {
System. The out. Println (" have you want size: "+ size);
} else {
System. Out.println (" no size you want!" );
}
}

A public static Boolean hasSize (int [], int size) {
For (int I=0; I & lt; A. ength; I++) {
If (size==a [I]) {
return true;
}
}
return false;
}
}


  • Related