This is a code from a Decision Support System I'm trying to make. Basically the question is whether you want breakfast or not. StdIn.readChar()
is used to read a char you insert in the console. I am 100% sure it's correct.
Obviously there is a main method missing, I believe it is clear to understand without it, but if you need it I can paste it in later.
Now my Question:
If I run this program I am asked in the main whether I am hungry or not. So I type j (in German 'ja' is 'yes') and the program jumps into the Breakfast method. But then I am asked if I want breakfast but there is no time to insert a char. It directly jumps into the Default method. I have used the print command to see on which point, as you can see. Did I do something wrong with the switch-case selection?
My console says this:
Are you hungry?
(I typed) j
Do you want breakfast?
2 (this is due to the testing println command)
This insert is invalid
Do you want breakfast?
And the strange point is, at this moment I am able to write my answer. I have the same problem at the question "Do you like it to be warm?", but of course I can never answer because I need to answer the first question (Do you want breakfast) again.
Can you help me?
Do I need to delete the do-while-loop?
Am I doing so wrong I need to delete everything?
public static void Breakfast(){
int a = 0;
do {
System.out.println("Do you want Breakfast?");
char y = StdIn.readChar(); //read Char from console
switch (y) {
case 'j':
System.out.println("Do you like it to be warm?");
y=StdIn.readChar();
switch (y) {
case 'j':
System.out.println("Eggs with Bacon");
break;
case 'n':
System.out.println("Cereal");
break;
default:
System.out.println("1");
a=Default();
}
break;
case 'n':
Mittagessen();
break;
default:
System.out.println("2");
a=Default();
}
} while (a!=0);
}
public static int Default (){
System.out.println("This insert is invalid");
return 1;
}