String JavaEclipse控制台在比较用户输入值和字符串值之前终止程序
所以我为我弟弟做这个节目,我遇到了一个问题。假设程序请求用户输入,然后通过一系列“if”语句将其与多个字符串值进行比较。取而代之的是用户提供输入,然后程序立即终止。这件事我已经做了好几个小时了,我开始对此感到非常生气。以下是我目前键入的代码:String JavaEclipse控制台在比较用户输入值和字符串值之前终止程序,string,input,compare,String,Input,Compare,所以我为我弟弟做这个节目,我遇到了一个问题。假设程序请求用户输入,然后通过一系列“if”语句将其与多个字符串值进行比较。取而代之的是用户提供输入,然后程序立即终止。这件事我已经做了好几个小时了,我开始对此感到非常生气。以下是我目前键入的代码: package package1; import java.util.Scanner; public class Psychic_Calculator { @SuppressWarnings("resource") public static void
package package1;
import java.util.Scanner;
public class Psychic_Calculator {
@SuppressWarnings("resource")
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Hello user, please type your name below:");
String a = scan.nextLine();
System.out.println("Welcome " + a + ", think of a number. Once you have your number, type 'okay' below!");
String b = scan.nextLine();
if (b == "okay"){
System.out.println("Now, add '11' to your number and type 'okay' below.");
}
else if (b == "Okay"){
System.out.println("Please don't capitalize 'okay', try typing it again!");
String c = scan.nextLine();
if (c == "okay"){
System.out.println("Now, add '11' to your number and type 'okay' below.");
String d = scan.nextLine();
if (d == "okay"){
System.out.println("Now, add '2' to your new number, then type 'okay' below.");
String e = scan.nextLine();
if (e == "okay"){
System.out.println("Now, subtract your original number from your new number, then type 'okay' below.");
String f = scan.nextLine();
if (f == "okay"){
System.out.println("Your new number is '13'. Don't even try denying it.");
}
}
}
}
if (c == "Okay"){
System.out.println("I already told you not to capitalize 'okay', try typing it again, you idiot!");
String g = scan.nextLine();
if (g == "okay"){
System.out.println("Now, add '11' to your number and type 'okay' below.");
String h = scan.nextLine();
if (h == "okay"){
System.out.println("Now, add '2' to your new number, then type 'okay' below.");
String i = scan.nextLine();
if (i == "okay"){
System.out.println("Now, subtract your original number from your new number, then type 'okay' below.");
String j = scan.nextLine();
if (j == "okay"){
System.out.println("Your new number is '13'. Don't even try denying it.");
}
}
}
}
}
if (c != "okay") {
while (c != "okay") {
System.out.println("Do you even know how to spell 'okay'?" + "'" + c + "' does not spell 'okay', you moron! Try typing 'okay' again.");
String n = scan.nextLine();
if (n == "okay"){
System.out.println("Finally, you learned how to spell 'okay'. Your vocabulary is now one word larger, you're welcome. Now, please add '11' to your number and then type 'okay'(correctly this time).");
String k = scan.nextLine();
if (k == "okay"){
System.out.println("Now, add '2' to your new number, then type 'okay' below.");
String l = scan.nextLine();
if (l == "okay"){
System.out.println("Now, subtract your original number from your new number, then type 'okay' below.");
String m = scan.nextLine();
if (m == "okay"){
System.out.println("Your new number is '13'. Don't even try denying it.");
}
}
}
}
else {
System.out.println(a + ", " + "you have failed to type 'okay' too many times! You have no idea how to spell 'okay' you electricutin' motherboarder! Go shove your face in a pile of computer chips and grow a pair!");
System.out.println("(of RAM cartriges...I meant to say RAM cartriges).");
}
}
}
}
}
}
问题是如何比较字符串。将
b==“好的”
更改为b.equals(“好的”)
将所有==
比较更改为.equals()
或.equalsIgnoreCase()
。
对于否定,使用(!(c.equals(“ok”))
在Java中,==
将按值比较基本类型,但将按内存地址比较对象。换句话说,当你说b==“好的”
时,它不进行值比较,而是检查这两个对象是否指向同一内存地址,这当然是错误的
更新:关于您编写此程序的方式,只介绍几件事
1) 您正在不必要地创建大量字符串对象。在绝对需要另一个字符串对象之前,最好使用一个字符串对象。这适用于您正在使用的任何对象。尽量不要不必要地分配对象。
2) 您可以定义一个指令数组,而不是所有的if语句,例如,并压缩代码:
String [] instr = {"Add 2", "Add 11", "Subtract Original"};//array of directions
String [] invalidResp = {"Wrong", "You can't spell", "Try Again", "No"};//array of responses to print if user doesnt type 'okay' properly
int instrCounter = 0;//you don't really need this but it helps with readability
String userInput = "";//only string object you'll need =)
while (instrCounter < instr.length){//I couldve just as easily used a for loop instead.
userInput = scan.nextLine();
while (!userInput.equals("okay")){
userInput = scan.nextLine();
System.out.println(invalidResp[(int) (Math.random()*invalidResp.length)]);
//will print a random invalid response from the invalidResp array
System.out.println(userInput + " is not how you spell okay");
}//end while
System.out.println("Great, now, " + instr[instrCounter]);
instrCounter++;
}//end outer while
String[]instr={“添加2”、“添加11”、“减去原始”}//方向数组
字符串[]invalidResp={“错误”、“无法拼写”、“重试”、“否”}//如果用户未正确键入“OK”,则打印的响应数组
int instrCounter=0//你并不真的需要它,但它有助于提高可读性
字符串userInput=“”//您需要的唯一字符串对象=)
而(instrCounter
请记住,当您编写代码时,您希望它具有相当的通用性和灵活性。按照我编写代码的方式,我可以添加到instr数组中,或者添加更多无效响应,并且我不会不必要地创建对象。
就像我在内联评论中所说的,我可以很容易地在外循环中使用for循环,但是为了可读性和确保您理解我在做什么,我使用了while循环,因为我相信它们更直观 游戏:
(跑步时){
System.out.println(“-------------------------------”
继续游戏
//命名while循环,然后您可以中断。也许可以尝试在最后输入另一个输入,上面写着“按Enter键退出程序”您应该让控制台等待。但是,在发布之前,一些研究对您很有用。啊,另一件事..在Java中,您需要使用String.equals进行字符串比较..而不是相等运算符也就是“b”结尾可能包含换行符。因此,您可能希望使用Apache Commons StringUtils或regexWow清理它,这对我真的很有帮助!我非常感谢您的帮助!但是,我还有两个问题。是否有办法在特定点结束程序,以及我的代码行System.out.println(“你知道怎么拼写‘okay’?“+”“+c+””不拼写‘okay’,你这个笨蛋!再试试键入‘okay’);,一直给‘c’用户设置的第一个值。有没有办法将该值设置为用户每次循环时提供的值?我做了一些编辑。你可以使用System.exit(0)停止任何java程序;但是你为什么要这么做呢?再次感谢你!我之所以需要停止这个程序,是因为在程序结束后,它会重复while循环。哇,这些技巧真的很有用,如果没有你的帮助,我会完全不知所措!你在回答中发布的格式真的帮助了我!我试着投票支持它,但我没有足够的声望分享一些有价值的东西