할 일의 실행 동안 자바 계산기 프로그램에서 루프

요기 라즈 :

내 자바 프로그램에서 나는 변수가 true 인 경우에만 '할 루프 코드 블록'에서 실제 계산기 코드를 실행해야 부울 변수 '결정'을 사용하고 있지만, 그 결정 변수 인 경우에도 루프가 어쨌든 실행되는 동안 수행 그릇된. 나는 자바와 JDK (10) (모두 최신 버전입니다) 이클립스 IDE를 사용하고 있습니다. 해결책으로 저를 도와주세요. 코드는 다음과 같다

import java.util.Scanner;

public class apples {

public static void main(String[] args) {

    int option,a,b,c;
    boolean decision;
    System.out.println("We are here to create a calculator");
    System.out.print("Do you want to switch on the calculator:");
    Scanner yogi = new Scanner(System.in);
    decision = yogi.nextBoolean();
    do {
        System.out.println("Following operations are available to perform:");
        System.out.println("1. Addition");
        System.out.println("2. Subtraction");
        System.out.println("3. Multiplication");
        System.out.println("4. Division");
        System.out.print("Enter the operations do you want to perform:");
        option = yogi.nextInt();
        System.out.println("Choice of operation is:"+option);

        switch(option) {

        case 1:
            System.out.println("Enter two numbers to be added:");
            a = yogi.nextInt();
            b = yogi.nextInt();
            c = a + b;
            System.out.print("Addition:"+c);
            break;

        case 2:
            System.out.println("Enter two numbers to be subtracted:");
            a = yogi.nextInt();
            b = yogi.nextInt();
            c = a - b;
            System.out.print("subtracted:"+c);
            break;

        case 3:
            System.out.println("Enter two numbers to be multiplied:");
            a = yogi.nextInt();
            b = yogi.nextInt();
            c = a * b;
            System.out.print("multiplied:"+c);
            break;

        case 4:
            System.out.println("Enter two numbers to be divided:");
            a = yogi.nextInt();
            b = yogi.nextInt();
            c = a / b;
            System.out.print("divided:"+c);
            break;

        default:
            System.out.println("This is a wrong choice");
            break;
        }
    }while(decision==true);
  }
}
고쿨 나스 KP :

결정 변수가 false 인 경우에도 루프 동안 DO 어쨌든 실행

do...while뜻 한번 상태를 체크하기 전에 신체를 행한다.

하나에 대한 코드를 수정 while하거나 추가 if-else둘러싸을 do...while.

추천

출처http://43.154.161.224:23101/article/api/json?id=192063&siteId=1