Results 1 to 13 of 13
  1. #1
    shroomiin is offline Member
    Join Date
    Sep 2009
    Posts
    31
    Rep Power
    0

    Default printing number of pos/neg numbers input?

    i need to write a code that when you input 5 numbers, it displays how many pos, neg and 0s were entered. i thought i was pretty close...but as you can see my output is not right. can someone please point me in the right direction from herE? thank you.


    Java Code:
    import java.util.Scanner;
    public class negposzerovals {
    
    	public static void main(String[] args) {
    		
    	
    int n1;
    int n2;
    int n3;
    int n4;
    int n5;
    int posnum;
    int negnum;
    int zeros;
    
    posnum = 0;
    negnum = 0;
    zeros = 0;
    		
    		Scanner input = new Scanner (System.in);
    		
    		System.out.print("Please enter first number:");
    		n1 = input.nextInt();
    		if (n1 > 0);
    		posnum++;
    		if (n1==0);
    		zeros++;
    		if (n1 < 0);
    		negnum++;
    		
    		System.out.print("Please enter second number:");
    		n2 = input.nextInt();
    		if (n2 > 0);
    		posnum++;
    		if (n2==0);
    		zeros++;
    		if (n2 < 0);
    		negnum++;
    		
    		System.out.print("Please enter third number:");
    		n3 = input.nextInt();
    		if (n3 > 0);
    		posnum++;
    		if (n3==0);
    		zeros++;
    		if (n3 < 0);
    		negnum++;
    		
    		System.out.print("Please enter fourth number:");
    		n4 = input.nextInt();
    		if (n4 > 0);
    		posnum++;
    		if (n4==0);
    		zeros++;
    		if (n4 < 0);
    		negnum++;
    		
    		System.out.print("Please enter fifth number:");
    		n5 = input.nextInt();
    		if (n5 > 0);
    		posnum++;
    		if (n5==0);
    		zeros++;
    		if (n5 < 0);
    		negnum++;
    		
    		
    		
    		System.out.print("There were " + posnum + " positive numbers entered, " + negnum + "negative numbers entered, and " + zeros + "zeros entered.");
    		
    		
    		
    
    	}
    
    }
    Last edited by shroomiin; 09-25-2009 at 01:03 AM.

  2. #2
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    6

    Default

    Use a counter? Just create a variable that is visible in the class itself, name it count or so, and set it's initial value to 0. Every time a number is inputted, increase the counter by one...

    I don't really think this would solve your problem, and if it won't, you should rephrase your question, clearly.

    ~Matt
    I die a little on the inside...
    Every time I get shot.

  3. #3
    shroomiin is offline Member
    Join Date
    Sep 2009
    Posts
    31
    Rep Power
    0

    Default

    yes sorry for the confusion. i am giving it a go right now and i have gotten it started but i know what i have right now will not work in the end. heres what i have:

    import java.util.Scanner;
    public class negposzerovals {

    public static void main(String[] args) {


    int n1;
    int n2;
    int n3;
    int n4;
    int n5;
    int posnum;
    int negnum;
    int zeros;

    Scanner input = new Scanner (System.in);

    System.out.print("Please enter first number:");
    n1 = input.nextInt();
    if (n1 > 0);
    posnum = 1;
    if (n1 < 0);
    negnum = 1;

    System.out.print("There were " + posnum + " positive numbers entered");




    }

    }



    obviously i am still working on it and i would add more to the end, but as far as my variables and my math in the code go i think i need a little guidance. how would you go about fixing this?

  4. #4
    Mr.Beans's Avatar
    Mr.Beans is offline Senior Member
    Join Date
    Apr 2009
    Location
    Virginia Beach, Virginia
    Posts
    149
    Rep Power
    0

    Default

    Maybe look into using a for statement.

    That way you can have one set of commands repeated x amount of times.
    That would be the best way to get five inputs.

    Also you forget to check if a number is a zero.


    Maybe try drawing a flow chart of how the program should run, then try to implement it.

    Note: Also, use the code tags next time you post your source code.

  5. #5
    shroomiin is offline Member
    Join Date
    Sep 2009
    Posts
    31
    Rep Power
    0

    Default

    i am only allowed to use if or while for this assignment, unfortunately.

  6. #6
    shroomiin is offline Member
    Join Date
    Sep 2009
    Posts
    31
    Rep Power
    0

    Default

    ALRIGHT. i thought i was going well until the code didnt work. this is what i have now. i think that i am close. any suggestions pleasE?
    Java Code:
    import java.util.Scanner;
    public class negposzerovals {
    
    	public static void main(String[] args) {
    		
    	
    int n1;
    int n2;
    int n3;
    int n4;
    int n5;
    int posnum;
    int negnum;
    int zeros;
    
    posnum = 0;
    negnum = 0;
    zeros = 0;
    		
    		Scanner input = new Scanner (System.in);
    		
    		System.out.print("Please enter first number:");
    		n1 = input.nextInt();
    		if (n1 > 0);
    		posnum++;
    		if (n1==0);
    		zeros++;
    		if (n1 < 0);
    		negnum++;
    		
    		System.out.print("Please enter second number:");
    		n2 = input.nextInt();
    		if (n2 > 0);
    		posnum++;
    		if (n2==0);
    		zeros++;
    		if (n2 < 0);
    		negnum++;
    		
    		System.out.print("Please enter third number:");
    		n3 = input.nextInt();
    		if (n3 > 0);
    		posnum++;
    		if (n3==0);
    		zeros++;
    		if (n3 < 0);
    		negnum++;
    		
    		System.out.print("Please enter fourth number:");
    		n4 = input.nextInt();
    		if (n4 > 0);
    		posnum++;
    		if (n4==0);
    		zeros++;
    		if (n4 < 0);
    		negnum++;
    		
    		System.out.print("Please enter fifth number:");
    		n5 = input.nextInt();
    		if (n5 > 0);
    		posnum++;
    		if (n5==0);
    		zeros++;
    		if (n5 < 0);
    		negnum++;
    		
    		
    		
    		System.out.print("There were " + posnum + " positive numbers entered, " + negnum + "negative numbers entered, and " + zeros + "zeros entered.");
    		
    		
    		
    
    	}
    
    }

  7. #7
    shroomiin is offline Member
    Join Date
    Sep 2009
    Posts
    31
    Rep Power
    0

    Default

    edited 1st post...can someone please assist?

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  9. #9
    shroomiin is offline Member
    Join Date
    Sep 2009
    Posts
    31
    Rep Power
    0

    Default

    Well requirement was to create the program using only the topics we covered in the first chapter. in the first chapter it just covered the basics such as printing text and if statements or while statements.

    the problem is: Write a program that inputs five numbers and determines and prints the number of positive, negative, and zero's entered. and then prints them.

    i thought i was doing pretty good with it until the output said that all 3 values were 5...so i am somwhat lost on what to do about that. thanks again. i appreciate it.

  10. #10
    Mr.Beans's Avatar
    Mr.Beans is offline Senior Member
    Join Date
    Apr 2009
    Location
    Virginia Beach, Virginia
    Posts
    149
    Rep Power
    0

    Default

    If you can use a while, then use a while statement. You can make it do the same thing as a for statement

    Java Code:
    int counter = 0;
    while(counter < 5)
    {
         System.out.println(counter);
         counter++;
    }

  11. #11
    rdtindsm is offline Member
    Join Date
    Feb 2009
    Posts
    92
    Rep Power
    0

    Default

    Look at the syntax for "if" again. Your if statements don't do anything except make the comparison.

    This is one of those problems that you can stare at till you're blind and not find the problem because you know what you mean and it seems so simple.

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by rdtindsm View Post
    Look at the syntax for "if" again. Your if statements don't do anything except make the comparison.

    This is one of those problems that you can stare at till you're blind and not find the problem because you know what you mean and it seems so simple.
    Good pick-up. I think we'll just need to tell the OP.

    To the original poster, the semicolon at the end of your if statement is short-circuiting it. It shouldn't be:

    Java Code:
    if (blah);  // this semicolon is messing things up
      myVar++;
    but rather,

    Java Code:
    if (blah)  // semicolon is now gone.
      myVar++;
    even better is to use curly braces:

    Java Code:
    if (blah)  {
      myVar++;
    }

  13. #13
    shroomiin is offline Member
    Join Date
    Sep 2009
    Posts
    31
    Rep Power
    0

    Default

    thanks so much everybody. after fixing my if syntax it works like a charm surprisingly.

Similar Threads

  1. Log4j not printing out file and line number
    By devin in forum Java Servlet
    Replies: 2
    Last Post: 03-05-2009, 03:16 PM
  2. Printing ranges of numbers using if/else statements
    By russbuss106 in forum New To Java
    Replies: 8
    Last Post: 03-05-2009, 05:03 AM
  3. printing two smallest numbers from a series of numbers
    By trofyscarz in forum New To Java
    Replies: 2
    Last Post: 10-14-2008, 11:46 PM
  4. trying to add up random numbers into one number
    By pjr5043 in forum New To Java
    Replies: 4
    Last Post: 09-15-2008, 02:20 PM
  5. Printing Fibonacci Numbers
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-09-2008, 06:43 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
  •