Positive and Negative integer counter

S

StaR.BoY2005

Hello everyone,

I am working on a program that needs to read an unspecified number of
integers and determine how many positive and negative values there are.
Also, the program needs to end when the input is 0 (zero). Here is the
code I have which is causing me problems!!! I would greatly appreciate
all of the help!

import java.util.Scanner;
public class Counter
{


public static void main(String[] args)
{
int inputNumber;
int count;
int number;

Scanner keyboard = new Scanner(System.in);
count = 0;
number = 0;

System.out.print("Enter your first integer: ");
inputNumber = keyboard.nextInt();

while (inputNumber >0);
{
count++;
System.out.print("Enter your next integer, or 0 to end: ");
inputNumber = keyboard.nextInt();
}

while (inputNumber <0);
{
number++;
System.out.print("Enter your integer, or 0 to end:");
inputNumber = keyboard.nextInt();
}

if (count == 0)
{
System.out.print("You didn't enter any data!");
}
else
{
keyboard.nextInt();
System.out.print("You entered " + count + " positive
integers.");
System.out.print("You entered " + number + " negative
integers.");
}

}

}

Thanks again.

--John
 
P

PofN

Hello everyone,

I am working on a program that needs to read an unspecified number of
integers and determine how many positive and negative values there are.
Also, the program needs to end when the input is 0 (zero). Here is the
code I have which is causing me problems!!! I would greatly appreciate
all of the help!

Take your homework, shove it and piss off.
 
M

Manish Pandit

You need to :

1. Put all this in 1 while loop that prompts the user for input
2. Put a break condition in your while loops
3. Bulletproof your code so that if non-numeric data is entered, the
code handles it gracefully.

-cheers,
Manish
 
P

Patricia Shanahan

Hello everyone,

I am working on a program that needs to read an unspecified number of
integers and determine how many positive and negative values there are.
Also, the program needs to end when the input is 0 (zero). Here is the
code I have which is causing me problems!!! I would greatly appreciate
all of the help!

For this type of thing, I would try play-the-computer.

Write yourself some test data. Write the name of each variable on a
whiteboard, or a large piece of paper with room to writing in and cross
off multiple values for each variable.

Do what your program tells the computer to do, keeping a note of the
current value of each variable. Are the results what you want? If not,
what would you really do, if you were trying to do this task?

Patricia
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top