我在使用 Do/While 循环时遇到问题。无论用户键入什么字符或字符串,都会显示 if 块,即使不满足 while 条件也是如此。
更具体地说,此代码旨在提供条件(您要计算分数吗?)并根据用户响应,继续 if 块(用户 = y ---> 你的名字等),或者,当不满足 which 语句,跳过它(用户 = n ----> 程序结束)。
我使用的代码如下:
请记住,此代码尚未完成,并且显示的一些变量尚未使用。
import java.util.Scanner;
public class Test1 {
public static void main (String [] args) {
//Declares letters to allow repetition of program
final String START= "y";
//Declerations of variables
//Declares space for user's name
String Name;
//Declares space for user's year of birth
int Year;
//Declares space for user's double value, day and month of birth entered
//by user
double birthDM;
//Declares space for int value day of birth, calculated from double value
int day;
//Declares space for int value month of birth, calculated from double value
int month;
//Declares space for int of calculated score
int Score = 0;
//Declares space for the string value of test (check wording)
String confirmation;
//Declares space for total number of scores calculated
int Total;
//Initilisaton of Scanner object
Scanner sc;
//Declares the space for the string that will carry the initilising letter
String userStart;
//Declares space for the char that will activate program
char yesChar;
// Program Start
//User input to begin loop or not (test)
System.out.println(
"The following program will calculate an score from your name and birthday.");
System.out.println(" ");
do {
System.out.println("Would you like to calculate and score?");
sc = new Scanner(System.in);
userStart=sc.next();
if (uProgStart.equalsIgnoreCase(PROGSTART));
{
System.out.println("What is your name?");
userName=sc.next();
System.out.println("What is the year of your birth?");
birthYear = sc.nextInt();
}
} while(!uProgStart.equalsIgnoreCase(PROGSTART));
System.out.println("Program End");
}
}
任何和所有的帮助将不胜感激。