这些是我的问题:
我在 "public static boolean validNumCheck(String num){" - "illegal start of expression", "';' 预期”和“')'预期”。
我怎样才能为每个号码给用户 3 次尝试?我相信现在程序要求用户输入 3 个数字,并总共给他们 3 次尝试以使数字正确(我的解释很糟糕……阅读代码以更好地理解我的意思)。
这是我的代码:
import javax.swing.JOptionPane;
public class Assignment3 {
public static void main (String[] args){
final int MAX_TRIES = 3;
int[] attempts= new int[2];
String[] getNumber= new String [2];
//Ask the user for 3 integers.
while(attempts[0]<MAX_TRIES){
getNumber[0]= JOptionPane.showInputDialog(null,"Please enter an integer between 0-200.");
//Pass the value to validNumChek
validNumCheck (getNumber);
//If it is not a valid number give the user 3 more tries.
if (getNumber== false){
while(attempts[1]<MAX_TRIES){
getNumber[1]= JOptionPane.showInputDialog(null,"Please enter an integer between 0-200.");
attempts[1]++;}
}
attempts[0]++;
}
//Parse the string to an integer and check if it is a valid number.
public static boolean validNumCheck(String num){
int number;
try {
number = Integer.parseInt(num);
return number >= 0 && number <= 200;
}catch(NumberFormatException e){
//If it is not a valid number return false.
return false;
}
}
}