mercredi 4 septembre 2019

how to end a while loop with a certain variable

I am a beginner in Java making an oddoreven program with a while loop. I am trying to figure out how to end the while loop with a certain number. Right now I have 1 to continue the loop, and trying to make 2 the number that terminates it. Also trying to figure out how to terminate the program if a user types anything but a number like a letter/words.

package oddoreven; import java.util.Scanner;

public class oddoreven { public static void main (String[] args){

    int num;
    int x = 1;


        while(x == 1) {

    System.out.println("Enter a number to check whether or not it is odd or even");
    Scanner s = new Scanner(System.in);
    num = s.nextInt();

    if (num % 2 == 0)
        System.out.println("The number is even");

    else 
        System.out.println("The number is odd");

    //trying to figure out how to get the code to terminate if you put in a value that isn't a number
        System.out.println("Type 1 to continue, 0 to terminate");
    x = s.nextInt();

        }

}

}

Aucun commentaire:

Enregistrer un commentaire