我希望此代码继续运行,只要用户在出现提示时继续输入“Y”或“y”,任何其他输入都应从程序中退出。正如我现在的代码,无论输入什么,它都只运行一次。我该如何解决?
这是我的代码:
import java.util.Scanner;
public class zigzag {
public static void main(String[] args) {
Scanner myScanner, sc;
myScanner = new Scanner(System. in );
while (true) {
System.out.print("Enter an integer between 3 and 33: ");
int nStars = myScanner.nextInt();
if (nStars > 33 || nStars < 3) {
System.out.println("Sorry, this input is out of the accepted range");
} else {
for (int i = 0; i < nStars; i++) {
System.out.print("*");
}
System.out.println(" ");
}
for (int j = 0; j < nStars - 2; j++) {
System.out.print(" ");
for (int k = 0; k <= j - 1; k++) {
System.out.print(" ");
}
System.out.println("*");
}
for (int g = 0; g < nStars; g++) {
System.out.print("*");
}
System.out.println(" ");
System.out.print("Enter 'Y' or 'y' to go again: ");
char ch = myScanner.next().charAt(0);
if (ch != 'y' || ch != 'Y') {
break;
}
}
}
}
最佳答案
你想要
if(ch!='y' && ch!='Y'){
不是
if(ch!='y' || ch!='Y'){
关于java - 运行程序直到满足特定条件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29883418/