Java 虽然循环在不同的项目中工作方式不同,但代码结构相同?
我编写了以下代码:Java 虽然循环在不同的项目中工作方式不同,但代码结构相同?,java,while-loop,Java,While Loop,我编写了以下代码: while (choice > 2 && count <= 2) { System.out.println("The number you have typed is invalid. Please try again."); choice = s.nextInt(); count++; } if (choice == 1 && count <= 2) { System.out.println
while (choice > 2 && count <= 2) {
System.out.println("The number you have typed is invalid. Please try again.");
choice = s.nextInt();
count++;
}
if (choice == 1 && count <= 2) {
System.out.println("You have chosen teacher's information.");
t.information();
} else if (choice == 2 && count <= 2) {
System.out.println("You have chosen student's information.");
st.info();
} else {
System.out.println("You have been blocked.");
}
while(choice>2&&counta | | b&c
的意思是a | |(b&c)
,而我相信你的意思是(a | b)&c
请参阅:|
和&
的优先级不相等
因此,您的循环应该是:
while ((!rusern.equalsIgnoreCase(username) || !rpass.equals(password)) && attempts <= 2) {
while(!rusern.equalsIgnoreCase(username)| | | rpass.equals(password))和尝试a | | | b和&c
的意思是a | | b和&c
请参阅:|
和&
的优先级不相等
因此,您的循环应该是:
while ((!rusern.equalsIgnoreCase(username) || !rpass.equals(password)) && attempts <= 2) {
while(!rusern.equalsIgnoreCase(用户名)| | |!rpass.equals(密码))&&尝试检查用户名和密码是否正确的逻辑应该在while方法中。在if
语句中,一旦用户输入了正确的凭据或尝试次数超过2次,您将在if
语句中放置一个break;
以退出循环手动循环您的while
循环的条件也可以是while(尝试检查用户名和密码是否正确的逻辑应该在while方法中。在if
语句中,您将在用户输入正确凭据或尝试次数超过2次后放置break;
以退出循环。并且,由于您将break
退出循环手动循环你的while
循环的条件也可以是while(尝试@alison如果答案解决了你的问题,不要忘记接受它。(答案投票旁边的绿色复选标记)它会给你和回答问题的人荣誉点数,并表明答案是正确的correct@GBlodgett哦,好吧,我是stackoverflow的新手,所以我不知道我应该这么做。谢谢你。@alison别担心!欢迎来到stack overflow!@alison如果有答案解决了你的问题,别忘了接受它。(答案投票旁边的绿色复选标记)它将为您和回答问题的人提供声誉分数,并表明答案是正确的correct@GBlodgett哦,好吧,我是stackoverflow的新手,所以我不知道我应该这么做。谢谢。@alison别担心!欢迎来到stack overflow!