我将如何继续此循环?我需要按Y继续循环,当我按N时它应该结束循环。当我按下不同的输入时,循环不会停止,直到我只按Y或N.我是初学者,谢谢你的帮助!
android:layout_below="@id/app_bar_layout
答案 0 :(得分:1)
您可以使用此代码
while(true){
//scan the choice then enter the loop
while (!choice.equals("N")){
// you should use "switch case".
}
if(choice.equals("N")){
System.out.println("Thank you for using the program!") ;
break;
}
}
你可以把它全部循环。
答案 1 :(得分:0)
您应该使用'break'语句
您的代码:
else if (option.equals ("N") )
{
System.out.println("Thank you for using the program!") ;
}
更新的代码:
else if (option.equals ("N") )
{
System.out.println("Thank you for using the program!") ;
break;
}
同样,您可以在代码的“else”语句中使用它...
答案 2 :(得分:0)
我建议您在其他编程实践中研究抽象,以便您的代码更易读,更容易理解。但这是一个循环过程的简单方法(取决于用户输入(选择))。
String choice;
while (!(choice = readLine()).equals("N")){
if (choice == "Y"){
//Do the process you wish to loop
} else{
System.out.println("Invalid input.");
}
}
private String readLine(){
System.out.println("Enter Y to enter another number.\nEnter N to exit.");
return input.nextLine();
}
答案 3 :(得分:0)
试试这个程序:
import java.util.Scanner;
public class Proj {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int S1 = 0;
String option = null;
int[][] MT = new int[S1][S1];
System.out.println("Welcome To The Multiplication Table Program!");
boolean validInput = true;
boolean exitOuterLoop = false;
do {
validInput = true; // to reset from second time onwards
System.out.print("Input a number from 1-20: ");
String Num = input.nextLine();
if (Num.equals("1")) {
S1 = 1;
MT = timesTable(1, 1);
} else if (Num.equals("2")) {
S1 = 2;
MT = timesTable(2, 2);
} else if (Num.equals("3")) {
S1 = 3;
MT = timesTable(3, 3);
} else if (Num.equals("4")) {
S1 = 2;
MT = timesTable(4, 4);
} else if (Num.equals("5")) {
S1 = 2;
MT = timesTable(5, 5);
} else if (Num.equals("6")) {
S1 = 2;
MT = timesTable(6, 6);
} else if (Num.equals("7")) {
S1 = 2;
MT = timesTable(7, 7);
} else if (Num.equals("8")) {
S1 = 2;
MT = timesTable(8, 8);
} else if (Num.equals("9")) {
S1 = 2;
MT = timesTable(9, 9);
} else if (Num.equals("10")) {
S1 = 2;
MT = timesTable(10, 10);
} else if (Num.equals("11")) {
S1 = 2;
MT = timesTable(11, 11);
} else if (Num.equals("12")) {
S1 = 2;
MT = timesTable(12, 12);
} else if (Num.equals("13")) {
S1 = 2;
MT = timesTable(13, 13);
} else if (Num.equals("14")) {
S1 = 2;
MT = timesTable(14, 14);
} else if (Num.equals("15")) {
S1 = 2;
MT = timesTable(15, 15);
} else if (Num.equals("16")) {
S1 = 2;
MT = timesTable(16, 16);
} else if (Num.equals("17")) {
S1 = 2;
MT = timesTable(17, 17);
} else if (Num.equals("18")) {
S1 = 2;
MT = timesTable(18, 18);
} else if (Num.equals("19")) {
S1 = 2;
MT = timesTable(19, 19);
} else if (Num.equals("20")) {
S1 = 2;
MT = timesTable(20, 20);
} else {
System.out.println("Error! Input a number from 1-20 only!");
validInput = false;
}
if (validInput) {
for (int row = 0; row < MT.length; row++) {
for (int column = 0; column < MT[row].length; column++) {
System.out.printf("%3d ", MT[row][column]);
}
System.out.println();
}
System.out
.println("Enter 'Y' to enter another number \nEnter 'N' to exit ");
do {
System.out.print("Y/N: ");
option = input.nextLine();
if (option.equalsIgnoreCase("Y")) {
S1 = 0;
MT = timesTable(0, 0);
} else if (option.equalsIgnoreCase("N")) {
System.out.println("Thank you for using the program!");
exitOuterLoop = true;
}
else
System.out.println("Enter Y or N only!");
} while (!option.equalsIgnoreCase("Y")
&& !option.equalsIgnoreCase("N"));
}
} while (!exitOuterLoop);
}
public static int[][] timesTable(int r, int c) {
int[][] TT = new int[r][c];
for (int row = 0; row < TT.length; row++) {
for (int column = 0; column < TT[row].length; column++) {
TT[row][column] = (row + 1) * (column + 1);
}
}
return TT;
}
}
注意:我在这里给出的解决方案是针对您遇到的问题。有更好的方法来解决这个问题。