2016-11-14 55 views
-8
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, "")); 
     } 
    } 
+7

看来你忘了在你的问题中包含一个问题。 – Biffen

+1

在这个问题中存在一个严重缺乏'问题'的问题 – ItamarG3

+1

因此,在存储之前检查一个值是否已经存在。 – azurefrog

回答

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, "")); 
     } 
    } 
+0

你的代码无法正常工作。您仍然可以输入重复值 – XtremeBaumer

+0

非常感谢。 – colOnTheBall

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--; 
     } 
    } 

这是我能想到的最好的办法。布尔值确定该值是否已经存在

+0

谢谢我使用了类似的东西,它完美的作品 – colOnTheBall

2

而不是使用数组使用Set。

实施例:

Set<Integer> numbers=new HashSet<>(); 

号码然后加入到集合中。它不会允许重复。