我將如何繼續此循環?我需要按Y來繼續循環,當我按N時它應該結束循環。而當我按下不同的輸入時,循環將不會停止,直到我只按Y或N.我是初學者感謝您的幫助!double做while循環
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!");
do{
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!") ;
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.equals ("Y"))
{
S1 = 0 ; MT = timesTable(0,0) ;
}
else if (option.equals ("N"))
System.out.println("Thank you for using the program!") ;
else System.out.println("Enter Y or N only!") ;
} while (option.equals("N")) ;
} while (option.equalsIgnoreCase("Y")) ;
}
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;
}
}
你是怎麼測試這個程序的?你能否清楚地提到實際和預期產出的步驟 –
對不起,我無法在任何地方看到兩個循環,當沒有正確縮進時。我甚至不會去嘗試。 **修正縮進**,因此人類可以遵循代碼。也許這甚至會讓你明白。 – Andreas