13 Replies - 2074 Views - Last Post: 10 September 2011 - 01:51 PM Rate Topic: -----

#1 kikiskia  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 10-September 11

using percent mod to count number of even number of user input

Posted 10 September 2011 - 11:58 AM

ok so I need to use the percent mod operator or the remainder operator to calculate the number of integers total, and the number of even numbers..All this will be done by a set of numbers that the user will input.... so far I got this



package cis2168lab1p1;

import java.util.Scanner;

public class CIS2168Lab1P1 {

   
    public static void main(String[] args) {

        Scanner keyboard = new Scanner(System.in);
       
        int sum;
      
        System.out.println("Please enter your numbers:");
       
        int userInput= keyboard.nextInt();
      
     sum=0;
     
     int numberOfTotal=userInput;
     
     while (userInput >=0)
         
     {
     sum=sum + userInput;
     numberOfTotal++;
     userInput= keyboard.nextInt();
     
     
   if (userInput%2, count<=0, count++)
     
     
     }
        
    }

}


This is my first time programming and I really do not know that much. I need people to be specific like show me examples...Thanks...

Is This A Good Question/Topic? 0
  • +

Replies To: using percent mod to count number of even number of user input

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: using percent mod to count number of even number of user input

Posted 10 September 2011 - 12:02 PM

This if has an invalid syntax

if (userInput%2, count<=0, count++)

what are you trying to do ?

if(userInpout % 2 == 0) then something ?
Was This Post Helpful? 0
  • +
  • -

#3 kikiskia  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 10-September 11

Re: using percent mod to count number of even number of user input

Posted 10 September 2011 - 12:39 PM

this is my new code..I can not figure out how to do the total number...meaning calculating how many numbers the user inputs..also something is wring with calculting my even total...

package cis2168lab1p1;

import java.util.Scanner;
	 
public class CIS2168LP1
	{
    
    public static void main(String[] args)
	    {
	      int userInputNumber;
	         int userInput = 0;
	         int evenNumbers = 0;
	          
	       Scanner keyboard = new Scanner(System.in);
              
 
	int [] numbers = new int [userInput];
        	    
	        System.out.println("Enter the numbers you want to count:   ");
	        userInput = keyboard.nextInt();
	     
	        
	        for (int i = 0; i < userInput; i++ )
	    
	        {
	               int random = keyboard.nextInt();
	                if (random % 2 == 0)
	    
	                {
	                        evenNumbers++;
                                userInputNumber++;
	                }
	        }     
	 
	System.out.println("Total even numbers " + evenNumbers);
	 System.out.println("Total Numbers used" +  userInputNumber);
  
	    
	    }
	}








Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: using percent mod to count number of even number of user input

Posted 10 September 2011 - 12:50 PM

Should be

     userInput = keyboard.nextInt();
     int [] numbers = new int [userInput];


You have to read userInput before creating the array based on its value

And total numbers used is simply userInput :)
Was This Post Helpful? 0
  • +
  • -

#5 kikiskia  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 10-September 11

Re: using percent mod to count number of even number of user input

Posted 10 September 2011 - 12:54 PM

ok this is the new one sorry about the other post......ummmm it is not counting the correct number of the user input...i need the total of the even and the unused ones...





package cis2168lab1p1;

import java.util.Scanner;
	 
public class CIS2168LP1
	{
    
    public static void main(String[] args)
	    {
	     
	         int userInput = 0;
	         int evenNumbers = 0;
	          
	       Scanner keyboard = new Scanner(System.in);
              
 
	
        	    
	        System.out.println("Enter the numbers you want to count:   ");
                
               
	    int [] numbers = new int [userInput];
	        userInput = keyboard.nextInt();
	     
	        
	        for (int i = 0; i < userInput; i++ )
	    
	        {
	               int random = keyboard.nextInt();
	                if (random % 2 == 0)
	    
	                {
	                        evenNumbers++;
                                
	                }
	        }     
	 
	System.out.println("Total even numbers " + evenNumbers);
	System.out.println("Total Numbers used" +  userInput);
  
	    
	    }
	}





This post has been edited by kikiskia: 10 September 2011 - 12:57 PM

Was This Post Helpful? 0
  • +
  • -

#6 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2203
  • View blog
  • Posts: 5,235
  • Joined: 10-September 10

Re: using percent mod to count number of even number of user input

Posted 10 September 2011 - 01:03 PM

Where is your counter for the number of entries made by the user? I see one counter for evenNumbers, and another variable for the value entered by the user. Where's the other counter?
Was This Post Helpful? 0
  • +
  • -

#7 kikiskia  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 10-September 11

Re: using percent mod to count number of even number of user input

Posted 10 September 2011 - 01:07 PM

View PostGregBrannon, on 10 September 2011 - 01:03 PM, said:

Where is your counter for the number of entries made by the user? I see one counter for evenNumbers, and another variable for the value entered by the user. Where's the other counter?




wouldnt it be the userInput??? or do I have to create another counter...Should I put it after the for loop???? ugh this is soo hard..
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: using percent mod to count number of even number of user input

Posted 10 September 2011 - 01:18 PM

