2016-10-23 4 views
-1

4 가지 방법으로 된 코드가 있는데 (아래 그림에는 보이지 않음) 사용자가 선택할 방법을 선택할 수있는 메뉴가 생성되었습니다.while 루프를 종료하는 방법은 무엇입니까?

어떻게 사용자가 입력하지 않으면 계속해서 메뉴 화면으로 돌아가는 방식으로 루프를 돌릴 수 있습니까? 나는 완전히 단계 중 하나를 통과 한 후

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

은 지금, 그냥 단계를 반복합니다. 단계를 거쳐 메뉴로 돌아가서 메뉴에 5가 입력되어 있지 않으면 반복해야합니다.

+0

루프 내부에서'choice' 변수 **를 업데이트해야합니다. –

+0

'while-loop'에서 옵션을 다시 요청하여 계속 진행하십시오. 'numberChoice = in.nextInt();'를 호출하고 메서드를 다시 선택하라는 메시지를 표시합니다. 또는 while 루프를 사용하여 모든 것을 래핑하십시오. –

+0

잠시 동안 전체 코드를 넣으십시오. 잠시 자르고 메소드 main의 첫 번째 줄에 붙여 넣으십시오. – Paulo

답변

0

죄송합니다. 대부분의 코드를 붙여 넣기 만하면됩니다. 루프를 맨 위로 이동하고 int를 0으로 초기화하여 적어도 한 번 이상 실행하게합니다.

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); 
     } 
    } 
} 
} 
관련 문제