2016-02-28 162 views
-1

我將如何繼續此循環?我需要按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; 
} 


} 
+0

你是怎麼測試這個程序的?你能否清楚地提到實際和預期產出的步驟 –

+0

對不起,我無法在任何地方看到兩個循環,當沒有正確縮進時。我甚至不會去嘗試。 **修正縮進**,因此人類可以遵循代碼。也許這甚至會讓你明白。 – Andreas

回答

0

你應該使用 '破' 語句的

代碼:

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; 
} 

同樣,你可以在你的「其他人使用它'您的代碼聲明...

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(); 
} 
0

您可以使用此代碼

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; 
 
} 
 
}

,並可以循環這一切。

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; 
    } 

} 

注:解決方案我在這裏給出的是所面臨的問題。有更好的方法來解決這個問題。

+0

非常感謝!我的編碼詞彙非常有限。我只知道基本代碼 –

+0

你有沒有試過這個程序?它有用嗎? –

+0

它確實有效。但是現在我遇到了一個新問題。我怎樣才能使它成爲一張桌子?每個數字都有框。像桌子一樣。你能幫我嗎 ? TY –