2016-10-23 20 views
-1

私は4つの異なる方法(下図ではありません)のコードを持っており、ユーザーが選択したい方法を選択するためのメニューを作成しました。whileループを終了するにはどうすればいいですか?

ユーザーが5を入力しない限り、メソッドを常にメニュー画面に戻す方法をループするにはどうすればよいですか?

import java.util.Scanner; 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.PrintWriter; 

public class ReactionAnalysis { 
    public static void main (String [] args) throws FileNotFoundException 
    { 
    /// Menu Screen 
    System.out.println("What would you like to do?"); 
    System.out.println("1: Get the score of a word"); 
    System.out.println("2: Get the average score of words in a file (one word per line)"); 
    System.out.println("3: Find the highest/lowest scoring words in a file"); 
    System.out.println("4: Sort words from a file into positive.txt and negative.txt"); 
    System.out.println("5: Exit the program"); 

    Scanner in = new Scanner(System.in); 
    System.out.println("Enter a number 1-5"); 
    int numberChoice = in.nextInt(); 
    //// 

    while (numberChoice != 5) 
    { 
     if (numberChoice == 1) 
     { 
     String methodOne = methodOne(); 
     System.out.println(methodOne); 
     } 
     else if (numberChoice == 2) 
     { 
     String methodTwo = methodTwo(); 
     System.out.println(methodTwo); 
     } 
     else if (numberChoice == 3) 
     { 
     String methodThree = methodThree(); 
     System.out.println(methodThree); 
     } 
     else if (numberChoice == 4) 
     { 
     String methodFour = methodFour(); 
     System.out.println(methodFour); 
     } 
    } 
} 
} 

ここまでの手順のうちの1つを実行すると、手順が繰り返されます。ステップを通過してメニューに戻り、メニューに5が入力されていない限り、それを繰り返す必要があります。

+0

あなたのループ内であなたの 'choice'変数**を更新する必要があります。 –

+0

あなたの 'while-loop'でオプションを要求してください。それぞれのメソッドで 'numberChoice = in.nextInt();'を実行して、メソッドを再度選択するようユーザに促します。またはwhileループですべてをラップします。 –

+0

中にコード全体を入れてください。しばらくしてメソッドmainの最初の行に貼り付けてください。 – Paulo

答えて

0

申し訳ありませんが、ほとんどのコードを貼り付けてコピーしてください:ループを一番上に移動し、intを0に初期化して少なくとも1回は実行します。

import java.util.Scanner; 
    import java.io.File; 
    import java.io.FileNotFoundException; 
    import java.io.PrintWriter; 

    public class ReactionAnalysis { 
     public static void main (String [] args) throws FileNotFoundException 
     { 
     int numberChoice = 0; 
    //loop everything 
    while (numberChoice != 5) 
    { 
     /// Menu Screen 
    System.out.println("What would you like to do?"); 
    System.out.println("1: Get the score of a word"); 
    System.out.println("2: Get the average score of words in a file (one word per line)"); 
    System.out.println("3: Find the highest/lowest scoring words in a file"); 
    System.out.println("4: Sort words from a file into positive.txt and negative.txt"); 
    System.out.println("5: Exit the program"); 

    Scanner in = new Scanner(System.in); 
    System.out.println("Enter a number 1-5"); 
    numberChoice = in.nextInt(); 
    //// 

     if (numberChoice == 1) 
     { 
     String methodOne = methodOne(); 
     System.out.println(methodOne); 
     } 
     else if (numberChoice == 2) 
     { 
     String methodTwo = methodTwo(); 
     System.out.println(methodTwo); 
     } 
     else if (numberChoice == 3) 
     { 
     String methodThree = methodThree(); 
     System.out.println(methodThree); 
     } 
     else if (numberChoice == 4) 
     { 
     String methodFour = methodFour(); 
     System.out.println(methodFour); 
     } 
    } 
} 
} 
関連する問題