简体   繁体   中英

I am being asked to create a loop while calculator is operating in two modes Standard and Scientific and getting user imput

Asking a user to input calculator mode and operation.
then make a while loop after getting user input.
I have to do all operations once I receive the input I can't seem to get user input and calculate the code

import java.util.Scanner;

 public class loopsProject {

 public static void main(String[] args) {

 System.out.println("Hello Codiva");



  Scanner scnr = new Scanner(System.in);
  String mode;
  String operator;
  double numOne;
  double numTwo;
  double numThree;
  double numofdValues;
  String result;
  //asking user to enter mode
  System.out.print("Enter the calculator mode: Standard/Scientific?");

  mode = scnr.nextLine();
  while (mode.equals("Standard")) {
      System.out.println("Enter '+' for addition, '-' for subtractions, '*' 
  for multiplication, '/' for division");
      operator = scnr.nextLine();
  }

  while (numOne != 0 && numTwo != 0 && numThree !=0)
    if (operator.equals("+")) {
        System.out.println("How many numbers would you like to add?");
        numofdValues = scnr.nextDouble();
        System.out.println("Enter + numofdValues + numbers");
        numOne = scnr.nextDouble;
        numTwo = scnr.nextDouble;
        numThree = scnr.nextDouble;
        result = numOne + numTwo + numThree;  
        System.out.println("Your added answer is:" + result);
    }
    if (operator.equals("-")) {
       System.out.println("How many numbers would you like to subtract?");
        numofdValues = scnr.nextDouble();
        System.out.println("Enter + numofdValues + numbers");
        num1 = scnr.nextDouble;
        num2 = scnr.nextDouble;
        num3 = scnr.nextDouble;
        result = numOne - numTwo - numThree;   
        System.out.println("Your subtracted answer is:" + result);
  }
    if (operator.equals("*"))
        System.our.println("Your multiplied answer is:" + result);

    if (operator.equals("/"))

    if (operator.equals("invalid")) {
      System.out.println("Your imput is invalid, please try again");
    }

You mentioned you're having trouble with

setting up the user input for the entire problem

Hopefully this will provide you with the structure to move forward. You have two while loops, but you need one outside to control the looping of the user choosing modes or quitting.

public class loopsProject {
    public static void main(String[] args) {
        System.out.println("Hello Codiva");

        //Your variables here

        //asking user to enter mode
        System.out.print("Enter the calculator mode: Standard/Scientific?");

        mode = scnr.nextLine();
        //This loop controls the mode, user will keep on entering or they can quit
        while (!mode.equals("Quit")) {
            if (mode.equals("Standard")) {
                //your Standard logic
            }
            else if (mode.equals("Scientific")) {
                //your Scientific logic
            }
            else {
                //Handle however you want or quit
                break;
            }

            //Ask user for input again
            System.out.print("Enter the calculator mode: Standard/Scientific?");
            mode = scnr.nextLine();
        }
    }
}

if operation is invalid reprompt the user again

For the case when operations are invalid, what you can do is just tell the user the operation is invalid and continue to skip asking them for input. That way it retains the mode and will restart the logic in the next loop. This would go inside your Standard logic or Scientific logic, each time you need to validate the user's input.

if (...) { //your checks for user input invalid operation here
    System.out.println("Invalid operation, please retry.");
    continue;
}

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