I was wondering if you can help me out with this code, I am learning on my own mostly from a book.
The code runs fine but in the 2nd do/while statement I am curious about the condition: (ch=='\n') or (ch=='\r'); Why would ch be equal to a carrage return or line feed? I found out that line feed and carriage returns occur when a keyboard character is input, but do they occur continuously? Why not use the condition: while(ch != 'K'); this condition should run the loop until 'K' is selected on the keyboard, shouldn't it?
Thanks in advance-Farmer
public class HSGuess4 {
public static void main(String[] args) throws java.io.IOException{
char ch, answer = 'K';
do{
System.out.println("I'm thinkning of a letter between A and Z ");
System.out.println("Can you guess it: ");
//read a letter, but skip cr/lf (carrage return and line feed)
do{
ch = (char) System.in.read(); // get char from keyboard command
} while(ch == '\n'| ch == '\r');
if(ch == answer) System.out.println("**Right**");
else{
System.out.println("Sorry you're ");
if(ch < answer) System.out.println("too low");
else System.out.println("too high");
System.out.println("Try again!\n");
}
} while(answer !=ch);
}
}