Results 1 to 4 of 4
  1. #1
    dksellou is offline Member
    Join Date
    Sep 2012
    Posts
    13
    Rep Power
    0

    Default how to ask the user to enter a sequence of numbers one at a time

    Hi.
    I have created this little program. It is the solution to exercise 6.17 of Deitel "how to program Java". The program asks the user to add a sequence of numbers and the program tests if it is odd or even...here is what I did. However, I don't know how to ask the user to enter more than one integers (one at a time) before I show the results:

    //Exercise 6.17 Solution : EvenOdd.java

    import java.util.Scanner;

    public class EvenOdd
    {
    public void evenOrOdd()
    {
    Scanner input = new Scanner( System.in );

    System.out.printf("%s\n%s\n",
    "Please enter an integer and I will tell you if it is even or odd: ",
    "Enter 0 to exit: ");

    int number= input.nextInt();

    while (number !=0)
    {
    if ( isEven( number ) )
    System.out.printf( "%d is even\n", number );
    else
    System.out.printf( "%d is odd\n", number );

    number= input.nextInt();
    } // end while loop
    } // end method evenOrOdd

    // return true if number is even
    public boolean isEven( int number )
    {
    return number % 2 == 0;
    } // end method isEven
    }//end of class EvenOdd



    This program output for example is:

    enter an integer: 4
    4 is even
    enter an integer: 5
    5 is odd.


    how can you program it showing the following:

    enter an integer: 4
    enter an integer: 5
    4 is even
    5 is odd.

  2. #2
    thinkinjava24 is offline Member
    Join Date
    Sep 2012
    Posts
    17
    Rep Power
    0

    Default Re: how to ask the user to enter a sequence of numbers one at a time

    import java.util.Scanner;

    public class EvenOdd
    {
    public static final int NUMBER = 5;
    static int[] array = new int[NUMBER];
    public static void main(String[] args) {
    Scanner input = new Scanner( System.in );

    System.out.printf("%s\n%s\n",
    "Please enter an integer and I will tell you if it is even or odd: ",
    "Enter 0 to exit: ");
    for (int i = 0; i < NUMBER; i++) {
    System.out.println("Please enter number " + (i+1) + " : ");
    array[i]= input.nextInt();
    }

    for (int i = 0; i < NUMBER; i++) {
    isEven( array[i] );
    }
    }

    public static void isEven( int number )
    {
    if(number % 2 == 0)
    System.out.printf( "%d is even\n", number );
    else System.out.printf( "%d is odd\n", number );
    }
    }

    this is the code i write. one thing you need to know is that, you have to initialize the number of the integers you want to enter to the system, like the definition of NUMBER which is static in my code. otherwise, it is impossible to realize what you want, personally opinion.
    hope it helps.

  3. #3
    dksellou is offline Member
    Join Date
    Sep 2012
    Posts
    13
    Rep Power
    0

    Default Re: how to ask the user to enter a sequence of numbers one at a time

    my original post was so clear ...
    I also gave an example.

    P.S. I am not in Arrays yet....

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: how to ask the user to enter a sequence of numbers one at a time

    Please wrap your code in the code tags.

    thinkinjava24's post is what is called spoon-feeding - evidently thinkinjava24 does not know what this is, but for his benefit I'd recommend reading The Problem with Spoon-feeding.

    Back to the problem at hand

    how can you program it showing the following:
    Write out how you would do this on paper - do you know before hand how many integers a user want's to enter? If no, then you must prompt the user to enter this value if you wish for that output.

Similar Threads

  1. How do i allow user to enter ammount?
    By Diehardwalnut in forum New To Java
    Replies: 3
    Last Post: 09-27-2012, 07:54 AM
  2. Replies: 5
    Last Post: 02-29-2012, 11:07 AM
  3. How to ERROR if user presses Enter?
    By net2chris in forum New To Java
    Replies: 3
    Last Post: 10-03-2011, 06:30 PM
  4. Replies: 11
    Last Post: 01-14-2011, 07:36 PM

Posting Permissions

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