if/else and if/elseif

后端 未结 10 1876
傲寒
傲寒 2020-12-17 01:00

If I have a statement block like this:

if (/*condition here*/){ }
else{ }

or like this:

if (/*condition here*/)
else if (/         


        
10条回答
  •  长情又很酷
    2020-12-17 01:35

    import java.util.*;
    
    public class JavaApplication21 {
        public static void main(String[] args) {
    
            Scanner obj = new Scanner(System.in);
            System.out.println("You are watching an example of if & else if statements");
    
            int choice, a, b, c, d;
            System.out.println(" Enter 1-Addition & 2-Substraction");
    
            int option = obj.nextInt();
            switch (option) {
                case (1):
                    System.out.println("how many numbers you want to add.... it can add up to 3 numbers only");
                    choice = obj.nextInt();
                    if (choice == 2) {
                        System.out.println("Enter 1st number");
                        a = obj.nextInt();
    
                        System.out.println("Enter 2nd number");
                        b = obj.nextInt();
    
                        c = a + b;
    
                        System.out.println("Answer of adding " + a + " & " + b + " is= " + c);
                    } else if (choice == 3) {
                        System.out.println("Enter 1st number");
                        a = obj.nextInt();
    
                        System.out.println("Enter 2nd number");
                        b = obj.nextInt();
    
                        System.out.println("Enter 3rd number");
                        c = obj.nextInt();
    
                        d = a + b + c;
    
                        System.out.println("Answer of adding " + a + " , " + b + " & " + c + "  is= " + d);
                    }
                case (2):
                    System.out.println("how many numbers you want to substract.... it can substract up to 3 numbers only");
                    choice = obj.nextInt();
                    if (choice == 2) {
                        System.out.println("Enter 1st number");
                        a = obj.nextInt();
    
                        System.out.println("Enter 2nd number");
                        b = obj.nextInt();
    
                        c = a - b;
                        System.out.println("Answer of substracting " + a + " & " + b + " is= " + c);
                    } else if (choice == 3) {
                        System.out.println("Enter 1st number");
                        a = obj.nextInt();
    
                        System.out.println("Enter 2nd number");
                        b = obj.nextInt();
    
                        System.out.println("Enter 3rd number");
                        c = obj.nextInt();
    
                        d = a - b - c;
                        System.out.println("Answer of substracting " + a + " , " + b + " & " + c + "  is= " + d);
                    }
                default:
                    System.out.println("no option you have chosen" + option);
            }
        }
    }
    

提交回复
热议问题