-3

I am trying to create a calculator for class, we have to show the operation, we have to make it loop back to the top where it says to enter the first number but also give an option to return back to the top every time I try to add something, something goes wrong any ideas on how to make this calculator on how I want it to be?

import java.util.Scanner;

public class Calculator {
  public static void main(String[] argument) {
    {
      char repeat; //initialize repeat
      String input; //initialize input
      Scanner keyboard = new Scanner(System.in);
      System.out.println(" Please enter the first Number");
      int Number1 = keyboard.nextInt();
      System.out.println("Please enter the second number");
      int Number2 = keyboard.nextInt();
      System.out.println("Please enter the operation");
      keyboard.nextLine();
      {
        System.out.println(
            "The result of " + Number1 + " + " + Number2 + " = " + (Number1 + Number2));
        System.out.println(
            "The result of " + Number1 + " - " + Number2 + " = " + (Number1 - Number2));
        System.out.println(
            "The result of " + Number1 + " * " + Number2 + " = " + (Number1 * Number2));
        System.out.println(
            "The result of " + Number1 + " % " + Number2 + " = " + (Number1 % Number2));
        System.out.println(
            "The result of " + Number1 + " / " + Number2 + " = " + (Number1 / Number2));
        if (Number2 >= 0) System.out.println("Division by Zero is not possible.");
        System.out.println("Please run program again and ");
        System.out.println("enter a number other then zero.");
      }
      String userChoice = null;
      do {
        System.out.println("Would you like to " + "select again? ");
        System.out.print("Enter Y for yes or N for no: ");
        input = keyboard.nextLine();
        repeat = input.charAt(0);
      } while (repeat == 'Y' || repeat == 'y');
      System.out.println(userChoice); //give user option to go back to the top

      {
        keyboard.close();
      }
    }
  }
}
Andy Turner
  • 137,514
  • 11
  • 162
  • 243
Jacob
  • 7
  • 2
  • 1
    your do{ of the do while should be placed above the section that you want to keep repeating. why dont you try starting your do { section above this line `System.out.println(" Please enter the first Number");` ? – Renuka Deshmukh Mar 09 '16 at 20:18
  • 5
    `every time I try to add something, something goes wrong` is an extremely vague problem statement. – tnw Mar 09 '16 at 20:20
  • 2
    Side note: you are using the curly braces to create blocks ... without a need to do so. It is good practice that the body of loops or if / then / else ... and so on is "blocked" using { statements }. But { { whatever } } doesn't do anything useful for you. – GhostCat Mar 09 '16 at 20:20
  • because I need the whole thing to loop back to the top, I need a first number then a second number, the equation and then after I got the answer I need to be able to go back or just cancel he process – Jacob Mar 09 '16 at 20:24
  • 1
    You do the division by zero before the check `if (Number2 >= 0)`. (Which should really just be `if (Number2 == 0)`) – OneCricketeer Mar 09 '16 at 20:27

2 Answers2

2

Your do should be at the beginning of the method. That way i would execute one time and then ask the user if he/she wants to repeat the process.

do{
   //some process
  }
   while(condition is true)
DCruz22
  • 806
  • 1
  • 9
  • 18
0

Your code has a number of issues :

  1. If you want to prompt user to enter numbers again and again, you need to move you do { of the do-while loop.

do { System.out.println(" Please enter the first Number"); int Number1 = keyboard.nextInt();

  1. you are calculating division and mod before you check if Number2 == 0. So if you number2 is infact 0, your program will throw an exception. You need to change it to :

if (Number2 == 0) System.out.println("Division by Zero is not possible."); else { System.out.println("The result of " + Number1 + " % " + Number2 + " = " + (Number1 % Number2)); System.out.println("The result of " + Number1 + " / " + Number2 + " = " + (Number1 / Number2)); }

  1. After making these changes, I was getting an error on System.out.print("Enter Y for yes or N for no: "); keyboard.nextLine(); as it would not wait for user input. I looked it up and looks like an issue with the way Scanner works, explained here.

So making changes to your code:

System.out.print("Enter Y for yes or N for no: ");
keyboard.nextLine();
input = keyboard.nextLine();
repeat = input.charAt(0);

And voila! It works. :)

Community
  • 1
  • 1
Renuka Deshmukh
  • 998
  • 9
  • 16