-1

I am trying to program a simple calculator program.But the problem is that when the user finishes his calculation, the program terminates. So I tried to make it in such a way that after you finish a calculation, the program asks you that if you want the program to terminate or not. But now I have no idea how to repeat the calculator program again infinitely.

My code (it is incomplete):

import java.util.Scanner ;
public class NewSrc
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter first number.... ");
        double x = sc.nextDouble();
        System.out.println("Enter next number....");
        double y = sc.nextDouble();
        double z = x*y;
        System.out.println("processing answer ....");
        System.out.println("The answer is :- " + z );

        System.out.println("Do you want to continue ?Answer with either Yes or No ");
        String Agree = "Yes";
        String Disagree = "No";
        String Continue = sc.nextLine();
        if (Continue == Agree)
        {   
          //How to restart it?
        }
    }
 }
Kayaman
  • 72,141
  • 5
  • 83
  • 121

3 Answers3

1

Use a while loop

import java.util.Scanner ;
public class NewSrc {

 public static void main(String[] args) {

    boolean continueCalculation = true;
    Scanner sc = new Scanner(System.in);
    while (continueCalculation == true) {        
        System.out.println("Enter first number.... ");
        double x = sc.nextDouble();
        System.out.println("Enter next number....");
        double y = sc.nextDouble();
        double z = x*y;
        System.out.println("processing answer ....");
        System.out.println("The answer is :- " + z );
        System.out.println("Do you want to continue ?Answer with either Yes or No   ");
        String continueInput = sc.nextLine();
        if (continueInput.equals("No")) //do not use == to compare strings
        {
             continueCalculation = false;
        }
   }

 }

}
CloudPotato
  • 1,255
  • 1
  • 17
  • 32
0

A minor modification in your program to continue with loop based on user entered input otherwise discontinue

public static void main(String[] args) {

    while(true){
            Scanner sc = new Scanner(System.in);
            System.out.println("Enter first number.... ");
             double x = sc.nextDouble();
            System.out.println("Enter next number....");
             double y = sc.nextDouble();
             double z = x*y;
            System.out.println("processing answer ....");
             System.out.println("The answer is :- " + z );

            System.out.println("Do you want to continue ?Answer with either Yes or No   ");
                String Disagree = "No";
                String Continue = sc.nextLine();
                if (Continue.equals(Disagree ))
                {
                   break;
                }
    }

        }
M Sach
  • 33,416
  • 76
  • 221
  • 314
-1
import java.util.Scanner ;
public class NewSrc
{
    public static void main(String[] args)
    {
        boolean doRepeat = false;
        Scanner sc = new Scanner(System.in);

        do{

        System.out.println("Enter first number.... ");
        double x = sc.nextDouble();
        System.out.println("Enter next number....");
        double y = sc.nextDouble();
        System.out.println("processing answer ....");
        System.out.println("The answer is :- " + (x*y) );

        System.out.println("Do you want to continue ?Answer with either Yes or No ");
        String userWish= sc.nextLine();
        if (userWish.equalsIgnoreCase("Yes"))
        {   
          doRepeat = true;
        }
        else doRepeat = false;

       }(doRepeat)
    }
 }

Please do not use additional Variable declarations for just printing the output

use EqualsIgnoreCase method wherever applicable , it will enhance usability for end user to type as they wish

Venkat
  • 2,549
  • 2
  • 28
  • 61