2013年10月29日星期二

java array of problems, please master guiding ( ideas + code )

The five numbers into an array , and then let the user enter an arbitrary number , and if this number in the array , the output of the number in the array subscript , if not in the array , the output -1 . ( User input methods do )
------ Solution ---------------------------------- ----------

int[] arr = {1,2,3,4,5};
int num = 6;
int count = 0;
for(int i=0;i<arr.length;i++){
if(arr[i]==num){
System.out.println(i);
}else{
count++;
}
}
if(count==5){
System.out.println("-1");
}


insult to intelligence.
------ Solution ---------------------------------------- ----
	public static void main(String[] args) throws Exception, IOException {
int[] arr = {1,2,3,4,5};
BufferedReader strin=new BufferedReader(new InputStreamReader(System.in));
int a = Integer.parseInt(strin.readLine());

print(arr,a);

}

public static void print(int[] arr,int a){
int count = 0;
for(int i=0;i<arr.length;i++){
if(arr[i]==a){
System.out.println(i);
}else{
count++;
}
}
if(count==arr.length){
System.out.println("-1");
}
}

------ For reference only ---------------------------- -----------
brother Hello I bother to ask you to come most of java array value problems
programming to achieve the user to enter five integers , save it to the array , and then find the maximum number of five .
------ For reference only ----- ----------------------------------
Hello , I do not know that if (count == arr . length) the brackets in the sense that the number of cycles equal to it ? Base a little bad , please exhibitions. .

没有评论:

发表评论