我试图创建一个程序,它接收用户输入的字符并将小写字符转换为大写字母,反之亦然。每次进行转换时,更改次数都会递增,何时“。”。被输入时,程序停止询问输入。这是我下来迄今:Java大写,小写问题
import java.io.*;
class Example5
{
public static void main(String args[]) throws IOException
{
InputStreamReader inStream = new InputStreamReader (System.in);
BufferedReader stdin = new BufferedReader (inStream);
char input = '\0';
int counter = 0;
while(!(input == '.'))
{
System.out.print("Input a character. Input will continue until you enter a period : ");
input = (char)stdin.read();
if((int)input > 96 & (int)input < 123)
{
char upperInput = Character.toUpperCase(input);
System.out.println(upperInput);
counter++;
}
else if((int)input > 64 & (int)input < 91)
{
char lowerInput = Character.toLowerCase(input);
System.out.println(lowerInput);
counter++;
}
}
System.out.println("The number of changes are : " + counter);
}
}
转化率和计数器工作正常,但由于某些原因,每次输入后,该行“输入一个字符输入会继续下去,直到你进入一段:”重复每次输入后多次。任何解决这个问题的方法?我犯了什么错误?
感谢提前:)
你只需要移动是System.out.print'(“输入的文字输入会继续下去,直到你进入一段:”);上面的''的同时, '循环。 – yate
user3580294 - 谢谢!不知道这个! – user3529827
yate - yup,我改变了这种方式。但我也很好奇为什么这条线会重复多次。非常感谢! – user3529827