-2
import java.util.Scanner;

public class ADVclass 
{

    public static void main(String[] args) 
    {
        char function;
        double num1,num2;
        
        
        Scanner input = new Scanner(System.in);
        
     
         while (true)
         {
             
            System.out.println("Enter the First number");
             num1=input.nextDouble();
             num2=input.nextDouble();
             System.out.println("Enter the function");
            function = input.next().charAt(0);
             
           if(function == '+') 
            System.out.println(num1+num2);
           else if (function =='-')
               System.out.println(num1-num2);
           else if(function =='*')
               System.out.println(num1*num2);
           else if(function =='/')
           {
               if (num2!=0)
                   System.out.println(num1/num2);
               else
                   System.out.println("Invalide");
           }
           
           
         }
    }

}

i want to add another number to the result generated

example: if i add 1+1=2 i want to add this two to another number and add a clear statement at the end

deepakchethan
  • 5,240
  • 1
  • 23
  • 33
  • What do you mean by "to another number and add a clear statement at the end"? – Spectric Jun 14 '21 at 01:08
  • when we provide a input in calculator as 1+ 1 we get 2 and them if we want to add another number such as 4 we will get 6 in the same way i want to perform a operation and later at the end want to clear the results such that thay wont add to the next coming numbers – user16218147 Jun 14 '21 at 12:59

1 Answers1

0

In order to perform any further operations, you need to store the operation result into some variable and later you can do any operation you want. I have taken a result variable of type double, if you want results of all operations to be saved after while loop you can take a array of double. For console cleaning in java, you can take a look Java clear console

import java.util.Scanner;

public class ADVclass {

public static void main(String[] args) 
{
    char function;
    double num1,num2,result;
    
    
    Scanner input = new Scanner(System.in);
    
 
     while (true)
     {
         
        System.out.println("Enter the First number");
         num1=input.nextDouble();
         num2=input.nextDouble();
         System.out.println("Enter the function");
        function = input.next().charAt(0);
         
       if(function == '+') {
        result = num1 + num2;
       else if (function =='-')
        result = num1 - num2;
       else if(function =='*')
        result = num1 * num2;
       else if(function =='/')
       {
           if (num2!=0)
              result = num1/num2;
           else
               System.out.println("Invalide");
       }
       
       
     }
     System.out.println(result+10);//Can do any operation adding 10 as you said you have to add something to the result.
}

}
Ashish Mishra
  • 704
  • 1
  • 6
  • 20
  • I did make some changes to the code ,but still was not able to perform the operation where user can add another number to the result generated. such as if user provides 1 and 1 as numbers and operation is addition he gets 2 and later when wants to add another number as 4 it should show me 6 as total – user16218147 Jun 14 '21 at 12:54
  • @user16218147 You need to define the problem statement properly, May be could provide some test cases then only I can help you – Ashish Mishra Jun 15 '21 at 03:02