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&&count
a | | 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!