import javax.swing.JOptionPane;
public class filmboxoffice {
public static void main (String[] args)
{
String [] filmname = new String [4];
String [] filmtime = new String [4];
String name;
String time;
int result;
for(int i=1; i<5; i++)
{
name = JOptionPane.showInputDialog("What is the film on Screen "+i+" today?");
result = i;
time = JOptionPane.showInputDialog("What time does it start?");
}
JOptionPane.showMessageDialog(null,"Screen "+ result +": "+" "+filmtime [time]);
}
}
错误堆栈:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Type mismatch: cannot convert from String to int
at filmboxoffice.main(filmboxoffice.java:22)
最佳答案
问题在于这个表达式:
filmtime [time]
filmtime
是 array - 数组总是通过索引访问,索引是 int
。 time
是 String
类型的变量,因此不能将其用作数组索引。
也许您想要 Map<String, String>
,或者更好的是使用 Joda Time ,一个Map<LocalTime, Film>
?
实际上,再读一遍,看起来应该是这样的:
for (int i = 0; i < 4; i++) {
JOptionPane.showMessageDialog(null,"Screen "+ i +": "+" "+filmtime[i]);
}
...当然是在填充之后。 (当前,在提示用户输入 time
的值后,您忽略了该值。)
请注意,Java 中的数组从 0 开始,因此 new String[4]
具有有效索引 0、1、2 和 3,因此您当前的循环将引发异常 - 并且不清楚 result
是什么。应该是,因为在当前循环结束时它总是为 4。
关于java - 为什么它告诉我将时间转换为 int ?时间的格式是XX :XX, 这个变量应该是什么类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13035898/