所以我正在为我的弟弟制作这个程序,我遇到了一个问题。该程序假设请求用户的输入,然后通过一系列"if"语句将其与多个字符串值进行比较。相反,发生的情况是用户提供他们的输入,然后程序立即终止。我已经在这里待了几个小时,我开始对此感到非常兴奋。这是我到目前为止键入的代码:
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 == "okay"
更改为b.equals("okay")
将所有==
比较更改为 .equals()
或 .equalsIgnoreCase()
。对于否定,请使用(!(c.equals("okay"))
在 Java 中,==
将按值比较基元类型,但会按内存地址比较对象。换句话说,当你说b == "okay"
它不做值比较时,它会检查这两个对象是否指向相同的内存地址,这当然是错误的。
更新:只是关于你编写这个程序的方式的一些事情。
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
请记住,当你编写代码时,你希望它是相当通用和灵活的。我编写代码的方式,我可以添加到 instr 数组或添加更多无效响应,并且不会不必要地创建对象。就像我在内联评论中所说的那样,我可以很容易地将 for 循环用于外部循环,但为了可读性并确保您理解我在做什么,我使用了 while 循环,因为我相信它们更直观地阅读。
游戏: 而(运行) { System.out.println("---------------------------");
继续游戏;
命名 while 循环,然后就可以中断; out。