嗨,大家对不起,我是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.");
}
}
具体是什么问题?我的意思是,当你说我的工作无法完成时,你的意思是什么_ – kaonashi
为什么你运行6次计数?使用相同的'.length'属性? –
尝试运行时出现错误。 @kaonashi –