我只是在磨练我的 Java 技能,我写了这段代码来玩键盘输入并执行 while 循环。
package Chapter3;
import java.io.IOException;
public class convertCase {
public static void main(String args[]) throws IOException {
char ch = 'a';
do {
System.out.println("please type in a value: ");
ch = (char) System.in.read();
if((int) ch < 96) {
ch += 32;
int newCh = (int) ch;
System.out.println("The lowercase version is: " + (char)newCh);
}
else if((int) ch >96) {
System.out.println("You have typed in" + ch);
ch -= 32;
int newCh = (int) ch;
System.out.println("the uppercase version is: " + (char)newCh);
}
} while(ch!='.');
}
}
问题是,当我测试它时,“while 循环”在要求输入之前运行了两次,而不仅仅是一次:
please type in a value:
a
You have typed ina
the uppercase version is: A
please type in a value:
The lowercase version is: *
please type in a value:
L
The lowercase version is: l
please type in a value:
The lowercase version is: *
please type in a value:
有人可以澄清这种情况吗?