简体   繁体   English

如何在Java中分配数组中的元素数?

[英]How to assign the number of elements in an array in Java?

I have a Java exercise here wherein you'll create a program that will allow you to enter your academic grades into an array of floats. 我在这里有一个Java练习,您将在其中创建一个程序,该程序将允许您将学术成绩输入一系列的花车。

At the beginning of the program, the application should prompt the user for the total number of grades you intend to enter. 在程序开始时,应用程序应提示用户您要输入的总成绩数。 After entering the grades, you will then calculate for the average of those grades. 输入成绩后,您将计算这些成绩的平均值。 The code below is what I have coded so far. 下面的代码是我到目前为止编写的代码。

import java.io.*;

public class Proj4exe1
    {
    public static void main(String args[]) throws IOException
    {
        BufferedReader dataIn = new BufferedReader(new  InputStreamReader(System.in));
        String strSize, strGrades;
        int laki = 100;
        int totalGrades = 0;
        float gradeAverage = 0;
        float[] grades = new float[laki];

        System.out.print("How many grades will you enter?");
        strSize = dataIn.readLine();
        laki = Integer.parseInt(strSize);


        for(int i=0; i<laki; i++)
    {
        System.out.print("Please enter grade # " + (i+1) + ": ");
        strGrades = dataIn.readLine();
        grades[i] = Float.parseFloat(strGrades);
        totalGrades += grades[i];
    }           
        gradeAverage = totalGrades / laki;

        System.out.println("Your grades average is " + gradeAverage);
    }
}

I have tried int[] grades = new int[laki.length] as the number of grades to input to the program but this results in a compile error "int cannot be dereferenced" so I just initialized int laki to 100 or I could have done int[] grades = new int[100] to hold 100 integers then delete int laki . 我已经尝试过将int[] grades = new int[laki.length]作为要输入到程序的年级数,但这会导致编译错误“无法取消引用int”,因此我只将int laki初始化为100,否则我可以done int[] grades = new int[100]以容纳100个整数,然后删除int laki

I didn't include a try and catch block here because I already did one and this is the only part of the program that I still don't know what to do appropriately. 我没有在此处包括try and catch块,因为我已经做过一个了,这是程序的唯一部分,我仍然不知道该怎么做。

You just need to reorganise your code a bit: 您只需要重新组织一下代码:

 int totalGrades = 0;
 float gradeAverage = 0;

 System.out.print("How many grades will you enter?");
 strSize = dataIn.readLine();
 laki = Integer.parseInt(strSize);
 float[] grades = new float[laki];

You were trying to get .length from an int which is wrong. 您试图从错误的int获取.length。 Instead what this does is to convert your input to an int (laki) and then uses that to initialize the array 取而代之的是,将输入转换为int(laki),然后使用该值初始化数组

You can create the vector after getting the numer of grades. 您可以在获得分数编号后创建矢量。 Try this instead: 尝试以下方法:

    int laki;

    System.out.print("How many grades will you enter?");
    strSize = dataIn.readLine();
    laki = Integer.parseInt(strSize);

    float[] grades = new float[laki];

By the way, why do you read the number of grades as a String and then convert it to int? 顺便说一句,为什么您要读取一个分数作为String并将其转换为int? Read it like int instead. 像int一样阅读它。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Proj4exe1
    {
    public static void main(String args[]) throws IOException
    {
        BufferedReader dataIn = new BufferedReader(new  InputStreamReader(System.in));
        String strSize, strGrades;
        int laki = 100;
        int totalGrades = 0;
        float gradeAverage = 0;
        float[] grades = new float[laki];

        System.out.print("How many grades will you enter?");
        strSize = dataIn.readLine();
        laki = Integer.parseInt(strSize);
        grades = new float[laki]; // reassign <-----------------------

        for(int i=0; i<laki; i++)
    {
        System.out.print("Please enter grade # " + (i+1) + ": ");
        strGrades = dataIn.readLine();
        grades[i] = Float.parseFloat(strGrades);
        totalGrades += grades[i];
    }           
        gradeAverage = totalGrades / laki;

        System.out.println("Your grades average is " + gradeAverage);
    }
}

The problem is that you're calling a method on a primitive type whereas it doesn't have any. 问题在于您要在原始类型上调用方法,而该方法没有任何方法。 First you have to read the number from stdin into the laki variable 首先,您必须将stdin中的数字读入laki变量

int laki = Integer.parseInt(dataIn.readline());

then you can do 那你就可以

float[] grades = new float[laki];

@user3328588: 1. you program will not give the correct grand average of the grades as you are not typecasting average into float. @ user3328588:1.您的程序不会给出正确的成绩总和,因为您没有将平均值转换为浮点数。 So it will always give you the round figure. 因此,它将始终为您提供圆角图形。

gradeAverage = totalGrades / laki;

the above statement should be like this : 上面的声明应该是这样的:

gradeAverage = (float) totalGrades / laki;
  1. Your code is not optimized and the initialization of the array is not at the correct place. 您的代码未优化,数组的初始化不在正确的位置。 (int[] grades)

3.Do not you * while importing package while use import for importing multiple classes. 3.在导入包时不要* ,而使用import来导入多个类。

The whole code with optimized version and the correct logic is attached below: 下面附有具有优化版本和正确逻辑的整个代码:

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader;

public class Proj4exe1 {
    public static void main(String args[]) throws IOException {
        BufferedReader dataIn = new BufferedReader(new InputStreamReader(
                System.in));

        int totalGrades = 0;
        float gradeAverage = 0;

        System.out.print("How many grades will you enter?");
        int laki = Integer.parseInt(dataIn.readLine());
        float[] grades = new float[laki]; // reassign <-----------------------

        int count = 0;
        while (laki > 0) {
            System.out.print("Please enter grade # " + (count + 1) + ": ");
            grades[count] = Float.parseFloat(dataIn.readLine());
            totalGrades += grades[count];
            laki--;
            count++;
        }
        gradeAverage = (float) totalGrades / count;

        System.out.println("Your grades average is " + gradeAverage);
    }
}

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

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