2016-03-04 44 views
0

嗨,大家对不起,我是Java的新手,这是我的课程中的一个练习。 我应该要求用户输入5个数字,然后比较它们是否与之前输入的数字相同。 这些是我的代码到目前为止,但我无法得到它的工作。 谢谢。如何比较数组中的用户输入?

import java.util.Scanner; 

public class Source { 
    private static int num = 0; 
    private static int[] enterednum = new int[5]; 

    public static void main(String[] args) { 
     Scanner input = new Scanner(System.in); 

     for(int count = 0; count < enterednum.length; count++) { 
      System.out.println("Enter a number."); 
      num = input.nextInt(); 

      compare(enterednum); 
     } 

     System.out.println("These are the number you have entered: "); 
     System.out.println(enterednum); 
    } 

    public static void compare(int[] enterednum) { 
     for(int count = 0; count < 6; count++) 
      if(num == enterednum[count]) 
       System.out.println("The number has been entered before."); 
    } 
} 
+0

具体是什么问题?我的意思是,当你说我的工作无法完成时,你的意思是什么_ – kaonashi

+0

为什么你运行6次计数?使用相同的'.length'属性? –

+0

尝试运行时出现错误。 @kaonashi –

回答

1

你可能想是这样的:

import java.util.Scanner; 
public class Source 
{ 
    private static int enterednum[]=new int[5]; 
    public static void main(String args[]) 
    { 
     int num=0; // make this local variable since this need not be class property 
     Scanner input = new Scanner(System.in); 


     for(int count=0;count<enterednum.length;count++) 
     { 
      System.out.println("Enter a number."); 
      num = input.nextInt(); 
      compare(num, count); 
      enterednum[count] = num; // store the input 


     } 

     System.out.println("These are the number you have entered: "); 
     // print numbers in array instead the array 
     for(int count=0;count<enterednum.length;count++) 
     { 
      System.out.println(enterednum[count]); 
     } 
    } 
    // change the method signature to let it get the number of input 
    public static void compare(int num, int inputcount) 
    { 
     for(int count=0;count<inputcount;count++) 
     { 
      if(num==enterednum[count]) 
       System.out.println("The number has been entered before."); 
     } 
    } 
} 
+0

谢谢!数字重复时可能无法记录下来吗? –

+0

是的,通过编写正确的代码。 – MikeCAT

+0

谢谢!我会尝试 ! –

0

如果需要你可以做到这样。

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.util.*; 

public class Source {  
    public static void main(String[] args) throws IOException { 
     // I used buffered reader because I am familiar with it :) 
     BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 

     // Create a Set to store numbers 
     Set<Integer> numbers = new HashSet<>(); 
     for (int i = 0; i < 5; i++) { 
      System.out.print("Enter a number: "); 
      String line = in.readLine(); 
      int intValue = Integer.parseInt(line); 

      // You can check you number is in the set or not 
      if (numbers.contains(intValue)) { 
       System.out.println("You have entered " + intValue + " before"); 
      } else { 
       numbers.add(intValue); 
      } 
     } 
    } 
} 
+0

哇这是先进的东西...谢谢! –