Results 1 to 16 of 16
- 10-09-2010, 05:54 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 7
- Rep Power
- 0
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!
- 10-09-2010, 06:11 PM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
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; }
-
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 [code] above your pasted code and the tag [/code] below your pasted code like so:
Best of luck, and again, welcome!Java Code:[code] // your code goes here // notice how the top and bottom tags are different [/code]
- 10-09-2010, 06:33 PM #4
Member
- Join Date
- Oct 2010
- Posts
- 7
- Rep Power
- 0
Thank you so much for your help, but how do I get the program only to accept 10 inputs.
-
- 10-09-2010, 06:47 PM #6
Member
- Join Date
- Oct 2010
- Posts
- 7
- Rep Power
- 0
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);
}
}
-
- 10-09-2010, 07:06 PM #8
Member
- Join Date
- Oct 2010
- Posts
- 7
- Rep Power
- 0
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); } }
-
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-09-2010, 07:19 PM #10
Member
- Join Date
- Oct 2010
- Posts
- 7
- Rep Power
- 0
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); } }
- 10-09-2010, 07:23 PM #11
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
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.
-
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:
or Pascal 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); } }
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.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); } }
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?
- 10-09-2010, 08:05 PM #13
Member
- Join Date
- Oct 2010
- Posts
- 7
- Rep Power
- 0
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 onesJava 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); } }
-
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?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
- 10-09-2010, 08:40 PM #15
Member
- Join Date
- Oct 2010
- Posts
- 7
- Rep Power
- 0
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); } }
- 10-10-2010, 01:30 AM #16
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
Similar Threads
-
Reading strings from the user but dont want to accept numbers ,!
By javanew in forum New To JavaReplies: 1Last Post: 09-24-2010, 07:08 PM -
inputs numbers then outputs how many time a particular number appears
By koji_kun in forum New To JavaReplies: 23Last Post: 12-22-2009, 08:33 PM -
Positive Divisor of int
By starchildren3317 in forum New To JavaReplies: 16Last Post: 02-20-2009, 03:25 AM -
[SOLVED] How do I make user inputs appear bolded??
By mjm1189 in forum EclipseReplies: 0Last Post: 09-13-2008, 12:59 AM -
Java program that stores user inputs
By staticy2003 in forum Advanced JavaReplies: 6Last Post: 01-24-2008, 07:46 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks