简体   繁体   English

二维数组

[英]Two Dimensional Array

I am having troubles with my "for" loop in my program.我的程序中的“for”循环有问题。 The main error I am getting is "type mismatch: cannot convert from int to Boolean."我遇到的主要错误是“类型不匹配:无法从 int 转换为 Boolean”。 All I want to do is to have the corresponding "time" element printed out.我想要做的就是打印出相应的“时间”元素。 I know that conditions need to be of type bool for "classNames[0].length", but I can't figure out how to do that.我知道“classNames[0].length”的条件必须是 bool 类型,但我不知道如何做到这一点。 Thanks!谢谢!

    import javax.swing.JOptionPane;
import java.util.Scanner;

public class arraytest {
public static void main(String[] args)
{

    int i = 0;
    String classInput;

    String[][] classNames = {
              {"CIS 280", "ACC 212", "HIS 300"},
              {"Tue 7:30", "Thu 2:30", "Fri 1:00" }
            };


classInput = JOptionPane.showInputDialog("Please input a class name: ");


for (i = 0; classNames[0].length; ++i) // problem line
   if(classNames[0][i].equals(classInput)) 
   {
       System.out.println("class time:" + classNames[1][i]);

   }
}

} }

You probably wanted to compare i to classNames[0].length to provide the necessary boolean argument.您可能想将iclassNames[0].length进行比较以提供必要的boolean参数。 Providing just classNames[0].length is just an int .仅提供classNames[0].length只是一个int

for (i = 0; i < classNames[0].length; ++i)

我想你的意思是:

for (i = 0; i < classNames[0].length; i++)
import javax.swing.JOptionPane;
import java.util.Scanner;

public class arraytest {
public static void main(String[] args)
{

    int i = 0;
    String classInput;

    String[][] classNames = {
              {"CIS 280", "ACC 212", "HIS 300"},
              {"Tue 7:30", "Thu 2:30", "Fri 1:00" }
            };


classInput = JOptionPane.showInputDialog("Please input a class name: ");


for (i = 0; i<classNames[0].length; ++i) // problem line
   if(classNames[0][i].equals(classInput)) 
   {
       System.out.println("class time:" + classNames[1][i]);

   }
}

}

this will solve your error you just forgot the i<这将解决您的错误,您只是忘记了 i<

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM