Shriesh Kumar
Shriesh Kumar

Reputation: 21

How can I go back to a part of a program that is already executed?

I was creating a calculator. And in it, I have included different methods for different operations. It will take 2 numbers from the user and then ask for what operation to perform, and then execute the corresponding method. Here's the program:

import java.io.*;
public class calculator
{
  void add(double a,double b)
  {
    double a1=a+b;
    System.out.println("Their sum will be "+a1);
  }
  void subtract(double a,double b)
  {
    double a2=a-b;
    System.out.println("Their difference will be "+a2);
  }
  void multiply(double a,double b)
  {
    double a3=a*b;
    System.out.println("Their product will be "+a3);
  }
  void divide(double a,double b)
  {
    double a4=a/b;
    System.out.println("Their division will result to "+a4);
  }
  void findremainder(double a,double b)
  {
    if (a>b)
    {
      double a5a=a%b;
      System.out.println("The remainder obtained after their division is "+a5a);
    }
    if (b>a)
    {
      double a5b=b%a;
      System.out.println("The remainder obtained after their division is "+a5b);
    }
  }
  void percent(double a,double b)throws IOException
  {
    InputStreamReader read=new InputStreamReader(System.in);
    BufferedReader in=new BufferedReader(read);
    System.out.println("What type of percentage you want ? [give the number in the list below which correspons to that operation]");
    System.out.println("1=> Smaller number by Bigger number");
    System.out.println("2=> Bigger number by smaller number");
    int o=Integer.parseInt(in.readLine());
    if (o==1)
    {
      if (a>b)
      {
        double per=(100*b)/a;
        System.out.println("The percentage is "+per+"%");
      }
      if (b>a)
      {
        double per2=(100*a)/b;
        System.out.println("The percentage is "+per2+"%");
      }
    }
    if (o==2)
    {
      if (a>b)
      {
        double per=(100*a)/b;
        System.out.println("The percentage is "+per+"%");
      }
      if (b>a)
      {
        double per2=(100*b)/a;
        System.out.println("The percentage is "+per2+"%");
      }
    }
  }
  void power(double a,double b)
  {
    double pow=Math.pow(a,b);
    System.out.println(a+" raised to "+b+" is equal to "+pow);
  }
  void squareroot(double a,double b)throws IOException
  {
    InputStreamReader read=new InputStreamReader(System.in);
    BufferedReader in=new BufferedReader(read);
    System.out.println("Which no. you want me to take out the square root of ? [give the number in the list below which correspons to that operation]");
    System.out.println("1=> 1st given number");
    System.out.println("2=> 2nd given number");
    int h=Integer.parseInt(in.readLine());
    if (h==1)
    {
      double sq=Math.sqrt(a);
      System.out.println("Square root of "+a+" is "+sq);
    }
    if (h==2)
    {
      double sq2=Math.sqrt(b);
      System.out.println("Square root of "+b+" is "+sq2);
    }
  }
  void cuberoot(double a,double b)throws IOException
  {
    InputStreamReader read=new InputStreamReader(System.in);
    BufferedReader in=new BufferedReader(read);
    System.out.println("Which no. you want me to take out the cube root of ? [give the number in the list below which correspons to that operation]");
    System.out.println("1=> 1st given number");
    System.out.println("2=> 2nd given number");
    int po=Integer.parseInt(in.readLine());
    if (po==1)
    {
      double cr=Math.cbrt(a);
      System.out.println("Cube root of "+a+" is "+cr);
    }
    if (po==2)
    {
      double cr2=Math.cbrt(b);
      System.out.println("Cube root of "+b+" is "+cr2);
    }
  }
  public static void main(String args[])throws IOException
  {
    InputStreamReader read=new InputStreamReader(System.in);
    BufferedReader in=new BufferedReader(read);
    System.out.println("Hi,I am a calculator");
    System.out.println("As your real life calculator,I can do the same things");
    System.out.println("But I can only operate on 2 numbers, one at a time");
    System.out.println("And for every other operation,you have to exit and come back to the terminal window");
    System.out.println("But there are some other useful operations available here as well");
    System.out.println("So now,give me any 2 numbers on which you want me to operate");
    double n1=Double.parseDouble(in.readLine());
    double n2=Double.parseDouble(in.readLine());
    calculator ob=new calculator();
    System.out.println("What you want to do with these 2 numbers [give the number in the list below which correspons to that operation]");
    System.out.println("1=> Addition");
    System.out.println("2=> Subtraction");
    System.out.println("3=> Multiplication");
    System.out.println("4=> Division");
    System.out.println("5=> Find the remainder of their division");
    System.out.println("6=> Find the percentage of one to the other");
    System.out.println("7=> Find the power of the 1st number to the 2nd number");
    System.out.println("8=> Find the square root of anyone of the numbers");
    System.out.println("9=> Find the cube root of anyone of the numbers");
    int p=Integer.parseInt(in.readLine());
    if (p==1)
    {
      ob.add(n1,n2);
    }
    if (p==2)
    {
      ob.subtract(n1,n2);
    }
    if (p==3)
    {
      ob.multiply(n1,n2);
    }
    if (p==4)
    {
      ob.divide(n1,n2);
    }
    if (p==5)
    {
      ob.findremainder(n1,n2);
    }
    if (p==6)
    {
      ob.percent(n1,n2);
    }
    if (p==7)
    {
      ob.power(n1,n2);
    }
    if (p==8)
    {
      ob.squareroot(n1,n2);
    }
    if (p==9)
    {
      ob.cuberoot(n1,n2);
    }
    System.out.println("So I hope you got your answer and you are satisfied with it");
    System.out.println("Also, please give a feedback to my master");
    System.out.println("Your feedback could help me to improve more and more");
    System.out.println("And if I got time... I could conqueror the world");
    System.out.println("Anyways...");
    System.out.println("Thank you for accessing me and I hope you have a great day");
  }
}

