Results 1 to 4 of 4
- 10-01-2012, 06:44 PM #1
Member
- Join Date
- Sep 2012
- Posts
- 13
- Rep Power
- 0
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.
- 10-01-2012, 09:01 PM #2
Member
- Join Date
- Sep 2012
- Posts
- 17
- Rep Power
- 0
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.
- 10-02-2012, 12:53 AM #3
Member
- Join Date
- Sep 2012
- Posts
- 13
- Rep Power
- 0
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....
- 10-02-2012, 06:07 AM #4
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,605
- Rep Power
- 5
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
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.how can you program it showing the following:
Similar Threads
-
How do i allow user to enter ammount?
By Diehardwalnut in forum New To JavaReplies: 3Last Post: 09-27-2012, 06:54 AM -
enter the time zone offset to GMT and convert to current time
By Bonia in forum New To JavaReplies: 5Last Post: 02-29-2012, 10:07 AM -
How to ERROR if user presses Enter?
By net2chris in forum New To JavaReplies: 3Last Post: 10-03-2011, 05:30 PM -
Problem getting numbers from user and finding smallest two numbers
By radhi16 in forum New To JavaReplies: 11Last Post: 01-14-2011, 06:36 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks