简体   繁体   中英

How to ask the user if he wants to repeat the calculation using the do while

This is a basic calculator I wrote in java. Once the output is displayed, I want to ask the user if he wants to continue, if he say yes, then repeat the process. I have checked many topics on stack overflow and I still don't know how to achieve my goal with the do while loop , anyone can help?

import java.util.Scanner;

public class SecondQuestion {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        double firstN; 
        double secondN; 
        char operator;

        Scanner readInput = new Scanner(System.in);

        System.out.printf("Type a number, operator, number --" + "separated by a space: "); 

        firstN = readInput.nextDouble();
        operator = readInput.next().charAt(0); 
        secondN = readInput.nextDouble();

        if (operator == '+')
            System.out.printf("%f + %f = %f", firstN, secondN, firstN + secondN); 

        else if (operator == '-') 
            System.out.printf("%f - %f = %f", firstN, secondN, firstN - secondN); 

        else if (operator == '*')
            System.out.printf("%f * %f = %f", firstN, secondN, firstN * secondN); 

        else if (operator == '/')
            System.out.printf("%f / %f = %f", firstN, secondN, firstN / secondN); 

        else if (operator == '%')
            System.out.printf("%f %% %f = %f", firstN, secondN,firstN % secondN); 

        else
            System.out.printf("Unknown operator"); 
        System.out.printf("\n\n");

        int loopCount = 0;
        char charResponse='y';
        Scanner readInput1 = new Scanner (System.in);
        while (charResponse !='y')
        {
            System.out.println("press y");
            charResponse = readInput1.next().charAt(0);
        }
    }

}
public static void main(String[] args) {
    Scanner readInput = new Scanner(System.in);
    do {
        calculate(readInput);
        System.out.println("do you want to continue: y or n");
        String userAnswer = readInput.next();
        if(userAnswer.charAt(0) == 'y')
            calculate(readInput);
        else if(userAnswer.charAt(0) == 'n')
            break;

    } while (true);
    readInput.close();
}

private static void calculate(Scanner readInput) {

    double firstN; 
    double secondN; 
    char operator;

    System.out.printf("Type a number, operator, number --" + "separated by a space: "); 

    firstN = readInput.nextDouble();
    operator = readInput.next().charAt(0); 
    secondN = readInput.nextDouble();

    if (operator == '+')
        System.out.printf("%f + %f = %f", firstN, secondN, firstN + secondN); 

    else if (operator == '-') 
        System.out.printf("%f - %f = %f", firstN, secondN, firstN - secondN); 

    else if (operator == '*')
        System.out.printf("%f * %f = %f", firstN, secondN, firstN * secondN); 

    else if (operator == '/')
        System.out.printf("%f / %f = %f", firstN, secondN, firstN / secondN); 

    else if (operator == '%')
        System.out.printf("%f %% %f = %f", firstN, secondN,firstN % secondN); 

    else
        System.out.printf("Unknown operator"); 

    System.out.printf("\n\n");

}

Using a do-while :

public static void main(String[] args) {
    // TODO Auto-generated method stub

    double firstN;
    double secondN;
    char operator;

    Scanner readInput = new Scanner(System.in);
    char charResponse = 'y';
    do {
        System.out.printf("Type a number, operator, number --" + "separated by a space: ");

        firstN = readInput.nextDouble();
        operator = readInput.next().charAt(0);
        secondN = readInput.nextDouble();

        if (operator == '+')
            System.out.printf("%f + %f = %f", firstN, secondN, firstN + secondN);

        else if (operator == '-')
            System.out.printf("%f - %f = %f", firstN, secondN, firstN - secondN);

        else if (operator == '*')
            System.out.printf("%f * %f = %f", firstN, secondN, firstN * secondN);

        else if (operator == '/')
            System.out.printf("%f / %f = %f", firstN, secondN, firstN / secondN);

        else if (operator == '%')
            System.out.printf("%f %% %f = %f", firstN, secondN, firstN % secondN);

        else
            System.out.printf("Unknown operator");
        System.out.printf("\n\n");

        int loopCount = 0;
        Scanner readInput1 = new Scanner(System.in);
        System.out.println("press y to continue");
        charResponse = readInput1.next().charAt(0);
    } while (charResponse == 'y');

}

The do-while loop is a post-increment loop so it will execute first and then check the condition in the while.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM