2015-01-29 3 views
0
public static void main(String[] args) { 
    boolean run = true; 
    while (run) { 
     Scanner kb = new Scanner(System.in); 
     System.out.println("Welcome to MetricMan - the best converter in town!"); 
     System.out.println("Choose and option:"); 
     System.out.println("A) Fahrenheit to Celsius"); 
     System.out.println("B) Celcius to Fahrenheit"); 
     System.out.println("C) Miles to Kilometers"); 
     System.out.println("D) Kilometers to Miles"); 
     System.out.println("E) Inches to Centimeters"); 
     System.out.println("F) Centimeters to Inches"); 
     System.out.println("X) Quit the program"); 

     System.out.print("Your choice > "); 
     String choice = kb.nextLine(); 

     double output; int input; 
     System.out.print("Enter the number of "); 
     switch (choice) { // do actions according to the user's choice. convert and display 
     case "A": 
      System.out.print("Farenheit > "); 
      input = kb.nextInt(); 
      output = convertFtoC(input); 
      System.out.println(input+" farenheit is "+output+" celsius"); 
      break; 
     case "B": 
      System.out.print("Celsius > "); 
      input = kb.nextInt(); 
      output = convertCtoF(input); 
      System.out.println(input+" celsius is "+output+" farenheit"); 
      break; 
     case "C": 
      System.out.print("miles > "); 
      input = kb.nextInt(); 
      output = convertMtoK(input); 
      System.out.println(input+" miles is "+output+" kilometres"); 
      break; 
     case "D": 
      System.out.print("kilometres > "); 
      input = kb.nextInt(); 
      output = convertKtoM(input); 
      System.out.println(input+" kilometres is "+output+" miles"); 
      break; 
     case "E": 
      System.out.print("inches > "); 
      input = kb.nextInt(); 
      output = convertItoC(input); 
      System.out.println(input+" inches is "+output+" centimeters"); 
      break; 
     case "F": 
      System.out.print("centimeters > "); 
      input = kb.nextInt(); 
      output = convertCtoI(input); 
      System.out.println(input+" centimeters is "+output+" inches"); 
      break; 
     case "X": 
      System.out.println(); System.out.println(); 
      System.out.println("May the odds be ever metric!"); 
      run = false; 
      break; 
     } 
     System.out.println(); 
     kb.nextLine(); 
    } 
} // end of main() 

내 주요 방법이 있습니다. 코드에서 볼 수 있듯이 X가 입력 된 경우, 코드 그러나 실제 출력은 이전 actualy 구성 출력 "의 번호를 입력"이Java while 루프가 의도 한대로 작동하지 않습니다.

... 
Your choice > X 
Enter the number of 

May the odds be ever metric! 

입니다, 바로 실행을 중지해야하며, 나는 이유를 볼 수 없습니다 그것이 거기에 있기 위해서. 아무도 이것을 고칠 수 있습니까?

+3

입력을 읽은 후에 출력하지만 출력하기 전에 스위치를 누르기 전에 출력하고 있습니다. 그것을 할 모자 : System.out.print ("숫자를 입력하십시오"); –

+2

일반적인 질문 : 각 반복마다 새 Scanner 객체를 만드는 이유는 무엇입니까? 그건 정말 끔찍한 일입니다. –

+0

X를 입력 한 후 코드가 멈추는 경우 입력 행 뒤에'run = false; break;'를 넣어야합니다. 그렇지 않으면 루프를 벗어나기 전에 많은 코드가 실행됩니다. – DaaaahWhoosh

답변

1

당신은 당신이 X 들어갔을 때의 인쇄가 너무

if(!choice.equals("X")){ 
System.out.print("Enter the number of "); 
} 

또는

각 스위치 케이스 내부에 System.out.print("Enter the number of ");을 넣어 아래처럼 if(check if string entered is not X) 내부 System.out.print("Enter the number of ");를 넣어하려고하는 이유 낭포가 먹으 렴 모든 경우에 System.out.print("Enter the number of ");을하고있다 @Tanmoy가 가리키고 싶은 곳

1

루프를 반복 할 때마다 해당 줄이 나타납니다. 그래서 당신은 기본적으로 출력이 예상된다, 그래서 스위치 전에이 작업을 인쇄하는

System.out.print("Enter the number of "); 

    switch (choice) { // do actions according to the user's choice. convert and display 

, 당신은 할 수 있습니다이 문제를 해결하는 한 가지 방법은 당신의 코드에서 X.

1

을 제외하고 각각의 경우 문이 전화를하는 것입니다 스위치가 아닌 외부의 케이스를 호출 할 때 x가 방금 종료됩니다.

관련 문제