for (int i = 0; i < 6; i++) {
lineOne[i] = Integer.parseInt(JOptionPane.showInputDialog(null,"");
if (lineOne[i] > 47 || lineOne[i] < 1)//Number also have to be inside these parameters
JOptionPane.showMessageDialog(null, "Please try again!!!");
lineOne[i] = Integer.parseInt(JOptionPane.showInputDialog(null, ""));
}
}
答案 0 :(得分:2)
使用Set而不是使用数组。
示例:
Set<Integer> numbers=new HashSet<>();
然后将数字添加到集合中。它不允许重复。
答案 1 :(得分:0)
问题在于你的if语句。如果输入的值超出1-47范围,则写入代码的方式,您再次询问该值。但是,如果再次给出“不正确”的值,则将其保留在数组中并返回到for循环的顶部。
试试这个。
for (int i = 0; i < 6; i++) {
lineOne[i] = Integer.parseInt(JOptionPane.showInputDialog(null,"");
while (lineOne[i] > 47 || lineOne[i] < 1) { //Number also have to be inside these parameters
JOptionPane.showMessageDialog(null, "Please try again!!!");
lineOne[i] = Integer.parseInt(JOptionPane.showInputDialog(null, ""));
}
}
答案 2 :(得分:0)
boolean b = true;
int[] lineOne = new int[6];
for (int i = 0; i < 6; i++) {
b = true;
int k = Integer.parseInt(JOptionPane.showInputDialog(null, ""));
for (int j : lineOne) {
if (k == j) {
System.out.println("error");
b = false;
break;
}
}
if (b && k < 47 && k > 1) {
lineOne[i] = k;
} else {
JOptionPane.showInputDialog(null, "Duplicate value or value out of bounds.");
i--;
}
}
这是我能想到的最佳方式。 boolean确定值是否已存在