Java 当到达匹配号码时结束while循环
当这个while循环到达匹配的数字,如88、77等时,我需要结束它Java 当到达匹配号码时结束while循环,java,Java,当这个while循环到达匹配的数字,如88、77等时,我需要结束它 import java.util.Scanner; public class LabProgram { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int userNum = scan.nextInt(); if ((userNum >= 20) &&
import java.util.Scanner;
public class LabProgram {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int userNum = scan.nextInt();
if ((userNum >= 20) && (userNum <= 98)){
System.out.print(userNum + " ");
while ((userNum != 11) || (userNum != 22) || (userNum != 33) || (userNum != 44) || (userNum != 55) || (userNum != 66) || (userNum != 77) || (userNum != 88) || (userNum != 99)){
userNum = userNum - 1;
System.out.print(userNum + " ");
}
}
else{
System.out.print("Input must be 20-98");
}
System.out.print("\n");
}
}
但是,当它运行时,它永远不会离开循环,即使当数字达到while循环中匹配的数字之一时也是如此。请提供帮助。尝试在此处使用&&运算符,而不是或
import java.util.Scanner;
public class LabProgram {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int userNum = scan.nextInt();
if ((userNum >= 20) && (userNum <= 98)){
System.out.print(userNum + " ");
while (userNum != 11 && userNum != 22&& userNum != 33 && userNum != 44 && userNum != 55 && userNum != 66 && userNum != 77&& userNum != 88 && userNum != 99){
userNum = userNum - 1;
System.out.print(userNum + " ");
}
}
else{
System.out.print("Input must be 20-98");
}
System.out.print("\n");
}
}
问题就在这方面。 ifuserNum!=11 | | userNum!=22 | | userNum!=33 | | userNum!=44 | | userNum!=55 | | userNum!=66 | | userNum!=77 | | userNum!=88 | | userNum!=九十九
如果userNum是任意数字,则不能同时为11、22、33、44、55、66、77、88和99,这意味着您的程序将继续运行。正如@Carcigenicate所说,您需要和运算符。您的代码似乎在搜索11的倍数,因此我更愿意测试11的其余部分。另外,您可以使用x-而不是x=x-1。最后,一个do-while循环会使这个过程更干净一些。像
Scanner scan = new Scanner(System.in);
int userNum = scan.nextInt();
if (userNum >= 20 && userNum <= 98) {
do {
System.out.print(userNum + " ");
userNum--;
} while (userNum % 11 != 0);
System.out.println();
}
将始终为true,因为userNum不能同时为所有这些值。用| |代替&:
使用“模数”操作符可以简化此操作:
if(userNum % 11 != 0)
当while循环使用数组到达匹配的数字时,可以使用以下代码结束while循环
public class stopWhileTest{
public static void main(String arg[]) {
int[] number = {11, 22, 33, 44, 55, 66, 77, 88};
Scanner scan = new Scanner(System.in);
int userNum = scan.nextInt();
if ((userNum >= 20) && (userNum <= 98)){
System.out.print(userNum + " ");
while(!contains(number,userNum)){
userNum = userNum - 1;
System.out.print(userNum + " ");
}
}
else{
System.out.print("Input must be 20-98");
}
System.out.print("\n");
scan.close();
}
public static boolean contains(final int[] array, final int v) {
boolean result = false;
for(int i : array){
if(i == v){
result = true;
break;
}
}
return result;
}
}
希望此解决方案有效。只要将| | check替换为&&check,它就会起作用
if(userNum % 11 != 0)
public class stopWhileTest{
public static void main(String arg[]) {
int[] number = {11, 22, 33, 44, 55, 66, 77, 88};
Scanner scan = new Scanner(System.in);
int userNum = scan.nextInt();
if ((userNum >= 20) && (userNum <= 98)){
System.out.print(userNum + " ");
while(!contains(number,userNum)){
userNum = userNum - 1;
System.out.print(userNum + " ");
}
}
else{
System.out.print("Input must be 20-98");
}
System.out.print("\n");
scan.close();
}
public static boolean contains(final int[] array, final int v) {
boolean result = false;
for(int i : array){
if(i == v){
result = true;
break;
}
}
return result;
}
}