Results 1 to 16 of 16
  1. #1
    pvictory1 is offline Member
    Join Date
    Oct 2010
    Posts
    7
    Rep Power
    0

    Question Sum positive numbers using 10 inputs from user

    Hi

    I'm a little confused about this, I can't seem to get this to work corretly. What I'm trying to accomplish is I'm taking ten positive double number inputs and only printing out the positive ones plus the total. The problem is I don't know how to only have it except 10 inputs and also it's not totaling the inputs correctlly. Please help. Thank you

    Java Code:
    import java.util.*; 
    
    public class Sum 
    { 
    static Scanner console = new Scanner(System.in); 
    static final int N = 10; 
    
    public static void main(String[]args) 
    { 
    double number; 
    int total = 0; 
    
    System.out.println("Please enter 10 numbers, \n"
    + "adds the positive numbers, and displays the totals."); 
    
    System.out.print("Please enter " + N + " numbers; seperate each with a space: "); 
    
    for (int w = 1; w <= N; w++) 
    { 
    number = console.nextDouble(); 
    
    if (number > 0) 
    
    total += number;w++; 
    {
    continue; 
    }  
    }
    System.out.println("The total of the positive numbers is: " + total); 
    
    }
    }
    Last edited by Fubarable; 10-09-2010 at 06:24 PM. Reason: Moderator Edit: Code tags added -- but the code wasn't formatted to begin with -- D'oh!

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    786
    Rep Power
    5

    Default

    you raise "w" twice. Remove the w++ after total += number;!
    Your for-loop raise the w automatically after each iteration (see: The for Statement (The Java™ Tutorials > Learning the Java Language > Language Basics))
    So you can input 10 double values now.
    The second problem (" it's not totaling the inputs correctlly.") is, that you declare total as an Integer. Use double total = 0; instead of int total = 0;
    Java Code:
    		for (int w = 0; w < N; w++) {
    			number = console.nextDouble();
    			if (number > 0)
    				total += number;
    		}

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

    Default

    Hello, and welcome to the forum. I hope you don't mind that I edited your code and added code tags which should help make your posted code retain its formatting and be more readable.

    To do this yourself, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag [cod&#101;] above your pasted code and the tag [/cod&#101;] below your pasted code like so:

    Java Code:
    [cod&#101;]
      // your code goes here
      // notice how the top and bottom tags are different
    [/cod&#101;]
    Best of luck, and again, welcome!

  4. #4
    pvictory1 is offline Member
    Join Date
    Oct 2010
    Posts
    7
    Rep Power
    0

    Default

    Thank you so much for your help, but how do I get the program only to accept 10 inputs.

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

    Default

    Quote Originally Posted by pvictory1 View Post
    Thank you so much for your help, but how do I get the program only to accept 10 inputs.
    Accept input within a for loop and make sure that the loop iterates only 10 times.

  6. #6
    pvictory1 is offline Member
    Join Date
    Oct 2010
    Posts
    7
    Rep Power
    0

    Default

    Hi again,
    Ok, if I change my for loop as show in the program it still doesn't stop at 10. I must be missing the boat on this one.

    import java.util.*;

    public class Sum
    {
    static Scanner console = new Scanner(System.in);
    static final int N = 10;

    public static void main(String[]args)
    {
    double number;
    double total = 0;

    System.out.println("Please enter 10 numbers, \n"
    + "adds the positive numbers, and displays the totals.");

    System.out.print("Please enter " + N + " numbers; seperate each with a space: ");

    for (int w = 1; w <= 10;w++)
    {
    number = console.nextDouble();

    if (number > 0)

    total += number;
    {
    continue;
    }
    }
    System.out.println("The total of the positive numbers is: " + total);

    }
    }

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

    Default

    Quote Originally Posted by pvictory1 View Post
    Hi again,
    Ok, if I change my for loop as show in the program it still doesn't stop at 10. I must be missing the boat on this one.

    import java.util.*;

    public class Sum
    {
    static Scanner console = new Scanner(System.in);
    static final int N = 10;

    public static void main(String[]args)
    {
    double number;
    double total = 0;

    System.out.println("Please enter 10 numbers, \n"
    + "adds the positive numbers, and displays the totals.");

    System.out.print("Please enter " + N + " numbers; seperate each with a space: ");

    for (int w = 1; w <= 10;w++)
    {
    number = console.nextDouble();

    if (number > 0)

    total += number;
    {
    continue;
    }
    }
    System.out.println("The total of the positive numbers is: " + total);

    }
    }
    OP, please use code tags and please post formatted code. Many here will hold off reading your code if it is not formatted and easy to read.

  8. #8
    pvictory1 is offline Member
    Join Date
    Oct 2010
    Posts
    7
    Rep Power
    0

    Default

    I'm very sorry about that.
    Java Code:
    import java.util.*; 
    
    public class Sum 
    { 
    static Scanner console = new Scanner(System.in); 
    static final int N = 10; 
    
    public static void main(String[]args) 
    { 
    double number; 
    double total = 0; 
    
    System.out.println("Please enter 10 numbers, \n"
    + "adds the positive numbers, and displays the totals."); 
    
    System.out.print("Please enter " + N + " numbers; seperate each with a space: "); 
    
    for (int w = 1; w <= 10;w++) 
    { 
    number = console.nextDouble(); 
    
    if (number > 0) 
    
    
    total += number; 
    {
    continue; 
    }  
    }
    System.out.println("The total of the positive numbers is: " + total); 
    
    }
    }

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

    Default

    Thanks for the code tags, but you still may want to indent your code according to Java convention (that is what is meant above by posting formatted code). Either that or hopefully someone won't mind reading it all left justified and will help you. Best of luck!

  10. #10
    pvictory1 is offline Member
    Join Date
    Oct 2010
    Posts
    7
    Rep Power
    0

    Default

    Ok is this better?
    Java Code:
    import java.util.*; 
    
    public class Sum 
    { 
    	static Scanner console = new Scanner(System.in); 
    	static final int N = 10; 
    
    	public static void main(String[]args) 
    	{ 
    		double number; 
    		double total = 0; 
    
    		System.out.println("Please enter 10 numbers, \n"
    		+ "adds the positive numbers, and displays the totals."); 
    
    		System.out.print("Please enter " + N + " numbers; seperate each with a space: "); 
    
    			for (int w = 1; w <= 10;w++) 
    	{ 
    			number = console.nextDouble(); 
    
    			if (number > 0) 
    
    			total += number; 
    		{
    			continue; 
    		}  
    	}
    		System.out.println("The total of the positive numbers is: " + total); 
    
    	}
    }

  11. #11
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    786
    Rep Power
    5

    Default

    i think you dont understand your own program :)

    "System.out.print("Please enter " + N + " numbers; seperate each with a space: "); "

    that is not that what your program do... you must input a double value, press enter, and so on...

    after 10 numbers, the loop ends and the sum is printed.

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

    Default

    Almost, but your curly braces are all over the place. This may seem trivial to you, but trust me, if you do this properly (or use a decent IDE that does this for you), you will be able to spot errors and problems much more quickly and be able to understand code that you've written a month ago much more quickly. There are two main styles of brace placement, that I call C style:
    Java Code:
    import java.util.*;
    
    public class Sum {
       static Scanner console = new Scanner(System.in);
       static final int N = 10;
    
       public static void main(String[] args) {
          double number;
          double total = 0;
    
          System.out.println("Please enter 10 numbers, \n"
                   + "adds the positive numbers, and displays the totals.");
    
          System.out.print("Please enter " + N + " numbers; seperate each with a space: ");
    
          for (int w = 1; w <= 10; w++) {
             number = console.nextDouble();
    
             if (number > 0)
    
                total += number;
             {
                continue;
             }
          }
          System.out.println("The total of the positive numbers is: " + total);
    
       }
    }
    or Pascal style:
    Java Code:
    package linkAutoIt.v001;
    
    import java.util.*;
    
    public class Sum
    {
      static Scanner console = new Scanner(System.in);
      static final int N = 10;
    
      public static void main(String[] args)
      {
        double number;
        double total = 0;
    
        System.out.println("Please enter 10 numbers, \n"
            + "adds the positive numbers, and displays the totals.");
    
        System.out.print("Please enter " + N
            + " numbers; seperate each with a space: ");
    
        for (int w = 1; w <= 10; w++)
        {
          number = console.nextDouble();
    
          if (number > 0)
    
            total += number;
          {
            continue;
          }
        }
        System.out.println("The total of the positive numbers is: " + total);
    
      }
    }
    Either will do for general use, but you'll want to stick with one or the other -- usually the one your teacher or team leader requests.

    Looking at your code with indentations OK, it seems like it should work, that if you enter more than 10 numbers, it will only look at the first 10. I do have to wonder what the "continue" statement is doing in your code. Why do you have it in there?

  13. #13
    pvictory1 is offline Member
    Join Date
    Oct 2010
    Posts
    7
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Almost, but your curly braces are all over the place. This may seem trivial to you, but trust me, if you do this properly (or use a decent IDE that does this for you), you will be able to spot errors and problems much more quickly and be able to understand code that you've written a month ago much more quickly. There are two main styles of brace placement, that I call C style:
    Java Code:
    import java.util.*;
    
    public class Sum {
       static Scanner console = new Scanner(System.in);
       static final int N = 10;
    
       public static void main(String[] args) {
          double number;
          double total = 0;
    
          System.out.println("Please enter 10 numbers, \n"
                   + "adds the positive numbers, and displays the totals.");
    
          System.out.print("Please enter " + N + " numbers; seperate each with a space: ");
    
          for (int w = 1; w <= 10; w++) {
             number = console.nextDouble();
    
             if (number > 0)
    
                total += number;
             {
                continue;
             }
          }
          System.out.println("The total of the positive numbers is: " + total);
    
       }
    }
    or Pascal style:
    Java Code:
    package linkAutoIt.v001;
    
    import java.util.*;
    
    public class Sum
    {
      static Scanner console = new Scanner(System.in);
      static final int N = 10;
    
      public static void main(String[] args)
      {
        double number;
        double total = 0;
    
        System.out.println("Please enter 10 numbers, \n"
            + "adds the positive numbers, and displays the totals.");
    
        System.out.print("Please enter " + N
            + " numbers; seperate each with a space: ");
    
        for (int w = 1; w <= 10; w++)
        {
          number = console.nextDouble();
    
          if (number > 0)
    
            total += number;
          {
            continue;
          }
        }
        System.out.println("The total of the positive numbers is: " + total);
    
      }
    }
    Either will do for general use, but you'll want to stick with one or the other -- usually the one your teacher or team leader requests.

    Looking at your code with indentations OK, it seems like it should work, that if you enter more than 10 numbers, it will only look at the first 10. I do have to wonder what the "continue" statement is doing in your code. Why do you have it in there?
    Hi again, Here is what is asked of me. Code a program called SumPositive to enter 10 numbers and only total those that are positive. prompt the user to enter the numbers (double) and print out the total of only the positive ones
    Java Code:
    import java.util.*;
    
    public class Sum {
       static Scanner console = new Scanner(System.in);
       static final int N = 10;
    
       public static void main(String[] args) {
          double number;
          double total = 0;
    
          System.out.println("Please enter 10 numbers, \n"
                   + "adds the positive numbers, and displays the totals.");
    
          System.out.print("Please enter " + N + " numbers; seperate each with a space: ");
    
          for (int w = 1; w <= 10; w++) {
             number = console.nextDouble();
    
             if (number > 0)
    
                total += number;
             {
                continue;
             }
          }
          System.out.println("The total of the positive numbers is: " + total);
    
       }
    }

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

    Default

    Hi again, Here is what is asked of me. Code a program called SumPositive to enter 10 numbers and only total those that are positive. prompt the user to enter the numbers (double) and print out the total of only the positive ones
    OK, but again please describe as precisely as can be what's the current problem and again, please answer the qeustion: why does your program have "continue" in it? What purpose does it serve?

  15. #15
    pvictory1 is offline Member
    Join Date
    Oct 2010
    Posts
    7
    Rep Power
    0

    Default

    The current problem is I need to only accept 10 double numbers and only 10 double numbers and its allowing me to enter in more then 10 numbers. The contiune is just one of the options we could have used but I see that I don't need to use it.
    Java Code:
    import java.util.*; 
    
    public class Sum {
     
    	static Scanner console = new Scanner(System.in); 
    	static final int N = 10; 
    
    	public static void main(String[]args){ 
    		double number; 
    		double total = 0; 
    
    		System.out.println("Please enter 10 numbers, \n"
    		+ "adds the positive numbers, and displays the totals."); 
    
    		System.out.print("Please enter " + N + " numbers; seperate each with a space: "); 
    
    			for (int w = 1; w <= 10;w++) {
    			 
    			number = console.nextDouble(); 
    
    			if (number > 0) 
    
    			total += number; }
    			
    		System.out.println("The total of the positive numbers is: " + total); 
    
    	}
    }

  16. #16
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    nope you do not need continue, and the next question you should be asking yourself is how do I processs a positive number and a negative one. think if statement.

Similar Threads

  1. Replies: 26
    Last Post: 12-03-2013, 02:55 PM
  2. Replies: 1
    Last Post: 09-24-2010, 07:08 PM
  3. Positive Divisor of int
    By starchildren3317 in forum New To Java
    Replies: 16
    Last Post: 02-20-2009, 03:25 AM
  4. Replies: 0
    Last Post: 09-13-2008, 12:59 AM
  5. Java program that stores user inputs
    By staticy2003 in forum Advanced Java
    Replies: 6
    Last Post: 01-24-2008, 07:46 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
  •