Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2

Thread: Looping

  1. #1
    New to the CF scene
    Join Date
    Sep 2010
    Thanked 0 Times in 0 Posts


    public static void main(String[] args){
    int oddNo;
    oddNo = getOdd();
    public static int getOdd(){
    int oddNo = 0;
    Scanner sc = new Scanner(System.in);

    while (oddNo % 2 == 0){
    System.out.print("Enter an odd number : ");
    oddNo = sc.nextInt();
    System.out.println("Wrong input ! ");
    if (oddNo % 2 !=0);
    System.out.print("The odd number is : " + oddNo);
    return oddNo;


    however, the output:
    Enter an odd number : 10
    Wrong input !
    Enter an odd number : 8
    Wrong input !
    Enter an odd number : 9
    Wrong input ! (how to remove this?)
    The odd number is : 9

  2. #2
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    In a place far, far away...
    Thanked 1,043 Times in 1,019 Posts
    First off, this is the Javascript forum NOT the Java forum. Second you mean remove the Wrong input ! part? Well your while loop is checking to see if the int you put in is even. If it is then take in another int and print out the message. Your loop will always print out that line only because it has to finish executing before it can move on. You can move that line above your scanner in call or check inside the loop to see if oddNo is odd or even using the same condition for your while loop.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts