JAVA; 如果没有,预计不会发表其他声明
嗨,我想知道我的代码有什么问题,我得到标题中说明的错误。 这有什么问题? 提前致谢。 为什么我需要这么多细节,我觉得我已经很好地描述了它。
import java.util.Scanner; public class CombinationLock { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Please enter three uppercase letters."); System.out.println("Hit enter after each letter."); String str1 = in.nextLine(); String str2 = in.nextLine(); String str3 = in.nextLine(); System.out.println("Would you like to open the lock?"); if(Yes) Scanner lol = new Scanner(System.in); System.out.print("Please enter the combination for this lock."); System.out.println("Hit enter after each letter"); String str4 = lol.nextLine(); String str5 = lol.nextLine(); String str6 = lol.nextLine(); if(str4.equals(str1)) if (str5.equals(str2)) if(str6.equals(str3)) System.out.println("Congratulations you have unlocked the lock :)"); else System.out.println("Sorry the combination you have input is not correct :/"); else System.out.println("Sorry the combination you have input is not correct :/"); else System.out.println("Sorry the combination you have input is not correct :/"); else System.out.println("This lock has been locked enter code here with the letters that you have just input."); } }
你必须使用{}
!
那里 :
if(Yes) Scanner lol = new Scanner(System.in); System.out.print("Please enter the combination for this lock.");
这个if语句仅适用于此行Scanner lol = new Scanner(System.in);
所以最后一个else语句没有连接到任何if语句。
这是你应该如何使用{}
来获得正确的输出并明确什么是:
Scanner in = new Scanner(System.in); System.out.print("Please enter three uppercase letters."); System.out.println("Hit enter after each letter."); String str1 = in.nextLine(); String str2 = in.nextLine(); String str3 = in.nextLine(); System.out.println("Would you like to open the lock?"); if (Yes) { Scanner lol = new Scanner(System.in); System.out.print("Please enter the combination for this lock."); System.out.println("Hit enter after each letter"); String str4 = lol.nextLine(); String str5 = lol.nextLine(); String str6 = lol.nextLine(); if (str4.equals(str1)) { if (str5.equals(str2)) { if (str6.equals(str3)) { System.out.println("Congratulations you have unlocked the lock :)"); } else { System.out.println("Sorry the combination you have input is not correct :/"); } } else { System.out.println("Sorry the combination you have input is not correct :/"); } } else { System.out.println("Sorry the combination you have input is not correct :/"); } } else { System.out.println("This lock has been locked enter code here with the letters that you have just input."); }
你错过了一个花括号{
在第一个之后if (Yes)
if(Yes) { Scanner lol = new Scanner(System.in); System.out.print("Please enter the combination for this lock."); System.out.println("Hit enter after each letter"); String str4 = lol.nextLine(); String str5 = lol.nextLine(); String str6 = lol.nextLine(); if(str4.equals(str1)) if (str5.equals(str2)) if(str6.equals(str3)) System.out.println("Congratulations you have unlocked the lock :)"); else System.out.println("Sorry the combination you have input is not correct :/"); else System.out.println("Sorry the combination you have input is not correct :/"); else System.out.println("Sorry the combination you have input is not correct :/"); } else System.out.println("This lock has been locked enter code here with the letters that you have just input.");
好吧,这是我的看法(摘自我以前的评论):
我为每个 if语句使用大括号(快速前/后条件单行if(..)throw …语句除外)。 我认为自己是一个经验丰富的开发人员,我也建议初学者在每个 if语句中使用大括号。
遵循这样的建议会阻止语法错误开始;-)
无论如何,随后克朗彻说:
..“深筑巢”一般被认为是不好的做法。 如果你可以写相同的代码“浅”通常是首选。
所以,我想谈谈if-else语句的最终级联。 实际上,向它们添加大括号确实不会使代码更容易阅读。 但是,是什么让代码更容易调整方法,使其变得“更浅”。 (它还会删除一些冗余行。)
// .. if(Yes) { // .. if(str4.equals(str1) && str5.equals(str2) && str6.equals(str3)) { System.out.println("Congratulations you have unlocked the lock :)"); } else { System.out.println("Sorry the combination you have input is not correct :/"); } } else { System.out.println("This lock has been locked enter code here with the letters.."); }
每个if块,如果所述块包含多个语句,则应该用大括号括起来。 如果是单一陈述,仍然建议使用。 (注意:嵌套if可以算作单个语句!这就是为什么你通常应该总是使用花括号,无论它们是否在技术上需要)你可以在每个if条件中嵌套额外的if块。
if(someCondition) { //do some stuff if(someOtherCondition) { //do some additional stuff } } else { //do some other stuff }
从逻辑上讲,你在if之后缺少大括号。 但即使你修复它也没有名为Yes
变量。 如果要始终运行它,请使用true
if(true) {