Actually you do not even need the array you created

	public static void main(String[] args)
	{
		int evenNumbers = 0;

		Scanner keyboard = new Scanner(System.in);
		System.out.println("Enter the numbers you want to count:   ");

		int userInput = keyboard.nextInt();


		for (int i = 0; i < userInput; i++ )
		{
			if(keyboard.nextInt() %2 == 0)
			{
				evenNumbers++;
			}
		}     

		System.out.println("Total Numbers used" +  userInput);
		System.out.println("Total even numbers " + evenNumbers);
		System.out.println("Total odd numbers  " + (userInput - evenNumbers));
	}


Was This Post Helpful? 0
  • +
  • -

#9 kikiskia  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 10-September 11

Re: using percent mod to count number of even number of user input

Posted 10 September 2011 - 01:30 PM

ok so I did your suggestion but the code wont run..like it says running and nothing appears to be happening after I enter the random integers....what should I do??? also I decided to add the input with the even integers to get me the total..because the question wasnt asked how many odd..I dont want any points to be deducted,,haha



package cis2168lab1p1;

import java.util.Scanner;
	 
public class CIS2168LP1
	{
    
    public static void main(String[] args)	{
	    int evenNumbers = 0;
	 
	    Scanner keyboard = new Scanner(System.in);
	    System.out.println("Enter the numbers you want to count:   ");
	 
	    int userInput = keyboard.nextInt();
	 
	 
	    for (int i = 0; i < userInput; i++ )
	    {
	        if(keyboard.nextInt() %2 == 0)
	        {
	            evenNumbers++;
	        }
	    }    
	 
	    
	    System.out.println("Total even numbers " + evenNumbers);
	    System.out.println("Total odd numbers  " + (userInput + evenNumbers));
	}}



Was This Post Helpful? 0
  • +
  • -

#10 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: using percent mod to count number of even number of user input

Posted 10 September 2011 - 01:34 PM

Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the numbers you want to count:   ");
int userInput = keyboard.nextInt();
for (int i = 0; i < userInput; i++ )
{
    if(keyboard.nextInt() %2 == 0)
    {
        evenNumbers++;
    }
} 

When you do this, you only prompt for one count, the initial count. Inside of the for loop before the IF statement, you need to add another prompt asking for another number.
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: using percent mod to count number of even number of user input

Posted 10 September 2011 - 01:39 PM

View PostFuzzyness, on 10 September 2011 - 04:34 PM, said:

Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the numbers you want to count:   ");
int userInput = keyboard.nextInt();
for (int i = 0; i < userInput; i++ )
{
    if(keyboard.nextInt() %2 == 0)
    {
        evenNumbers++;
    }
} 

When you do this, you only prompt for one count, the initial count. Inside of the for loop before the IF statement, you need to add another prompt asking for another number.


Not really... this code works pefectly for me :)
Actually should be: "Enter the number of numbers you want to count"
Was This Post Helpful? 0
  • +
  • -

#12 kikiskia  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 10-September 11

Re: using percent mod to count number of even number of user input

Posted 10 September 2011 - 01:44 PM

ok should I do something like

package cis2168lab1p1;

import java.util.Scanner;
	 
public class CIS2168LP1
	{
    
    public static void main(String[] args)	{
	    int evenNumbers = 0;
	    int userInput=0;
	    Scanner keyboard = new Scanner(System.in);
	    System.out.println("Please enter integers   ");
	 
	    userInput = keyboard.nextInt();
	 
	 
	    for (int i = 0; i < userInput; i++ )
	    {
	        if(keyboard.nextInt() %2 == 0)
	        {
	            evenNumbers++;  
                    userInput++;
	        }
	    }    
	   
	    //System.out.println("Total even numbers " + evenNumbers);
	    System.out.println("Total odd numbers  " + (userInput + evenNumbers));
	}}




Was This Post Helpful? 0
  • +
  • -

#13 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2203
  • View blog
  • Posts: 5,235
  • Joined: 10-September 10

Re: using percent mod to count number of even number of user input

Posted 10 September 2011 - 01:45 PM

pbl's version didn't work for me, but changing the prompt helps. Here's another approach, very close to what you had but with minor mods:

add a counter for the number of numbers entered,
instruct the user to enter '0' to end,
use a while loop that terminates the loop with '0',
count total numbers entered in the while loop,
get userInput at end of while loop, and
add a space before printing totalNumbers.

import java.util.Scanner;

public class EvenNumberCounter
{
    public static void main(String[] args)
    {
        int evenNumbers = 0;
        int totalNumbers = 0;
        int userInput = 0;
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter the numbers you want to count (0 to end):");

        userInput = keyboard.nextInt();
        
        while ( userInput > 0 )
        {
            totalNumbers++;
            if( userInput %2 == 0)
            {
                evenNumbers++;
            }
            userInput = keyboard.nextInt();
        }     
        System.out.println("Total Numbers used " +  totalNumbers );
        System.out.println("Total even numbers " + evenNumbers );
        System.out.println("Total odd numbers  " + ( totalNumbers - evenNumbers ) );
    }
}

Was This Post Helpful? 1
  • +
  • -

#14 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: using percent mod to count number of even number of user input

Posted 10 September 2011 - 01:51 PM

Use the exact code that is in Fuzzyness reply #10
You can enter each number on different lines or multiple numbers per line as you wish

5 123 456 22 34 12

or

5
12
123
456
567
2

or

5
12 34
356 786
23
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1