do-while循环会在增加值之前或之后检查值吗?我似乎无法使此do-while循环转义,并且无法确定这是否是我犯的错误。该循环的重点是从用户那里获取输入,当他们按下“ X”时,我希望循环结束。我使用的是错误的循环类型,还是不正确的语句?
int i = 0,
inputCount = 0;
char letter = 'a';
String[] coefficient = new String[MAX_LENGTH];
do{
System.out.print("What is " + letter +"? ");
coefficient[i] = keyboard.nextLine();
i++;
letter++;
inputCount++;
}while(coefficient[i] != "X");
最佳答案
您要在i
和coefficient[i] = keyboard.nextLine();
之间递增while(coefficient[i] != "X");
,因此在check系数[i]为null的同时,还请使用.equals比较字符串。
int i = 0,
inputCount = 0;
char letter = 'a';
String[] coefficient = new String[MAX_LENGTH];
do{
System.out.print("What is " + letter +"? ");
coefficient[i] = keyboard.nextLine();
i++;
letter++;
inputCount++;
}while(!coefficient[i-1].equals("X"));