Java 不读取用户输入
我正试图编写一个程序,要求输入1、2或q。1将请求输入,打印输入,然后打印调整后的输入。2将执行与1完全相反的操作,请求输入,然后打印解码版本。q将退出该计划:Java 不读取用户输入,java,Java,我正试图编写一个程序,要求输入1、2或q。1将请求输入,打印输入,然后打印调整后的输入。2将执行与1完全相反的操作,请求输入,然后打印解码版本。q将退出该计划: import java.io.BufferedReader; import java.io.InputStreamReader; public class StringyString { public static void main (String[] args) throws java.io.IOException{
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class StringyString {
public static void main (String[] args) throws java.io.IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str;
//int n;
boolean whatever = true;
while(whatever){
System.out.println("If you would like to type something w/o using the formula, type 1");
System.out.println("If you would like to type something using the formula, type 2");
System.out.println("If you would like to quit, type q");
str = br.readLine();
if(str == "1"){
System.out.println("Type something below and press enter to end your input");
String str1 = br.readLine();
System.out.println("Your word is " + str1);
System.out.println("Your altered word is: ");
String str2 = str1.replace('A', 'Q');
String str3 = str2.replace('E', 'W');
String str4 = str3.replace('I', 'L');
String str5 = str4.replace('O', 'P');
String str6 = str5.replace('U', 'G');
String str7 = str6.replace('1', 'J');
//str = s7r.replace(‘J uuuuu’, ‘1’);
String str8 = str7.replace('5', 'S');
String str9 = str8.replace('S', '5');
String str10 = str9.replace('8', 'B');
String str11 = str10.replace('B', '8');
System.out.println(str11);
}
if(str == "2"){
System.out.println("Type something below and type '/.' to end your input");
str = br.readLine();
//n = Integer.parseInt(str);
System.out.println("Your word is " + str);
System.out.println("Your decoded word is: ");
str = str.replace('Q', 'A');
str = str.replace('W', 'E');
str = str.replace('L', 'I');
str = str.replace('P', 'O');
str = str.replace('G', 'U');
str = str.replace('1', 'J');
str = str.replace('J', '1');
str = str.replace('5', 'S');
str = str.replace('S', '5');
str = str.replace('8', 'B');
str = str.replace('B', '8');
System.out.println(str);
}
while(str != "q"){
whatever = true;
}
// System.out.println(str);
}
}
}
但是,每次我运行程序时,它都不会删除我的输入。
输出:
如果要键入不带公式的内容,请键入1
如果要使用公式键入内容,请键入2
如果要退出,请键入q
[输入]
[什么都没发生,但你可以继续输入]
谁能帮我解决这个问题???TIA抱歉,如果这是错误的代码格式您的问题就在这一部分:
while(str != "q")
{
whatever = true;
}
在这里,您已经进入了一个无限循环,系统没有从您那里读取任何值——只是永远循环。另一个问题是,即使str
是“q”,您也从未将str
设置为false
来中断while(无论如何)
循环。请尝试改用以下代码:
if(str.equals("q"))
{
whatever = false;
}
正如John3136所指出的,您不应该使用=
操作符来比较Java中的字符串,而应该使用str.equals(value)
对于进一步的更改,请不要命名变量
任何内容
;名称不能告诉您变量正在做什么,这将使调试程序变得更加困难。您的代码非常长而且很糟糕……但是您可以试试这个,我做了最小的更改
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class test {
public static void main (String[] args) throws java.io.IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str;
//int n;
boolean whatever = true;
while(whatever){
System.out.println("If you would like to type something w/o using the formula, type 1");
System.out.println("If you would like to type something using the formula, type 2");
System.out.println("If you would like to quit, type 0");
str = br.readLine();
if(Integer.parseInt(str)==1){
System.out.println("Type something below and press enter to end your input");
String str1 = br.readLine();
System.out.println("Your word is " + str1);
System.out.println("Your altered word is: ");
str1 = str1.replace('A', 'Q');
str1 = str1.replace('E', 'W');
str1 = str1.replace('I', 'L');
str1 = str1.replace('O', 'P');
str1 = str1.replace('U', 'G');
str1 = str1.replace('1', 'J');
//str = s7r.replace(‘J uuuuu’, ‘1’);
str1 = str1.replace('5', 'S');
str1 = str1.replace('S', '5');
str1 = str1.replace('8', 'B');
str1 = str1.replace('B', '8');
System.out.println(str1);
}
else if(Integer.parseInt(str)==2){
System.out.println("Type something below and type '/.' to end your input");
str = br.readLine();
System.out.println("Your word is " + str);
System.out.println("Your decoded word is: ");
str= str.replace('Q', 'A');
str= str.replace('W', 'E');
str= str.replace('L', 'I');
str= str.replace('P', 'O');
str= str.replace('G', 'U');
str= str.replace('1', 'J');
str= str.replace('J', '1');
str= str.replace('5', 'S');
str= str.replace('S', '5');
str= str.replace('8', 'B');
str= str.replace('B', '8');
System.out.println(str);
}
else if(Integer.parseInt(str)==0){
System.out.println("Quit");
whatever = true;
break;
}
else{
System.out.println("Wrong Input Try Again!");
}
}
}
}
您的问题似乎是您处于
状态,而(无论什么)
,并且您从未将无论什么设置为false
。将while(str!=“q”){whatever=true;}
更改为if(str==“q”){whatever=false;}。之所以可以继续输入值,是因为在这里进行第二个无限的循环。在java中将字符串与==
进行比较也是不正确的。使用if(str.equals(“1”))
while(str!=“q”){
nope。再一次使用String
比较问题。谢谢你们,这真的很有帮助。我仍在学习我的编码技能,但你们的回答让我找到了正确的方向。谢谢!谢谢雅各布。我一直在这里帮助你们。