classGuess{publicstaticvoidmain(Stringargs[])throwsjava.io.IOException{for(;;){charch,ignore,answer=Random;r=newRandom();charc=(char)(r.nextInt(26)+'a');do{System.out.pri...
class Guess {
public static void main(String args[])
throws java.io.IOException {
for(;;) {
char ch, ignore, answer=Random; r = new Random();
char c = (char) (r.nextInt(26) + 'a');
do {
System.out.println("I'm thinking of a letter between A and Z.");
System.out.print("Can you guess it:");
choice = (char) System.in.read();
do {
ignore = (char) System.in.read();
} while(ignore != '\n');
} while( choice 'z' | choice != '1' )
if (choice == answer) System.out.println("Right");
else {
System.out.print("Sorry, you're ");
if(choice < answer) System.out.println("too low");
else System.out.println("too high");
System.out.println("Try again!\n");
if(choice == '1') break;
}
} while(answer != choice);
}
}
展开