Results 1 to 20 of 21
Thread: Can't get the variable's value
 05312011, 04:38 PM #1Member
 Join Date
 May 2011
 Posts
 29
 Rep Power
 0
Can't get the variable's value
Java Code:import java.util.Scanner; import java.math.*; public class Q1 { public static void main(String[] args) { int sum=0; System.out.println("Enter the scores(negative number to end)"); for(int i=1;i<10;i++){ System.out.print("Score "+i+": "); int[] number= new int[10]; Scanner input= new Scanner(System.in); number[i]= input.nextInt(); sum+= number[i]; if(number[i]<0){ break; } int average= sum/2; if(number[i]>=average){ int above=0; ++above; } else{ int below=0; ++below; } } System.out.printf("Results\n\n"); System.out.println("Average is "+ average); System.out.println("Number of scores above or equal to average is "+ above); System.out.println("Number of scores below average is "+ below); } }
 05312011, 05:09 PM #2
You need to consider the scope of the variables' definitions. A variable is only known within the {} that enclose its definition. Outside of the enclosing {} it is not known. Read about "scope" for more info.
Basically move the definitions of the variables to the same level as where you are trying to reference them.
For example sum is at the highest level in main(). The others are within a for loop.
 05312011, 05:17 PM #3Member
 Join Date
 May 2011
 Posts
 29
 Rep Power
 0
Hi thanks, I can't believe how easy it is to solve it yet I can't think of it. All I did was change the position of the variable. I'm still having a problem though, below is my output:
Enter the scores(negative number to end)
Score 1: 5
Score 2: 6
Score 3: 4
Score 4: 6
Score 5: 2
Score 6: 3
Score 7: 8
Score 8: 1
Results

Average is 17
Number of scores above or equal to average is 2
Number of scores below average is 5
 05312011, 05:21 PM #4I'm still having a problem though
 05312011, 05:25 PM #5Member
 Join Date
 May 2011
 Posts
 29
 Rep Power
 0
The output should be:
Average is 17
Number of scores above or equal to average is 0
Number of scores below average is 7
Because non of the scores are above or equal to average(17) but all of the scores are below the average(17).
 05312011, 05:28 PM #6
You need to post your new code after you made the fixes for the first problem.
Try debugging your code by adding printlns to show variable values as the code is executed. For example add one before an if test to show the values of the two variables that are being compared.
 05312011, 05:31 PM #7Member
 Join Date
 May 2011
 Posts
 29
 Rep Power
 0
Java Code:import java.util.Scanner; import java.math.*; public class Q1 { public static void main(String[] args) { int average=0; int above=0; int below=0; int sum=0; System.out.println("Enter the scores(negative number to end)"); for(int i=1;i<10;i++){ System.out.print("Score "+i+": "); int[] number= new int[10]; Scanner input= new Scanner(System.in); number[i]= input.nextInt(); sum+= number[i]; if(number[i]<0){ break; } average= sum/2; } if(number[i]>=average){ ++above; } else{ ++below; } System.out.printf("Results\n\n"); System.out.println("Average is "+ average); System.out.println("Number of scores above or equal to average is "+ above); System.out.println("Number of scores below average is "+ below); } }
Enter the scores(negative number to end)
Score 1: 5
Score 2: 6
Score 3: 9
Score 4: 1
Score 5: 2
Score 6: 3
Score 7: 1
Results

Average is 13
Number of scores above or equal to average is 2
Number of scores below average is 4
 05312011, 05:32 PM #8Member
 Join Date
 May 2011
 Posts
 29
 Rep Power
 0
Java Code:import java.util.Scanner; import java.math.*; public class Q1 { public static void main(String[] args) { int average=0; int above=0; int below=0; int sum=0; System.out.println("Enter the scores(negative number to end)"); for(int i=1;i<10;i++){ System.out.print("Score "+i+": "); int[] number= new int[10]; Scanner input= new Scanner(System.in); number[i]= input.nextInt(); sum+= number[i]; if(number[i]<0){ break; } average= sum/2; } if(number[i]>=average){ ++above; } else{ ++below; } System.out.printf("Results\n\n"); System.out.println("Average is "+ average); System.out.println("Number of scores above or equal to average is "+ above); System.out.println("Number of scores below average is "+ below); } }
Enter the scores(negative number to end)
Score 1: 5
Score 2: 6
Score 3: 9
Score 4: 1
Score 5: 2
Score 6: 3
Score 7: 1
Results

Average is 13
Number of scores above or equal to average is 2
Number of scores below average is 4
 05312011, 05:35 PM #9
Try debugging your code by adding printlns to show variable values as the code is executed. For example add one before an if test to show the values of the two variables that are being compared.
Play computer with your program. Do the statements one by one and write down the results of each statement on a piece of paper as you go thru the program. You should be able to see where your logic problem is.
 05312011, 05:38 PM #10Moderator
 Join Date
 Apr 2009
 Posts
 12,370
 Rep Power
 21
You are comparing each score as it is entered against the average up to that point.
Print out the value of average as you go through the loop to see what I mean.
In addition, that is not the average of those scores...
 05312011, 05:42 PM #11
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,951
 Blog Entries
 7
 Rep Power
 22
 05312011, 06:02 PM #12Member
 Join Date
 May 2011
 Posts
 29
 Rep Power
 0
Ya I made a mistake in this computation > average= sum/2;
Now I'm trying to figure out what to replace the 2. It needs to be sum of scores divide by the number of scores.
 05312011, 06:09 PM #13It needs to be sum of scores divide by the number of scores.
 05312011, 06:30 PM #14
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,951
 Blog Entries
 7
 Rep Power
 22
I have the stamina of a seal; I lie on the beach instead of running on it.
 05312011, 08:54 PM #15Senior Member
 Join Date
 Feb 2011
 Location
 Georgia, USA
 Posts
 122
 Rep Power
 0
 06012011, 07:39 AM #16
 06052011, 11:55 AM #17Member
 Join Date
 May 2011
 Posts
 29
 Rep Power
 0
Output:
Enter the scores(negative number to end)
Score 1: 1
Score 2: 2
Score 3: 3
Score 4: 1
Results

Average is 2
Number of scores above or equal to average is 3
Number of scores below average is 0
I got my average correct now but my above and below are still wrong.
Java Code:import java.util.Scanner; import java.math.*; public class Q1 { public static void main(String[] args) { int average=0; int above=0; int below=0; int sum=0; int j=0; System.out.println("Enter the scores(negative number to end)"); int[] number= new int[10]; for(int i=1;i<number.length;i++){ System.out.print("Score "+i+": "); Scanner input= new Scanner(System.in); number[i]= input.nextInt(); sum+= number[i]; ++j; if(number[i]<0){ break; } average= sum/j; if(number[i]>=average){ ++above; } else{ ++below; } } System.out.printf("Results\n\n"); System.out.println("Average is "+ average); System.out.println("Number of scores above or equal to average is "+ above); System.out.println("Number of scores below average is "+ below); } }
 06052011, 12:22 PM #18
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,951
 Blog Entries
 7
 Rep Power
 22
You did cram everything in one big, complicated loop; try to execute it by hand and see how the value of variable average changes and influences the outcome of variables above and below. Do those calculations afterwards when the value of variable average is known.
kind regards,
JosI have the stamina of a seal; I lie on the beach instead of running on it.
 06052011, 07:02 PM #19Member
 Join Date
 May 2011
 Posts
 29
 Rep Power
 0
Okay I've separated it into 2 for loops but I'm still having problem with the out put:
Enter the scores(negative number to end)
Score 1: 1
Score 2: 2
Score 3: 3
Score 4: 1
Results

Average is 2.0
Number of scores above or equal to average is 1 // this should be 2
Number of scores below average is 2 // this should be 1
Java Code:import java.util.Scanner; import java.math.*; public class Q1 { public static void main(String[] args) { double average=0; int above=0; int below=0; double sum=0; int numberOfScore=0; Scanner input= new Scanner(System.in); System.out.println("Enter the scores(negative number to end)"); int[] number= new int[10]; for(int i=1;i<number.length;i++){ System.out.print("Score "+i+": "); number[i]= input.nextInt(); if(number[i]<0){ break; } else{ sum+= number[i]; ++numberOfScore; average=sum/numberOfScore; } } for(int i=0;i<numberOfScore;i++){ if(number[i]>=average){ ++above; } else{ ++below; } } System.out.printf("Results\n\n"); System.out.println("Average is "+ average); System.out.println("Number of scores above or equal to average is "+ above); System.out.println("Number of scores below average is "+ below); } }
 06052011, 07:09 PM #20
Add some printlns to show what numbers are being considered above the average and what are below the average.
For example add this for the above if test:
System.out.println("above=" + number[i] +", average=" + average);
The output will show you how your if tests are working
If that doesn't show what is happening, keep adding printlns to show ALL the numbers you are working with.
That is what I had to do to find the bugs in your code.
Keep adding printlns until you see what is wrong.Last edited by Norm; 06052011 at 07:18 PM.
Similar Threads

How to set a variable's value in one class, while having the value updated in another
By SeanC in forum New To JavaReplies: 11Last Post: 09272010, 12:20 PM 
Variable name determined by another variable's value
By Lumpkabob in forum New To JavaReplies: 5Last Post: 04142009, 08:00 AM 
Java path variable's
By Jordsk in forum New To JavaReplies: 2Last Post: 04092009, 02:20 AM
Bookmarks