我需要在这里导入吗?

时间:2018-08-17 16:28:16

标签: java

我是Java的初学者,我正在创建一个程序,将用户的输入输入30次后,它会将学生的姓名和体重存储在两个不同的数组中。这是我的代码:

import java.util.Scanner;
public class bodymass {

    public static void  main(String[] args) {

        Scanner scan=new Scanner(System.in);
        String[] studentName= new String[30];
        System.out.println("Please enter the Names of 30 students");
        // This "i" is your counter.
        for (int i=0; i< studentName.length; i++) {
            studentName[i]= scan.nextLine();}
        System.out.println("Please enter the Weight of 30 students");
        Scanner scan1= new Scanner(System.in);
        int[] studentWeight= new int[30];
        for(int i=0; i<studentWeight.length; i++) {
            studentWeight[i]= scan.nextInt();}
        }

    }

但是,当我调试它时,会收到消息:

  

线程“ main”中的异常java.lang.Error:未解决的编译问题:在bodymass.main(bodymass.java:5)

是什么意思?如果有人引导我完成此任务,我将不胜感激。

1 个答案:

答案 0 :(得分:-1)

我已经为2位学生尝试过此方法,并确保您的java类没有错误。

package com.test.practice;

import java.util.Scanner;

public class Bodymass {

public static void main(String[] args) {
     Scanner scan=new Scanner(System.in);
        String[] studentName= new String[2];
        System.out.println("Please enter the Names of 2 students");
        // This "i" is your counter.
        for (int i=0; i< studentName.length; i++) {
           System.out.print("Enter the student name at postion "+i+". "); 
           studentName[i]= scan.nextLine();
        }

        System.out.println("Please enter the Weight of 2 students");
        Scanner scan1= new Scanner(System.in);
        int[] studentWeight= new int[2];

        for(int i=0; i<studentWeight.length; i++) {
            System.out.print("weight of student at position "+i+" "); 
            studentWeight[i]= scan.nextInt();
            }
    }
}