How to insist that a users input is an int?

前端 未结 7 1972
日久生厌
日久生厌 2020-12-10 10:09

Basic problem here.. I will start off by asking that you please not respond with any code, as that likely will only confuse me further (programming noob). I am looking for

7条回答
  •  有刺的猬
    2020-12-10 10:43

    public class Sample {

    /**
     * author CLRZ
     */
    public static void main(String[] args) {
        int a; // variable
        Scanner in = new Scanner(System.in); // scans your input
        System.out.println("Enter your number's choice:"); 
        int sem1 = in.nextInt(); // reads next integer
        if (sem1 == 1) // conditioned if your choice number is equal to 1
        System.out.println("Hello World1"); // output wil be Hello World
        int b;
    
        System.out.println("Enter your number's choice:");
        int sem2 = in.nextInt(); 
        if (sem2 == 2)
        System.out.println("Hello World2");
        int c;
    
        System.out.println("Enter your number's choice:");
        int sem3 = in.nextInt(); 
        if (sem3 == 3)
        System.out.println("Hello World3");
    }
    

    }

提交回复
热议问题