Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使多维数组与字符串输入循环?_Java - Fatal编程技术网

Java 如何使多维数组与字符串输入循环?

Java 如何使多维数组与字符串输入循环?,java,Java,基本上,我正在制作一个程序,从多维数组中读取数据以显示相应的信息。我想做的是使while循环继续告诉我我输入了错误的类ID,直到您输入了正确的类ID 做 { System.out.println("Please enter the name of your course to display it's information"); name = input.nextLine(); for(int x = 0; x <= classes.length; ++x)

基本上,我正在制作一个程序,从多维数组中读取数据以显示相应的信息。我想做的是使while循环继续告诉我我输入了错误的类ID,直到您输入了正确的类ID

做 {

    System.out.println("Please enter the name of your course to display it's information");
    name = input.nextLine();

    for(int x = 0; x <= classes.length; ++x)
    {

        if(name.equals(classes[x][0]))
        {

            i = true;

            System.out.println("Course info: " + classes [x][0]);
            System.out.println(classes[x][1]);
            System.out.println(classes[x][2]);
            x = classes.length;


        }

        else{

            System.out.println("Wrong course id");
            i = false;
            input.next();

            }   
    }
    }

    while (!(i));
    System.out.println("This is the end of the program!");
    System.exit(0);
System.out.println(“请输入课程名称以显示其信息”);
name=input.nextLine();

对于(int x=0;x首先,尽量保持良好的命名约定。
i
对于标志变量来说是个坏名字。将其命名为
boolean found
或其他什么。它不仅可以帮助其他人阅读和理解您的代码,还可以帮助您找到必须使用的逻辑

现在,由于在
else
部分中有
input.next();
,我猜您想再次请求用户输入,直到找到某个东西。因此,a
name=input.nextLine()
再次被需要以获取新的输入。但是在您的情况下,可以完全删除
else
部分,让
在执行工作时执行

例如:

public class Classes {
    private static final String[][] CLASSES = { { "Maths", "info" }, { "History", "info" }, { "Biology", "info" } };

    public static void main(String[] args) {
        boolean found = false;
        String name;
        Scanner input = new Scanner(System.in);
        do {
            System.out.println("Please enter the name of your course to display it's information");
            name = input.nextLine();
            for (int i = 0; i < CLASSES.length; i++) {
                if (name.equals(CLASSES[i][0])) {
                    found = true;
                    System.out.println("Course info: " + CLASSES[i][0]);
                    System.out.println(CLASSES[i][1]);
//                  System.out.println(CLASSES[i][2]); //My CLASSES array, does not have 3 columns
                    break;// exit for loop
                }
            }
            if (!found)
                System.out.println("Wrong course id");
        } while (!found);
        input.close();
        System.out.println("This is the end of the program!");
    }
}
公共类{
私有静态最终字符串[][]类={{“数学”,“信息”},{“历史”,“信息”},{“生物学”,“信息”};
公共静态void main(字符串[]args){
布尔值=false;
字符串名;
扫描仪输入=新扫描仪(System.in);
做{
System.out.println(“请输入课程名称以显示其信息”);
name=input.nextLine();
for(int i=0;i
首先,尽量保持良好的命名约定。
i
对于标志变量来说是个坏名字。将其命名为
boolean found
或其他什么。它不仅可以帮助其他人阅读和理解您的代码,还可以帮助您找到必须使用的逻辑

现在,由于在
else
部分中有
input.next();
,我猜您想再次请求用户输入,直到找到某个东西。因此,a
name=input.nextLine()
再次被需要以获取新的输入。但是在您的情况下,可以完全删除
else
部分,让
在执行工作时执行

例如:

public class Classes {
    private static final String[][] CLASSES = { { "Maths", "info" }, { "History", "info" }, { "Biology", "info" } };

    public static void main(String[] args) {
        boolean found = false;
        String name;
        Scanner input = new Scanner(System.in);
        do {
            System.out.println("Please enter the name of your course to display it's information");
            name = input.nextLine();
            for (int i = 0; i < CLASSES.length; i++) {
                if (name.equals(CLASSES[i][0])) {
                    found = true;
                    System.out.println("Course info: " + CLASSES[i][0]);
                    System.out.println(CLASSES[i][1]);
//                  System.out.println(CLASSES[i][2]); //My CLASSES array, does not have 3 columns
                    break;// exit for loop
                }
            }
            if (!found)
                System.out.println("Wrong course id");
        } while (!found);
        input.close();
        System.out.println("This is the end of the program!");
    }
}
公共类{
私有静态最终字符串[][]类={{“数学”,“信息”},{“历史”,“信息”},{“生物学”,“信息”};
公共静态void main(字符串[]args){
布尔值=false;
字符串名;
扫描仪输入=新扫描仪(System.in);
做{
System.out.println(“请输入课程名称以显示其信息”);
name=input.nextLine();
for(int i=0;i
x我现在明白了,有没有什么好方法可以重写它,这样我就不会出错?
x
我尝试了一下,它似乎给了我同样的结果。当我去执行程序时,如果我给它提供了不正确的数据,它就会转到else,然后在else循环说“错误的课程id”即使我输入了正确的。这与此无关。
x我现在明白了,有没有好的方法重写它,这样我就不会出现错误?
x
我尝试了一下,它似乎给了我相同的结果。当我去执行程序时,如果我给它提供了不正确的数据,它就会转到else,然后循环其他人说“错误的课程id”,即使我输入了正确的课程id。这与此无关。