I need help putting a do while loop in my code. My code is correct but I
Question:
I need help putting a do while loop in my code. My code is correct but I need to put it in a loop so it can start over if the input is y. Please help me out, I'm really confused.
My code:
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in);
System.out.println("Enter the calculator mode: Standard/Scientific?"); String mode = input.next(); if (mode.equalsIgnoreCase("standard")) { System.out.println("The calculator will operate in standard mode."); System.out.println("Enter '+' for addition, '-' for subtractions, '*' for multiplication, '/' for division"); String operator = input.next();
if (operator.equals("+")) { System.out.println("How many numbers do you want to add?"); int num = input.nextInt(); double sum = 0;
System.out.println("Enter " + num + " numbers "); for (int i = 0; i < num; i++) { double number = input.nextDouble(); sum = sum + number; } System.out.println("Result: " + sum);
} else if (operator.equals("-")) { System.out.println("How many numbers do you want to subtract?"); int num = input.nextInt(); double difference = 0; System.out.println("Enter " + num + " numbers "); for (int i = 0; i < num; i++) { double number = input.nextDouble(); if (i == 0) { difference = number; } else { difference = difference - number; } } System.out.println("Result: " + difference);
} else if (operator.equals("*")) { System.out.println("How many numbers do you want to multiply?"); int num = input.nextInt(); double product = 1;
System.out.println("Enter " + num + " numbers "); for (int i = 0; i < num; i++) { double number = input.nextDouble(); product = product * number; } System.out.println("Result: " + product);
} else if (operator.equals("/")) { System.out.println("How many numbers do you want to divide?"); int num = input.nextInt(); double quotient = 0;
System.out.println("Enter " + num + " numbers "); for (int i = 0; i < num; i++) { double number = input.nextDouble(); if (i == 0) { quotient = number; } else { quotient = quotient / number; } } System.out.println("Result: " + quotient);
} else { System.out.println("Invalid operator " + operator); }
} else if (mode.equalsIgnoreCase("scientific")) { System.out.println("The calculator will operate in scientific mode."); System.out.println("Enter '+' for addition, '-' for subtractions, '*' for multiplication, '/' for division, 'sin' for sin x, 'cos' for cos x, 'tan' for tan x:"); String operator = input.next(); if (operator.equals("+")) { System.out.println("How many numbers do you want to add?"); int num = input.nextInt(); double sum = 0;
System.out.println("Enter " + num + " numbers "); for (int i = 0; i < num; i++) { double number = input.nextDouble(); sum = sum + number; } System.out.println("Result: " + sum);
} else if (operator.equals("-")) { System.out.println("How many numbers do you want to subtract?"); int num = input.nextInt(); double difference = 0;
System.out.println("Enter " + num + " numbers "); for (int i = 0; i < num; i++) { double number = input.nextDouble(); if (i == 0) { difference = number; } else { difference = difference - number; } } System.out.println("Result: " + difference);
} else if (operator.equals("*")) { System.out.println("How many numbers do you want to multiply?"); int num = input.nextInt(); double product = 1;
System.out.println("Enter " + num + " numbers "); for (int i = 0; i < num; i++) { double number = input.nextDouble(); product = product * number; } System.out.println("Result: " + product);
} else if (operator.equals("/")) { System.out.println("How many numbers do you want to divide?"); int num = input.nextInt(); double quotient = 0;
System.out.println("Enter " + num + " numbers "); for (int i = 0; i < num; i++) { double number = input.nextDouble(); if (i == 0) { quotient = number; } else { quotient = quotient / number; } } System.out.println("Result: " + quotient);
} else if (operator.equals("sin")) { System.out.println("Enter a number in radians to find the sine"); double radians = input.nextDouble(); double result = Math.sin(radians); System.out.println("Result: " + result);
} else if (operator.equals("cos")) { System.out.println("Enter a number in radians to find the cosine"); double radians = input.nextDouble(); double result = Math.cos(radians); System.out.println("Result: " + result);
} else if (operator.equals("tan")) { System.out.println("Enter a number in radians to find the tangent"); double radians = input.nextDouble(); double result = Math.tan(radians); System.out.println("Result: " + result);
} else { System.out.println("Invalid operator " + operator); }
} else { System.out.println("Invalid mode " + mode); }
System.out.println("Do you want to start over? (Y/N)"); String response = input.next();
if (response.equalsIgnoreCase("y")) { main(args); } else { System.out.println("Goodbye"); } } }
A Concise Introduction to Logic
ISBN: 978-1305958098
13th edition
Authors: Patrick J. Hurley, Lori Watson