Results 1 to 4 of 4

Thread: Looping

  1. #1
    Dean29126 is offline Member
    Join Date
    Sep 2010
    Posts
    18
    Rep Power
    0

    Default Looping

    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
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    You'll want an if statement attached to this line:
    Java Code:
    System.out.println("Wrong input ! ");
    ...to verify whether or not the new input is odd/even.

    PS: Adding [code] tags around your code is helpful to those trying to read it.

  3. #3
    Dean29126 is offline Member
    Join Date
    Sep 2010
    Posts
    18
    Rep Power
    0

    Default

    i've got thanks and btw how i go about solving the following:
    Write the main() method to call getNum () in order to get a series of positive integers between 1 and 9999. It will stop calling when the user enters 9999. The program will compute and display the following:
    a) product of all the numbers,
    b) average of all the numbers,
    c) difference between the biggest and the smallest number

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,271
    Rep Power
    25

    Default

    You've got the code for reading in number from a user. Now you need to add some logic to the loop where you read these numbers to do the computations as per the program requirements.
    Start with a program with a loop that prompts for the next number and exits the loop when the number is the end number value.
    When that works, move to the next part of the problem.

Similar Threads

  1. Help with While and For Looping
    By gmoney8316 in forum New To Java
    Replies: 2
    Last Post: 03-03-2010, 10:54 PM
  2. looping and filtering
    By javafanatic in forum New To Java
    Replies: 14
    Last Post: 02-09-2010, 09:48 AM
  3. Looping Help Please
    By JonnySnip3r in forum New To Java
    Replies: 5
    Last Post: 01-31-2010, 05:57 AM
  4. looping problems
    By Blakester in forum New To Java
    Replies: 4
    Last Post: 10-05-2009, 08:26 PM
  5. Looping if statements
    By rice in forum New To Java
    Replies: 10
    Last Post: 10-02-2009, 02:50 AM

Posting Permissions

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