2015-10-10 23 views
-2

我對Java真的很陌生,大部分知識都是自學的。你能幫我弄清楚這一點嗎?我可以在這裏使用「返回」或任何想法?

我們的老師希望我們製作一個關於Java的菜單。在哪裏輸出這樣的東西..

菜單 1 - Java的歷史 2- Java的關鍵字 3 - Java數組等。 。

你想讀一個(是/否): 如果是//

請輸入菜單號: //那麼它會顯示一個信息..

//我的問題是,我怎麼能在新輸入的值連接到第一個方法,這樣我就不會擁有遍佈再寫一遍......

這是什麼在我的心.. ,但即時通訊卡..

import java.util.Scanner; 

public class Program { 

public static void main (String [] args) { 

System.out.println("Please enter a number:"); 
int x = nextInt(); 

if (x == 1) { 

    for (int x = 5; x == 5;x++) // array of 1(first menu) 

System.out.println ("Do you want to read another? (Yes/No):"); 
System.out.println ("Please enter a menu number:") 

// return to if with it's new entered value.... 

    } 
    else if (x == 2) { 
    for loop of array 2 

    System.out.println ("Do you want to read another? (Yes/No):"); 
    System.out.println ("Please enter a menu number:") 

    // return to if with it's new entered value.... 


} 

else if (x == 3) { 
    for loop of array 3 

    System.out.println ("Do you want to read another? (Yes/No):"); 
    System.out.println ("Please enter a menu number:") 

    // return to if with it's new entered value.... 


else if (x ==4) { 
    for loop of array 4 

else if (x == 5) { 
    for loop of array 5 

else if (x == 6) { 
    for loop of array 6 

else if (x == 7) { 
    for loop of array 7 

else if (x == 8) { 
    for loop of array 8 

else if (x == 9) { 
    for loop of array 9 

else if (x == 10) { 
    for loop of array 10 

else { 
    //exit the program 
+0

你需要一個while循環嗎? – ergonaut

回答

0

如果我理解正確,您可能希望在程序中實現While或do while語句。 Example of While and Do While Loops

我不是很確定你想用你的程序完成什麼,但這裏有一個例子,我很快就如何在你的案例中使用for循環。 `import java.util.Scanner;

public class main { 

    public static void main(String[] args) { 
     Scanner scan = new Scanner(System.in); 

     boolean keepGoing = true; //As long as this is true the while statement will continue; 
     while (keepGoing) { 
      System.out.println("Enter a number"); 
      int x = scan.nextInt(); 

      //logic 
      if (x == 1) { 
       System.out.println("X is equal to 1"); 
      } else if (x == 2) 
       System.out.println("X is equal to 2"); 


      //prompts the user if they want to keep going. 
      System.out.println("Would you like to keep going? Y/N"); 

      String decision = scan.next(); 
      if (decision.equalsIgnoreCase("y")) { 
       keepGoing = true; 
      } else { 
       System.out.println("Quitting..."); 
       keepGoing = false; 

      } 
     } 
    } 
} 
` 
+0

是的,但我不完全知道如何以某種方式做到這一點,如果它要求用戶輸入一個新值,那麼新值將再次處理。 – NIece

+0

這段代碼是這樣做的。它會一直提示用戶輸入一個新號碼進行處理,直到他/她決定退出。這是While循環的工作。 – Rykuno

+0

是的,它的工作!非常感謝你!!這是一個很好的幫助..我完成了我的代碼..下次給你看。如果你沒有問題,我會把你的信告訴我。 – NIece

相關問題