Java 在while循环中将数组重置回数组[0]
我正在尝试将while循环始终重置为数组[0]。我正在努力做到这一点,这样我就可以说出我最喜欢的课程是什么,如果需要的话,在我选择了第一个课程后改变主意。目前,代码只允许我输出数组[0],然后是[1],然后是[2]或[2]>[3]或[1]>[3],但不允许输出[2]>[1]或[3]>[1]。谢谢我正在使用Java。编辑**如果不清楚我说的是第二个while循环Java 在while循环中将数组重置回数组[0],java,arrays,while-loop,Java,Arrays,While Loop,我正在尝试将while循环始终重置为数组[0]。我正在努力做到这一点,这样我就可以说出我最喜欢的课程是什么,如果需要的话,在我选择了第一个课程后改变主意。目前,代码只允许我输出数组[0],然后是[1],然后是[2]或[2]>[3]或[1]>[3],但不允许输出[2]>[1]或[3]>[1]。谢谢我正在使用Java。编辑**如果不清楚我说的是第二个while循环 import java.util.*; public class Hobby1 { public static void m
import java.util.*;
public class Hobby1 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int numClass;
int a;
int b;
String j;
System.out.println("How many classes");
numClass = scan.nextInt();
j = scan.nextLine();
String[] Class = new String[numClass];
a = 0;
while (a < numClass) {
System.out.println("What is class " + a + "?");
Class[a] = scan.nextLine();
a++;
}
System.out.println("Which class is most important");
String input = scan.nextLine();
b = 0;
boolean be = true;
while (be == true) {
if (input.contains(Class[b])) {
System.out.println("The class " + Class[b] + " is most important");
input = scan.nextLine();
}
b++;
}
}
}
import java.util.*;
公共阶级爱好1{
公共静态void main(字符串[]args){
扫描仪扫描=新扫描仪(System.in);
int numClass;
INTA;
int b;
字符串j;
System.out.println(“多少类”);
numClass=scan.nextInt();
j=scan.nextLine();
字符串[]类=新字符串[numClass];
a=0;
while(a
您在这里几乎没有问题:
- while(be==true)-您可以使用while(be)/be是布尔值
- 你从不把be=false,因此将有一个无限循环
- 如何迭代数组类以与数组的每个值进行比较?您只需按增量顺序b检查一次,就需要循环所有数组,而不仅仅是b上的当前元素
检查此项并尝试修复代码。感谢您提供的信息,我知道我有一个无限循环,它是一个用于无限量输入的测试组。
for(String currentClass: Class){
if (currentClass.equals(input)) {
System.out.println("The class " + currentClass + " is most important");
}
}