Page 1 of 1

JAVA

Posted: Tue Feb 03, 2004 4:53 pm
by dull1554
does anyone here know a bit about java, i'm having trouble using variables created inside a loop,

Code: Select all

import cs1.Keyboard;
 public class Final
 {
 	public static void main(Stringї]args)
 	{
 		int num1 = 3;
 		int num2 = 5;
 		int min = 0;
 		System.out.println("You will now enter 3 test grades,\nthen 5 quiz grades\nthen the students average will be outputed:"); 		
 		
 		while (num1!=min)
 		{
 			System.out.println("Please enter a test grades:");
 			int Test = Keyboard.readInt();
 			if ((Test<0) || (Test>100))
 			&#123;
 				System.out.println("Please input a valid grade (greater than 0)");
 			&#125;
 			if ((Test>0) && (Test<100))
 			&#123;
 			num1--;
 			&#125;
 		&#125;
 		
 			
 		while (num2!=min)
 		&#123;
 			System.out.println("Please enter a quiz grades:");
 			int quiz = Keyboard.readInt();
 			if ((quiz<0) || (quiz>100))
 			&#123;
 				System.out.println("Please input a valid grade (greater than 0)");
 			&#125;
 			if ((quiz>0) && (quiz<100))
 			&#123;
 			num2--;
 			&#125;
 		&#125;
 		
 		
 				
 	&#125;
 &#125;
i'm using 2 loops to collect test and quiz grades, but if i try to do like

System.out.println(Test);

it cant find the $var Test, once i have collected all the grades, i need to do some math on them so calculate the final average

Posted: Wed Feb 04, 2004 8:11 am
by kettle_drum
The variable scope looks to be inside of the while loop only. Declaire it at the start of the class and it should work fine.