2013-12-18 2 views
0

나는 가르치는 목적을위한 간단한 프로그램을 썼고, 당신이 선택한 계산에 대한 이름과 해답을 인쇄하는 부분을 제외한 모든 것은 작동합니다. if 문은 앞으로 진행하기 전에 한 걸음 뒤로 물러나는 것처럼 두 번 실행됩니다.두번 인쇄하기

"계속 하시겠습니까?"라는 메시지가 인쇄되지만 Y/N을 입력하라는 메시지 대신 AGAIN 계산 결과를 출력하고 나머지는 계속 진행할 것인지 묻습니다 두 번째로 실제로 입력을 기다립니다.

미리 감사드립니다.

import java.util.Scanner; 

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

     boolean cont; 
     String name, yorn; 
     double num1, num2, answer; 
     int choice, iterator = 0; 

     System.out.print("What is your name? "); 
     name = reader.nextLine(); 

     System.out.print("Please enter a number: "); 
     num1 = reader.nextDouble(); 
     if(num1%2 != 0) 
     { 
      System.out.println(name + ", the number uou entered, " + num1 + ", is odd"); 
     } 
     else 
     { 
      System.out.println(name + ", the number uou entered, " + num1 + ", is even"); 
     } 

     System.out.print("Please enter a second number: "); 
     num2 = reader.nextDouble(); 
     if(num2%2 != 0) 
     { 
      System.out.println(name + ", the second number you entered, " + num2 + ", is odd"); 
     } 
     else 
     { 
      System.out.println(name + ", the second number you entered, " + num2 + ", is even"); 
     } 

     System.out.println("1. Add"); 
     System.out.println("2. Subtract"); 
     System.out.println("3. Multiply"); 
     System.out.println("4. Divide"); 
     System.out.print("Please enter the number for the operation you would like to perform on your numbers: "); 
     choice = reader.nextInt(); 

     cont = true; 
     while(cont) 
     { 

      while(choice != 1 && choice != 2 && choice != 3 && choice != 4) 
      { 
       System.out.print("The number entered is not one of the options. Please choose one of the operations: "); 
       choice = reader.nextInt(); 
      } 

      if(choice == 1) 
      { 
       answer = num1 + num2; 
       System.out.println(name +" the sum of " + num1 + " and " + num2 + " is: " + answer); 
      } 
      else if(choice == 2) 
      { 
       answer = num1 - num2; 
       System.out.println(name +" the difference between " + num1 + " and " + num2 + " is: " + answer); 
      } 
      else if(choice == 3) 
      { 
       answer = num1 * num2; 
       System.out.println(name +" the product of " + num1 + " and " + num2 + " is: " + answer); 
      } 
      else //if(choice == 4) 
      { 
       answer = num1/num2; 
       System.out.println(name +" the quotient of " + num1 + " and " + num2 + " is: " + answer); 
      } 


      System.out.print("Would you like to do anything else (Y/N)? "); 
      yorn = reader.nextLine(); 

      if(yorn.equals("Y")) 
      { 
       System.out.println("1. Add"); 
       System.out.println("2. Subtract"); 
       System.out.println("3. Multiply"); 
       System.out.println("4. Divide"); 
       System.out.print("Please enter the number for the operation you would like to perform on your numbers: "); 
       choice = reader.nextInt(); 

      } 
      else if (yorn.equals("N")) 
      { 
       System.out.println("Thank you for using this prgram. Have a good day"); 
       cont = false; 

      } 
     } 
    } 
} 
+1

을 if'이 PLZ 구체적으로 어떤' – exexzian

+1

어떤 경우 블록 어느 루프? 어떤 텍스트 줄을 두 번 보았습니까? – clay

+0

수정 사항을 확인하십시오. 죄송 합니다만 충분히 명확하지 못해서 죄송합니다. 귀하의 답변을 주셔서 감사합니다 – PracticeFirst

답변

3

문제가 nextDouble() 기능입니다 :

여기 내 코드입니다. 예, double을 읽지 만 버퍼에 줄 바꿈 문자 (\n)도 읽습니다. 다음 번에 nextLine()으로 전화하면 입력을 기다리는 대신 줄 바꿈을 즉시 구문 분석합니다. 입력을 다시 요청하기 전에 또 다른 reader.nextLine()을 호출하여 개행 문자의 버퍼를 지우십시오.

+0

그걸 고쳤습니다. 정말 고맙습니다. :) – PracticeFirst

0

@ adback03은 스캐너 (obj)입니다. nextDouble() 버퍼 "\ n"및 nextLine() 버퍼에서 읽습니다. 루프 난 당신이 스위치 케이스를 사용하는 suggesst [주제 끄기]

을 계속 따라서 동안

switch(choice) { 
    case 1: System.out.println(num1 + num2); break; 
    case 2: System.out.println(num1 - num2); break; 
    case 3: System.out.println(num1 * num2); break; 
    case 4: System.out.println(num1/num2); break; 
    default: System.out.println("Invalid choice"); 
} 

훨씬 쉽게 읽기 :