I'm trying to write a program that reads unspecified number of positive and negative values, counts them and computes the average of the input values, not counting zeros. The program should end with the input 0 and display the average as a floating point number.
What can I use to count the input values?
Example: If I type 1, 2 and 0 the average should be 1.5
Ps: I'm a beginner...
21 10530
I'm trying to write a program that reads unspecified number of positive and negative values, counts them and computes the average of the input values, not counting zeros. The program should end with the input 0 and display the average as a floating point number.
What can I use to count the input values?
Example: If I type 1, 2 and 0 the average should be 1.5
Ps: I'm a beginner...
Read about the Scanner class (for reading the input) and try the program. You should make use of a while loop.
Post if you get stuck with the code.
Read about the Scanner class (for reading the input) and try the program. You should make use of a while loop.
Post if you get stuck with the code.
Yes I know how to read the input, but how do I count it ?
Yes I know how to read the input, but how do I count it ?
How would you count them in real life? Think about that, then you'll find the answer.
PS.: Have you found out yet, if the number is positive or negative? That's the first step of course.
Greetings,
Nepomuk
Yes I know how to read the input, but how do I count it ?
The while loop
I have no idea. Am I on the right track:
import java.util.Scanner;
public class Skilaverkefni2{
public static void main(String[] args) {
Scanner read = new Scanner(System.in);
System.out.println("text: ");
int number = read.nextInt();
while (number != 0) {
}
System.out.println("Quit");
}
}
I have no idea. Am I on the right track:
import java.util.Scanner;
public class Skilaverkefni2{
public static void main(String[] args) {
Scanner read = new Scanner(System.in);
System.out.println("text: ");
int number = read.nextInt();
while (number != 0) {
}
System.out.println("Quit");
}
}
1.) Use code tags for posting code
2.) Try it on the compiler and see what it does or doesn't do.
-
-
while (number != 0) {
-
number = read.nextInt();
-
};
-
System.out.println("Quit");
This gives me the ability to enter unspecified amount of numbers until I decide to quit by entering 0 as an input.
-
-
while (number != 0) {
-
number = read.nextInt();
-
};
-
System.out.println("Quit");
This gives me the ability to enter unspecified amount of numbers until I decide to quit by entering 0 as an input.
I'm trying to figure out how I can add the input numbers together and get the average. Am I on the right track?
I'm trying to figure out how I can add the input numbers together and get the average. Am I on the right track?
Yes, you are. Now think about how you can calculate the average - how would you do it in real life?
Greetings,
Nepomuk
I'm trying to figure out how I can add the input numbers together and get the average. Am I on the right track?
You need a counter (int) to count the numbers and a double variable to store the total and perhaps another one for the average.
Yes, you are. Now think about how you can calculate the average - how would you do it in real life?
Greetings,
Nepomuk
I would add them together, count the amount of numbers and divide with the amount.
What I learned is that this code reads the next input number, but overwrites it if I decide to continue with a new number I put in. -
while (number != 0) {
-
number = read.nextInt();
-
};
-
System.out.println("Quit");
I put 25, 43 and 55 in. If I add System.out.println(number) I get the last number 55.
I would add them together, count the amount of numbers and divide with the amount.
What I learned is that this code reads the next input number, but overwrites it if I decide to continue with a new number I put in. -
while (number != 0) {
-
number = read.nextInt();
-
};
-
System.out.println("Quit");
I put 25, 43 and 55 in. If I add System.out.println(number) I get the last number 55.
Perhaps now is the time to read a tutorial?
-
int count = 1;
-
int number = read.nextInt();
-
-
while (number != 0) {
-
number = read.nextInt();
-
if (number != 0) count++;
-
};
-
System.out.println("Quit....");
-
This is one way to count how often input numbers are put in.
-
int count = 1;
-
int number = read.nextInt();
-
-
while (number != 0) {
-
number = read.nextInt();
-
if (number != 0) count++;
-
};
-
System.out.println("Quit....");
-
This is one way to count how often input numbers are put in.
Good, now you have the last number and the amount of numbers. You just need the sum of all numbers now... You're nearly there!
Greetings,
Nepomuk
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.
-
int count = 1;
-
int number = read.nextInt();
-
-
while (number != 0) {
-
number = read.nextInt();
-
if (number != 0) count++;
-
};
-
System.out.println("Quit....");
-
This is one way to count how often input numbers are put in.
Here's a little programming tip: you can express what you want here a bit more
efficiently; this idiom is used a lot by more experienced and older twits like me: -
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.
-
}
-
kind regards,
Jos
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.
But it doesn't make any difference in this case! Let's see, you've got this code: -
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.println("Quit....");
-
or, as JosAH suggested: -
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.
-
}
-
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: -
int a = 3;
-
a *= 3; // or you can use a = a * 3;
-
Greetings,
Nepomuk
I did a little research. Here's my code. Tell me how you like it: -
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)));
-
-
}
-
}
-
}
-
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.
The code works but it probably isn't well organized nor the best way to this.
I did a little research. Here's my code. Tell me how you like it: -
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)));
-
-
}
-
}
-
}
-
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.
The code works but it probably isn't well organized nor the best way to this.
I just wanted to say that the easiest way of getting know something is to read about it.
I did a little research. Here's my code. Tell me how you like it: -
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)));
-
-
}
-
}
-
}
-
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.
The code works but it probably isn't well organized nor the best way to this.
You're code isn't bad, but there are a few things, that you could/should change. -
-
// Counters don't have to be doubles - it's a waste of space
-
int countMinus = 0;
-
int countPlus = 0;
-
int countAll = 0;
-
-
-
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--;
-
- 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.
As r035198x said, read about Java a bit more. There are some nice articles in the "Articles -> Java" Section and you can find a good eBook recommended here.
Greetings,
Nepomuk
I did a little research. Here's my code. Tell me how you like it:
I don't like it one bit; no insult intended though. I'm a nitpicker and I'd take all the
'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,
Jos
Post your reply Sign in to post your reply or Sign up for a free account.
Similar topics
2 posts
views
Thread by Pete |
last post: by
|
6 posts
views
Thread by M.Kamermans |
last post: by
|
4 posts
views
Thread by ZyPhiX |
last post: by
|
10 posts
views
Thread by Jon |
last post: by
|
68 posts
views
Thread by Martin Joergensen |
last post: by
| |
22 posts
views
Thread by MP |
last post: by
|
2 posts
views
Thread by nick048 |
last post: by
| | | | | | | | | | | |