Results 1 to 20 of 48
Thread: Need help
- 04-19-2011, 12:09 AM #1
Member
- Join Date
- Apr 2011
- Posts
- 30
- Rep Power
- 0
Need help
i'm writing a program thats gonna ask the user to enter 5 test scores and calculate the avg and view the highest and lowest grade.
public class LabAssig8 {
public static void main(String[] args) {
getUserInput();
}
public static double[] getUserInput() {
int numT =4;
double[] scores;
scores = new double[numT];
do {
for (int i = 0; i <= numT; i++)
JOptionPane.showInputDialog("Enter score " + (i + 1) + ": ");
} while (scores[numT]>= 0 && scores[numT] <= 100);
return (scores);
}
The code above has no error but It's not looping. the score has to be between 0 and 100.
public static void calculateAverage(double[] scores) {
avg = scores[numT] / 5;
JOptionPane.showInputDialog("The average of 5 " + "test scores"
+ " is " + avg);
}
Is the code about right? I'm trying to calc the avg and I'm not able to do that since i'm still new to java
public static void findHighLow(double[] scores) {
Above is a method that I have to use to get the highest and lowest grade. Can any one help me PLEAAAASEE. I've been trying to figure out
something that works for like the past 3 days but nothing
Thanks
- 04-19-2011, 12:17 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
There are some problems. I will point out first that you may want to consider a for loop to get the input and no do while loop.
Since you know how many times you want to loop a for loop is ideal. Your goal will be to loop from 0 - array.length - 1, like this
This will loop x amount of times, where x is the length of the list. This will allow you do do something with each item in the array. With this you can get the user input and return the scores in the array correctly. The validation can also happen in the for loop with a while loop.Java Code:int[] blah = new int[10]; for(int i = 0; i < blah.length; i++){ //do stuff }
To find the average you will want to add the scores up, again, a for loop here is very ideal. Once you have added all the scores in the array you will need to divide it by the length of the array.
Finally, for finding the high and low variables you will also want to use for loops. Try correcting these two things and then post back. Be sure to use code tags when posting code.
[ code] //no space
YOUR CODE HERE
[/code]
- 04-19-2011, 12:23 AM #3
Member
- Join Date
- Apr 2011
- Posts
- 30
- Rep Power
- 0
is this right?
Java Code:// public static void main(String[] args) { getUserInput(); } public static double[] getUserInput() { double[] scores; int[] numT = new int[4]; do { for (int i = 0; i <= numT.length; i++) JOptionPane.showInputDialog("Enter score " + (i + 1) + ": "); } while (scores[numT>= 0 && scores[numT] <= 10) return (scores); }
- 04-19-2011, 12:30 AM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
It's close. Here is some pseudo code of this method
Java Code:declare scores array loop get user input loop if input is not from 0 - 100 get user input end loop store input in array end loop return scores array
- 04-19-2011, 12:33 AM #5
Member
- Join Date
- Apr 2011
- Posts
- 30
- Rep Power
- 0
my problem is I know the steps but I'm not being able to write them as code :/
- 04-19-2011, 12:39 AM #6
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
You don't want to use a do while loop. If your user enters a number that isn't 1 - 100 you want to start a loop which continues until the user enters a correct number. A for loop lets you loop x times, so you can loop through the array quite easily.
Java Code:for loop while(input < 0 || input > 100){ prompt store input } end for loop
- 04-19-2011, 12:43 AM #7
Member
- Join Date
- Apr 2011
- Posts
- 30
- Rep Power
- 0
I hope this is right
Java Code:\\ double[] scores; int[] numT = new int[4]; for (int i = 0; i <= numT.length; i++){ while(scores >= 0 || scores <= 10) { JOptionPane.showInputDialog("Enter score " + (i + 1) + ": "); } } return (scores); }
- 04-19-2011, 12:49 AM #8
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
It's closer, you need to remember to test yourself as well. Your code has pseudo code like this
You are missing actually storing the input in the array, it should look more like thisJava Code:declare scores declare and initialize numT for loop while loop prompt end while end for return scores
Java Code:declare scores and initialize for loop declare input and initialize with user input while loop //only entered if input is out of range store user input in input variable end while store in scores array end for return scores
- 04-19-2011, 12:56 AM #9
Member
- Join Date
- Apr 2011
- Posts
- 30
- Rep Power
- 0
so declare input would be int = numT; right?
and how do you initialize it with user input?
I'm sorry I just don;t understand programming language :(
- 04-19-2011, 01:03 AM #10
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
I don't mind helping you out. The input you want named inside the loop and it will be re created each time through the loop, and destroyed once it leaves the loop.
will return a string argument, so you want to capture it in a string, and then parse it to get the double grade.Java Code:JOptionPane.showInputDialog("Enter score " + (i + 1) + ": ");
Java Code:for loop String s = JOptionPane.showInputDialog("Enter score " + (i + 1) + ": "); //stores the user input double score = Double.parseDouble(s); //parses the string above into a double while(score is not in correct range){ repeat above } store score in array end for
- 04-19-2011, 01:11 AM #11
Member
- Join Date
- Apr 2011
- Posts
- 30
- Rep Power
- 0
Thanks soo much for wanting to help me.
I have a red line under double scores = Double.parseDouble(numT);Java Code:// public static double[] getUserInput() { int[] scores = new int[4]; for (int i = 0; i <= scores.length; i++){ String numT = JOptionPane.showInputDialog("Enter score " + (i + 1) + ": "); double scores = Double.parseDouble(numT); while(scores <= 0 || scores >= 100) { } } return (scores); }
- 04-19-2011, 01:13 AM #12
Member
- Join Date
- Apr 2011
- Posts
- 30
- Rep Power
- 0
I guess it should be something like this?Java Code:public static double[] getUserInput() { int[] scores = new int[4]; for (int i = 0; i <= scores.length; i++){ String numT = JOptionPane.showInputDialog("Enter score " + (i + 1) + ": "); double scores = Double.parseDouble(numT); while(scores <= 0 || scores >= 100){ JOptionPane.showInputDialog("Enter score " + (i + 1) + ": "); } } return (scores); }
- 04-19-2011, 01:21 AM #13
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
This is close, but still a bit off, first the local variable in the loop should not have the same name as the array variable at the beginning, change scores inside the loop to score.
In the for loop you need to do the same thing to capture the value again(re use numT to store the input as a string, then use score to parse the string(numT), finally, in the for loop, at the end you must add the score to the scores array. Also, declare scores at the top as a double[].
You said in the method header that the method will return a double array, not an integer array, the compiler will complain if you don't match what the method header says.
- 04-19-2011, 01:30 AM #14
Member
- Join Date
- Apr 2011
- Posts
- 30
- Rep Power
- 0
ok I still have a red line under double score = Double.parseDouble(numT);Java Code:public static double[] getUserInput() { int[] scores = new int[4]; for (int i = 0; i <= scores.length; i++){ String numT = JOptionPane.showInputDialog("Enter score " + (i + 1) + ": "); double score = Double.parseDouble(numT); while(score <= 0 || score >= 100){ JOptionPane.showInputDialog("Enter score " + (i + 1) + ": "); double score = Double.parseDouble(numT); } } return (scores); }
- 04-19-2011, 01:42 AM #15
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Again, this is close, but whats the whole goal of this method? Is it to simply get user input and throw it away, or do you want to store it somewhere? What do you want to store it in? How do you store it?Java Code:public static double[] getUserInput() { int[] scores = new int[4]; for (int i = 0; i <= scores.length; i++){ String numT = JOptionPane.showInputDialog("Enter score " + (i + 1) + ": "); double score = Double.parseDouble(numT); while(score <= 0 || score >= 100){ numT = JOptionPane.showInputDialog("Enter score " + (i + 1) + ": ");//ADDED THE "numT =" PART HERE double score = Double.parseDouble(numT); } } return (scores); }
- 04-19-2011, 01:49 AM #16
Member
- Join Date
- Apr 2011
- Posts
- 30
- Rep Power
- 0
hmmm I'm confused. what do you mean by store it?
- 04-19-2011, 02:02 AM #17
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
At the end of your method you are returning the array named scores, but what does this array contain? When did you add values to it?
- 04-19-2011, 02:10 AM #18
Member
- Join Date
- Apr 2011
- Posts
- 30
- Rep Power
- 0
Am I suppose to return getUserInput(); ?
and Its only repeating once. If I enter 101 and it asks me again to reenter if I re enter a smaller number it ends.
- 04-19-2011, 02:37 AM #19
Member
- Join Date
- Apr 2011
- Posts
- 30
- Rep Power
- 0
Help! Anyone? please
- 04-19-2011, 02:37 AM #20
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
No, the method should return an array. In the for loop you are doing the following
You are missing the most important setJava Code:declare array initialize array for loop declare string initialize with input declare double initialize with parsed string while(not in range) repeat lines 3 and 4 end while end for
where should this line go in my pseudo code above?Java Code:store input in array index i


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks