Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    jaylimix is offline Member
    Join Date
    May 2011
    Posts
    29
    Rep Power
    0

    Default 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);
        }
    
    
    }
    Hi all, I can't figure out how can I get the values of average, above and below, I mean I can't include it inside the for loop.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    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.

  3. #3
    jaylimix is offline Member
    Join Date
    May 2011
    Posts
    29
    Rep Power
    0

    Default

    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

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    I'm still having a problem though
    please explain what it is

  5. #5
    jaylimix is offline Member
    Join Date
    May 2011
    Posts
    29
    Rep Power
    0

    Default

    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).

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    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.

  7. #7
    jaylimix is offline Member
    Join Date
    May 2011
    Posts
    29
    Rep Power
    0

    Default

    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);
        }
    
    
    }
    This is my current code with the outputs:


    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

  8. #8
    jaylimix is offline Member
    Join Date
    May 2011
    Posts
    29
    Rep Power
    0

    Default

    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);
        }
    
    
    }
    This is my current code with the outputs:


    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

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    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.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,044
    Rep Power
    20

    Default

    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...

  11. #11
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,571
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by jaylimix View Post
    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 2an
    Number of scores below average is 4
    How can the average be 13 while all of the numbers are less than 13? There must be an error in the computation of the average.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    jaylimix is offline Member
    Join Date
    May 2011
    Posts
    29
    Rep Power
    0

    Default

    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.

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    It needs to be sum of scores divide by the number of scores.
    That means you must do it in two steps. First step: sum the numbers, second step divide

  14. #14
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,571
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Norm View Post
    That means you must do it in two steps. First step: sum the numbers, second step divide
    And only then comes step 3: find the numbers that are lower/higher than the average (calculated in step two). Doing those elementary steps greatly simplifies your loops, i.e. you don't have to try to cram everything in one big loop.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  15. #15
    yellowledbet is offline Senior Member
    Join Date
    Feb 2011
    Location
    Georgia, USA
    Posts
    122
    Rep Power
    0

    Default

    Quote Originally Posted by jaylimix View Post
    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.
    So you need a variable to count how many scores are entered. Increment it each time a score is entered.

  16. #16
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Cool

    Quote Originally Posted by yellowledbet View Post
    So you need a variable to count how many scores are entered. Increment it each time a score is entered.
    Instead [if used correctly] the for loop initiator variable "i" can be used to calculate the average.

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  17. #17
    jaylimix is offline Member
    Join Date
    May 2011
    Posts
    29
    Rep Power
    0

    Default

    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);
        }
    
    
    }

  18. #18
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,571
    Blog Entries
    7
    Rep Power
    21

    Default

    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  19. #19
    jaylimix is offline Member
    Join Date
    May 2011
    Posts
    29
    Rep Power
    0

    Default

    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);
        }
    
    
    }

  20. #20
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    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; 06-05-2011 at 07:18 PM.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 11
    Last Post: 09-27-2010, 12:20 PM
  2. Variable name determined by another variable's value
    By Lumpkabob in forum New To Java
    Replies: 5
    Last Post: 04-14-2009, 08:00 AM
  3. Java path variable's
    By Jordsk in forum New To Java
    Replies: 2
    Last Post: 04-09-2009, 02:20 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •