當用戶選擇是時,它循環並重新開始。當用戶選擇N時,它應該結束程序,但我不確定我在這裏錯過了什麼。這是一個程序,用於告訴您在給程序提供斜率和Y軸截距時的x和y值。java - 當用戶鍵入「N」時結束循環
的Java文件
int slope;
int yintercept;
String newEquation;
boolean play = true;
System.out.print("Enter the slope: ");
slope = input.nextInt();
System.out.print("Enter y-intercept: ");
yintercept = input.nextInt();
System.out.printf("The equation of the line is: y = %dx + %d", slope, yintercept);
System.out.print("\nWould you like to create a new equation... Y or N? ");
newEquation = input.next();
while (play)
{
if (newEquation.equals("Y"))
{
System.out.print("Enter the slope: ");
slope = input.nextInt();
System.out.print("Enter y-intercept: ");
yintercept = input.nextInt();
System.out.printf("The equation of the line is: y = %dx + %d", slope, yintercept);
System.out.print("\nWould you like to create a new equation... Y or N? ");
newEquation = input.next();
}
if (newEquation.equals("N")){
play =false;
}
else{
System.out.print("Enter the slope: ");
slope = input.nextInt();
System.out.print("Enter y-intercept: ");
yintercept = input.nextInt();
System.out.printf("The equation of the line is: y = %dx + %d", slope, yintercept);
System.out.print("\nWould you like to create a new equation... Y or N? ");
newEquation = input.next();
}
}
}
}
它適用於我。你輸入「n」而不是「N」? –
通過使用Java的do-while構造,可以極大地簡化代碼。 https://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html – rajah9
哦哇我多麼愚蠢 –