I am VERY VERY new to Java (started Monday last)... and also have the dubious pleasure (ha ha) of being an external student...
My problem is this: in the attached I can't understand why the do/while loop wont work.
I get the error message:
m7.java:95: variable response might not have been initialized
}while(response != 'q');
^
The error points to the 'r' in response.
If I take out the do/ while , the program works but not correctly( it is sposed to loop!)
Can anyone help me??
Also, apologies if I didn't post correctly, still getting used to all this.
thank you
mittens
PS Never study this stuff externally!!!
import B102.*;
class m7
{
static void printMenu()
{
Screen.out.println("You have the following options:");
Screen.out.println("Enter A to print name.");
Screen.out.println("Enter B to print Tutorial.");
Screen.out.println("Enter C for number display.");
Screen.out.println("Enter D for a random number between 1 and 10 to be displayed.");
Screen.out.println("Enter Q to quit.");
}
static void printName()
{
Screen.out.println("mittens.");
Screen.out.println("");
}
static void printTutorial()
{
Screen.out.println("No tutorial time. External student.");
Screen.out.println("");
}
static void NumCount()
{
int num, i;
Screen.out.print("Enter an integer number between 1 and 50:");
num = Keybd.in.readInt();
if((num <= 0) || (num >= 51))
Screen.out.print("Invalid Range. It must be between 1 and 50!");
else
for(i = 0; i <= num; i++)
Screen.out.print(" " + i);
Screen.out.println(" ");
Screen.out.println(" ");
}
static void RandomNumber()
{
int guess;
final int GUESS_MAX = 10;
guess = (int)(Math.random() * GUESS_MAX) + 1;
Screen.out.println("The random number is " + guess);
Screen.out.println(" ");
}
static void SortResponse()
{
char response;
response = Keybd.in.readChar();
switch(response)
{
case 'a':
printName();
return;
case 'b':
printTutorial();
return;
case 'c':
NumCount();
return;
case 'd':
RandomNumber();
return;
case 'q':
Screen.out.println("OK. Bye!");
break;
default:
Screen.out.println("You haven't selected a valid option. Try again.");
Screen.out.println(" ");
return;
}
}
public static void main(String[]args)
{
char response;
do
{
printMenu();
SortResponse();
}while(response != 'q');
if(response == 'q')
Screen.out.println("OK. Bye!");
}
}

New Topic/Question
Reply




MultiQuote





|