What I can't figure out is the unspecified amount of numbers which are put in. For example if it would always be 5 numbers this would be so much easier.
Count input values?
Collapse
X
-
Here's a little programming tip: you can express what you want here a bit moreOriginally posted by dadimarThis is one way to count how often input numbers are put in.Code:int count = 1; int number = read.nextInt(); while (number != 0) { number = read.nextInt(); if (number != 0) count++; }; System.out.println("Quit....");
efficiently; this idiom is used a lot by more experienced and older twits like me:
[code=java]
int count = 0;
for (int number; (number= read.nextInt()) != 0; count++) {
// your valuable code here using 'number'
// no need to test for 0 here again.
}
[/code]
kind regards,
JosComment
-
But it doesn't make any difference in this case! Let's see, you've got this code:Originally posted by dadimarWhat I can't figure out is the unspecified amount of numbers which are put in. For example if it would always be 5 numbers this would be so much easier.
[CODE=java]
int count = 1;
int number = read.nextInt();
while (number != 0) {
number = read.nextInt();
if (number != 0) count++;
// do something with "number"
} // You don't need a semicolon here, although it isn't wrong
System.out.prin tln("Quit....") ;
[/CODE]or, as JosAH suggested:
[CODE=java]
int count = 0;
for (int number; (number= read.nextInt()) != 0; count++) {
// your valuable code here using 'number'
// no need to test for 0 here again.
}
[/CODE]Now you have to know, what the sum of all numbers, that were typed in at some point, is. Say, you have another variable: int sum...
By the way, you do know that variables are, as the name says, variable? So their value can be changed, even when they are read out to do so:
[CODE=java]
int a = 3;
a *= 3; // or you can use a = a * 3;
[/CODE]
Greetings,
NepomukComment
-
I did a little research. Here's my code. Tell me how you like it:
I used the math round to limit the decimal places on the average outcome. I used JOptionPane because I didn't know how to do this with the Scanner.Code:import javax.swing.JOptionPane; public class ListingBls99 { public static void main(String[] args) { String dataString = JOptionPane.showInputDialog( "Enter a number:\n (The program will shut down and give you your resault if you press 0 )"); double data = Double.parseDouble(dataString); double countMinus = 0.0; double countPlus = 0.0; double countAll = 0.0; double sum = 0.0; while (data != 0.0) { sum += data; if (data < 0.0) countMinus++; if (data > 0.0) countPlus++; if (data != 0.0) countAll++; double average = sum/countAll; dataString = JOptionPane.showInputDialog( "Enter a number:\n (The program will shut down and give you your resault if you press 0 )"); data = Double.parseDouble(dataString); JOptionPane.showMessageDialog(null, ("The sum of all numbers is: " + sum + "\n You used: " + countAll + " numbers all together." + "\n You used: " + countMinus + " negative numbers." + "\n You used: " + countPlus + " positive numbers." + "\n The average of the sum of all input numbers: " + (average = Math.round(average*100.0)/100.0))); } } }
The code works but it probably isn't well organized nor the best way to this.Comment
-
I just wanted to say that the easiest way of getting know something is to read about it.Originally posted by dadimarI did a little research. Here's my code. Tell me how you like it:
I used the math round to limit the decimal places on the average outcome. I used JOptionPane because I didn't know how to do this with the Scanner.Code:import javax.swing.JOptionPane; public class ListingBls99 { public static void main(String[] args) { String dataString = JOptionPane.showInputDialog( "Enter a number:\n (The program will shut down and give you your resault if you press 0 )"); double data = Double.parseDouble(dataString); double countMinus = 0.0; double countPlus = 0.0; double countAll = 0.0; double sum = 0.0; while (data != 0.0) { sum += data; if (data < 0.0) countMinus++; if (data > 0.0) countPlus++; if (data != 0.0) countAll++; double average = sum/countAll; dataString = JOptionPane.showInputDialog( "Enter a number:\n (The program will shut down and give you your resault if you press 0 )"); data = Double.parseDouble(dataString); JOptionPane.showMessageDialog(null, ("The sum of all numbers is: " + sum + "\n You used: " + countAll + " numbers all together." + "\n You used: " + countMinus + " negative numbers." + "\n You used: " + countPlus + " positive numbers." + "\n The average of the sum of all input numbers: " + (average = Math.round(average*100.0)/100.0))); } } }
The code works but it probably isn't well organized nor the best way to this.Comment
-
You're code isn't bad, but there are a few things, that you could/should change.Originally posted by dadimarI did a little research. Here's my code. Tell me how you like it:
I used the math round to limit the decimal places on the average outcome. I used JOptionPane because I didn't know how to do this with the Scanner.Code:import javax.swing.JOptionPane; public class ListingBls99 { public static void main(String[] args) { String dataString = JOptionPane.showInputDialog( "Enter a number:\n (The program will shut down and give you your resault if you press 0 )"); double data = Double.parseDouble(dataString); double countMinus = 0.0; double countPlus = 0.0; double countAll = 0.0; double sum = 0.0; while (data != 0.0) { sum += data; if (data < 0.0) countMinus++; if (data > 0.0) countPlus++; if (data != 0.0) countAll++; double average = sum/countAll; dataString = JOptionPane.showInputDialog( "Enter a number:\n (The program will shut down and give you your resault if you press 0 )"); data = Double.parseDouble(dataString); JOptionPane.showMessageDialog(null, ("The sum of all numbers is: " + sum + "\n You used: " + countAll + " numbers all together." + "\n You used: " + countMinus + " negative numbers." + "\n You used: " + countPlus + " positive numbers." + "\n The average of the sum of all input numbers: " + (average = Math.round(average*100.0)/100.0))); } } }
The code works but it probably isn't well organized nor the best way to this.- [CODE=java]
// Counters don't have to be doubles - it's a waste of space
int countMinus = 0;
int countPlus = 0;
int countAll = 0;
[/CODE] - [CODE=java]
while (data != 0.0) {
sum += data;
if (data < 0.0) countMinus++;
if (data > 0.0) countPlus++;
if (data != 0.0) countAll++;
}
// can be improved to:
while (data != 0.0) {
sum += data;
if (data < 0.0) countMinus++;
else if (data > 0.0) countPlus++; // your data can't be positive AND negative
countAll++; // save a little CPU time - not relevant, but when you're improving your code anyway...
}
countAll--;
[/CODE] - Changing this to a version, which uses Scanner isn't difficult, and using Swing (the package, that JOptionPane belongs to) before having read about it isn't that a good idea. At the beginning, you should try to use the console.
- You nearly had the solution before and now you changed your code completely - that makes me think, that you probably don't understand quite how your code works. Have another good look at it and try to understand.
Greetings,
NepomukComment
- [CODE=java]
-
I don't like it one bit; no insult intended though. I'm a nitpicker and I'd take all theOriginally posted by dadimarI did a little research. Here's my code. Tell me how you like it:
'logic' out of the main method and create a separate method for it. I also get
the shivers when I see duplicated code.
I appreciate (I really do) that you managed to figure out a working version but I
still don't like that version one bit. Keep on studying and I appreciate your effiorts.
kind regards,
JosComment
Comment