자바 무한반복 계산기 조건문 수정하고 싶습니다.

조회수 1045회

아래 프로그램은 무한 한복 계산인데요. 이건 배운대로 한건데 여기서 숫자를 따로 2번 입력 받는게 아니라 한번에 2개를 입력하고 다음에 바로 연산자를 입력해서 출력하고 싶습니다.

또 지금은 첫 번째 숫자를 정해진 숫자로 입력하면 끝나는 반복문 인데요. 이걸 코드 마지막에 계속할거냐고 질문하고 N 이라고 답하면 끝나는 프로그램으로 만들고 싶습니다. 조건문을 어디로 옮기면 이렇게 바꿀수 있을까요?

''' import java.util.Scanner;

public class nuber2 {

    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);

        double x = 0;
        double y = 0;
        String z = "";

        while (true) {
            System.out.println("Hi, I am really good at math! Put me to the test.");
            System.out.print("Please enter number and then press Enter: ");
            x = scan.nextDouble();

            if (x == 999) {
                System.out.print("Bye bye!");
                return;
            } else {

                System.out.print("Please enter number and then press Enter: ");
                y = scan.nextDouble();

                System.out.print("Please enter one of the operations +, -, * or / and press Enter:");
                z = scan.next();

                switch (z) {
                case "+":
                    System.out.println( + x + " + " + y + " = " + (x + y) );
                    System.out.println("I'm great at subtraction!\n");
                    break;
                case "-":
                    System.out.println( + x + " - " + y + " = " + (x - y) );
                    System.out.println("I'm great at subtraction!\n");
                    break;
                case "*":
                    System.out.println( + x + " * " + y + " = " + (x * y) );
                    System.out.println("I'm great at subtraction!\n");
                    break;
                case "/":
                    System.out.println( + x + " / " + y + " = " + (x / y) );
                    System.out.println("I'm great at subtraction!\n");
                    break;
                default:
                    System.out.println("잘못된 연산자입니다.\n");
                } } }}}

'''

  • 답변이 달리면 성의를 봐서라도 답변 채택을 하는 습관을 기르셨으면 합니다. 전대호 2018.9.12 11:03

1 답변

  • 이걸 코드 마지막에 계속할거냐고 질문하고 N 이라고 답하면 끝나는 프로그램으로 만들고 싶습니다. 조건문을 어디로 옮기면 이렇게 바꿀수 있을까요?

    질문 속에 원하는 답변이 있는 것 같습니다.

    z라는 변수를 char로 선언해서 사용하는 게 좋아보입니다.

    또한, 나누기 하실 때 0으로 나누면 에러가 발생하므로 try - catch문을 반드시 이용하시기 바랍니다.

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)