JAVA

XML, Perl, Python, and other languages can be discussed here, even if it isn't PHP (We might forgive you).

Moderator: General Moderators

Post Reply
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

JAVA

Post 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
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

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