jeudi 3 juin 2021

How should I write 'else' statement in this code if I want to terminate the code after user gives input as 'n/N'?

I am trying to perform arithmetic operations by taking numbers as user input,I want to run the code continuously till user says no. How should I write 'else' statement in this code if I want to terminate the code after user gives input as 'n/N'?


    import java.util.*;
    
    class FirstExample{
        static void arithmetic(){
            Scanner sc = new Scanner(System.in);
            
            while(true){
            System.out.println("\nDo u want to perform the arithmetic operations? (Y/N): ");
            String input = sc.nextLine();
            
            if(input=="y"||input=="Y")
            sc.nextLine();
            System.out.println("Enter first number: ");
            double first = sc.nextDouble();
            System.out.println("Enter second number: ");
            double second = sc.nextDouble();
            System.out.println("Addition of the two number is: "+(first+second));
            System.out.println("Subtraction of the two number is: "+(first-second));
            System.out.println("Multiplication of the two number is: "+(first*second));
            System.out.println("Division of the two number is: "+(first/second));
            sc.nextLine();
            
            }
        }       
        public static void main(String[]args){
            
            FirstExample.arithmetic();  
        }
    }
    ```
    
           //else{
          //    System.exit(0);
          //     }
         //tried adding this block of code but it gets terminated even after giving 'y/Y'
         

Aucun commentaire:

Enregistrer un commentaire