But here's the problem. At the end, when the user gets the result, I want the program to ask the user if he/she wants to give 2 more numbers to operate on or leave the calculator. If user wants to perform more operations, the code will get repeated, or if the user wants to leave the program, the program will end. But I don't know how to do the repeating part. Is anyone has a solution for this??

Upvotes: 0

Views: 93

Answers (2)

Quadslab
Quadslab

Reputation: 294

To repeat the question, you can use do {...} while(...);. I also changed the if(p==...) blocks to use switch(...) { case(...): ... }, as it is a perfect use-case for it.
You can change your main(String args[]) to:

public static void main(String args[])throws IOException {
    InputStreamReader read=new InputStreamReader(System.in);
    BufferedReader in=new BufferedReader(read);
    System.out.println("Hi,I am a calculator");
    System.out.println("As your real life calculator,I can do the same things");
    System.out.println("But I can only operate on 2 numbers, one at a time");
    System.out.println("And for every other operation,you have to exit and come back to the terminal window");
    System.out.println("But there are some other useful operations available here as well");
    boolean shouldContinue;
    do {
        System.out.println("So now,give me any 2 numbers on which you want me to operate");
        double n1=Double.parseDouble(in.readLine());
        double n2=Double.parseDouble(in.readLine());
        calculator ob=new calculator();
        System.out.println("What you want to do with these 2 numbers [give the number in the list below which correspons to that operation]");
        System.out.println("1=> Addition");
        System.out.println("2=> Subtraction");
        System.out.println("3=> Multiplication");
        System.out.println("4=> Division");
        System.out.println("5=> Find the remainder of their division");
        System.out.println("6=> Find the percentage of one to the other");
        System.out.println("7=> Find the power of the 1st number to the 2nd number");
        System.out.println("8=> Find the square root of anyone of the numbers");
        System.out.println("9=> Find the cube root of anyone of the numbers");
        int p=Integer.parseInt(in.readLine());
        switch(p) {
            case 1:
                ob.add(n1,n2);
                break;
            case 2:
                ob.subtract(n1,n2);
                break;
            case 3:
                ob.multiply(n1,n2);
                break;
            case 4:
                ob.divide(n1,n2);
                break;
            case 5:
                ob.findremainder(n1,n2);
                break;
            case 6:
                ob.percent(n1,n2);
                break;
            case 7:
                ob.power(n1,n2);
                break;
            case 8:
                ob.squareroot(n1,n2);
                break;
            case 9:
                ob.cuberoot(n1,n2);
                break;
            default:
                System.out.println("Provided op-code not supported!");
        }
        System.out.println("Do you want to continue? (yes/no)");
        String choice = in.readLine();
        shouldContinue=choice.equalsIgnoreCase("yes");
    } while(shouldContinue);
    System.out.println("So I hope you got your answer and you are satisfied with it");
    System.out.println("Also, please give a feedback to my master");
    System.out.println("Your feedback could help me to improve more and more");
    System.out.println("And if I got time... I could conqueror the world");
    System.out.println("Anyways...");
    System.out.println("Thank you for accessing me and I hope you have a great day");
}

Upvotes: 1

Kaveri Mandlik
Kaveri Mandlik

Reputation: 44

You should use a do-while loop including a switch case.

For example,

int ch;
cout<<"\n********************MENU**************************";
cout<<"\n1.Addition";
cout<<"\n2.Subtraction";
cout<<"\n3.Exit";

do
{
    
    cout<<"\nEnter the choice: ";
    cin>>ch;
    
    switch(ch)
    {
        
        case 1: //call adition function here
            break;
            
        case 2: //call substraction function here
            break;
            
    }
}while(ch!=3);

This will keep running until the user enters choice as 3 i.e Exit in this case.

Upvotes: 0

Related Questions