虽然语句不是在 if 语句内部执行?



我正试图让它打印出一系列指定的数字。因此,如果他们选择选项1并输入1和15,我希望它打印出1到15。一旦到达while语句,它就什么也不打印。

import java.util.Scanner;
public class Lab4 {
public static void main(String[] args) {
Scanner in = new Scanner (System.in);

System.out.print("Please choose your choice from the following menu");

System.out.print("n1) Print through all integer numbers between two given integers");
System.out.print("n2) Display a right triangular pattern of stars");
System.out.println("n3) Quit");

int userInput = in.nextInt();

if (userInput == 1) {
System.out.print("Enter the start number: ");
int firstInteger = in.nextInt();
System.out.print("Enter the second number: ");
int secondInteger = in.nextInt();

while (firstInteger < secondInteger); 
System.out.print(firstInteger);
firstInteger++;


}   else if (userInput == 2) {
System.out.print("Enter the height: ");
int triangleHeight = in.nextInt();

}   else if (userInput == 3);{
System.exit(userInput);

}

in.close();
}
}

您应该更改:

while (firstInteger < secondInteger); 
System.out.print(firstInteger);
firstInteger++;

while (firstInteger < secondInteger) {
System.out.print(firstInteger);
firstInteger++;
}
while (firstInteger < secondInteger);

这将被视为两条可执行的指令行,类似

while (firstInteger < secondInteger)
;

在while语句之后尝试删除;

最新更新