我是编码的新手,所以请耐心等待。在我的代码中,我在for循环中遇到了类型错误的非法启动(位于student类中)。我该如何解决这个错误?
在我的for循环中,我希望我的数组成绩有10个值(已定义),这些值将是60到99之间的随机值。在我看来,我认为这部分是正确的,并且在整个I中使用了正确的方法。所以,如果有人能看到我出错的地方,我将非常感激:)
import javax.swing.*;
public class StudentBase
{
public static void main(String[] args){
StudentBase app = new StudentBase();
app.go();
}
void go(){
//this is the main routine
JOptionPane.showMessageDialog(null, "Welcome!" + '\n');
String n = JOptionPane.showInputDialog("Type in your name");
JOptionPane.showMessageDialog(null, "Press ok if your name is" + " " + n);
String h = JOptionPane.showInputDialog("What is your height? (in meters: eg - 1.80)");
JOptionPane.showMessageDialog(null, "Press ok if your height is" + " " + h + " " + "meters");
JOptionPane.showMessageDialog(null, "Introduction to student class:" + " " + "Hello! My name is" + " " + n + " " + "and I am" + " " + h + " " + "meters tall.");
String students[] = new String [20];
JOptionPane.showMessageDialog(null, students);
}
}
//this is where Student Class is defined
class Student{
//add variables, constructors and methods here
String name = "";
String height = "";
int grades [] =new int [10];
for(i = 0; i < grades.length; i++)
{
grades[i] = rand(60,99);
}
public Student(String n, String h, int g[])
{
name = n;
height = h;
grades = g;
}
}
答案 0 :(得分:0)
导致该特定错误的问题是,您尝试运行for循环而不将其作为任何方法或构造函数的一部分。你必须先包含它。此外,您需要在循环中声明i
的类型,否则您也会遇到不同的错误。
class Student{
//add variables, constructors and methods here
String name = "";
String height = "";
int grades [] =new int [10];
public void initGrades()
{
for(int i = 0; i < grades.length; i++)
{
grades[i] = rand(60,99);
}
}
public Student(String n, String h, int g[])
{
name = n;
height = h;
grades = g;
initGrades();
}
}