简体   繁体   English

我需要在这里导入一些东西吗?

[英]Do I need to import something here?

I'm a beginner at Java and I'm creating a program that stores the names and weight of students in two different arrays after taking input from the user 30 times.我是 Java 的初学者,我正在创建一个程序,在接受用户 30 次输入后,将学生的姓名和权重存储在两个不同的数组中。 Here's my code:这是我的代码:

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();}
        }

    }

However when I debug it, I get the message:但是,当我调试它时,我收到消息:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: at bodymass.main(bodymass.java:5)线程“main”中的异常 java.lang.Error:未解决的编译问题:在 bodymass.main(bodymass.java:5)

What does it mean?这是什么意思? I would be grateful if somebody guides me through this.如果有人指导我完成此操作,我将不胜感激。

I have tried it for 2 students and ensure that your java class having no errors.我已经为 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();
            }
    }
}

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

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