Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using percent Modulus to calculate total number of even numbers from user input

 
sandy skiathitis
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok my assignment for class is to use the percent mod to calculate the total number of all the numbers and the total number of even numbers only. This should be done by using the user's input of their number.
the % "mod" operator
Input:
The first line of input will be a random list of integers the user will put it.
Output:
Return the total integers in the list and count of even integers.
Sample:

Input 
 Output
3 4 10 12 1 0 6 78 7 #total: 9
#even: 6
...............................................................
this is what I got so far


 
sandy skiathitis
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sandy skiathitis wrote:Ok my assignment for class is to use the percent mod to calculate the total number of all the numbers and the total number of even numbers only. This should be done by using the user's input of their number.
the % "mod" operator
Input:
The first line of input will be a random list of integers the user will put it.
Output:
Return the total integers in the list and count of even integers.
Sample:

Input 
 Output
3 4 10 12 1 0 6 78 7 #total: 9
#even: 6
...............................................................
this is what I got so far




so I redid it ummmmm here is the second version...please help i am getting more lost...


 
Stephan van Hulst
Bartender
Pie
Posts: 5790
61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your for loop appears broken. Take a step back and write down in plain English what the program should do:

1) Read one line from user input
2) While the line has another integer:
2.1) Increase total count of integers
2.2) If the integer is even:
2.2.1) Increase total count of even integers
3) Print total count of integers and total count of even integers

Now solve each step separately. Test your program in between solving steps, and make sure it does what you expect it to do. For instance, print the line after step 1) to make sure it's the same as you've entered.

You seem to have trouble with step 2). Look in the Scanner class for a very useful method.
 
sandy skiathitis
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Your for loop appears broken. Take a step back and write down in plain English what the program should do:

1) Read one line from user input
2) While the line has another integer:
2.1) Increase total count of integers
2.2) If the integer is even:
2.2.1) Increase total count of even integers
3) Print total count of integers and total count of even integers

Now solve each step separately. Test your program in between solving steps, and make sure it does what you expect it to do. For instance, print the line after step 1) to make sure it's the same as you've entered.

You seem to have trouble with step 2). Look in the Scanner class for a very useful method.



so i would have to use a while statement then an if statement but how to I implement the remainder or percent modulud operator? also would i have to do something specefically so that the program will be able to read more than one input the user puts in??? sorry this is my first programming class and first project of the semester...
 
Stephan van Hulst
Bartender
Pie
Posts: 5790
61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a skeleton:
 
sandy skiathitis
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Here's a skeleton:


I am so confused....the outline helped but I just dont know enough computer language to put it together... I am on the right track? but I have to use percent mod..or the remainder of mod...

 
sandy skiathitis
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sandy skiathitis wrote:
Stephan van Hulst wrote:Here's a skeleton:


I am so confused....the outline helped but I just dont know enough computer language to put it together... I am on the right track? but I have to use percent mod..or the remainder of mod...



this is what I got..I am soooo close...how can I do the total number count...I got lost..can you help???
 
Stephan van Hulst
Bartender
Pie
Posts: 5790
61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sandy, Scanner has a very useful method called hasNextInt().

Here's an almost complete solution:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic