首先,这是一项家庭作业。我道歉。我只是机智,几乎就要到期了。
我这里有一个程序可以接受数字(我有多少个评分作业),然后输入每个作业的成绩。就像
How many graded assignments do you have? : 5
Please enter grade : 95
Please enter grade : 72
Please enter grade : 86
Please enter grade : 93
Please enter grade : 87
Your average is 86.6
我实际上有一个工作程序。问题是,我使用了整数和双精度型。在提交之前,我发现分配要求使用浮点数数组。我一生都无法使用浮动版本。我试图修改现有代码,但是无论如何,我都收到3次“从浮点数到整数的可能有损转换”错误。
我将同时上传两个版本。从工作int / double开始,然后是float版本。
import javax.swing.JOptionPane;
public class AverageCalc
{
public static void main(String[] args)
{
String input, numberofGrades;
int scoreSum;
double data[];
double sum = 0, average = 0;
System.out.println("\tGrade Average Calculator");
numberofGrades = JOptionPane.showInputDialog("How many assignments have been graded?");
scoreSum = Integer.parseInt(numberofGrades);
data = new double[scoreSum];
for (int i = 0; i < data.length; i++)
{
input = JOptionPane.showInputDialog("Please enter your grade for each assignment");
data[i] = Double.parseDouble(input);
}
for (int i = 0; i < data.length; i++)
{
sum += data[i];
average = sum / data.length;
}
JOptionPane.showMessageDialog(null," The Average is: " + average, "Result",
JOptionPane.INFORMATION_MESSAGE);
}
}
那个有效,所以我试图将其转换为浮点数。请记住,我已经在此代码上进行了很多实验,尝试了其他操作,例如0.0、0.0f等,因此可能会比实际情况多得多。
String input, numberofGrades;
float scoreSum;
float data[];
float sum = 0.0f, average = 0.0f;
System.out.println("\tGrade Average Calculator");
numberofGrades = JOptionPane.showInputDialog("How many assignments have been graded?");
scoreSum = Float.parseFloat(numberofGrades);
data = new float[scoreSum];
for (float f = 0.0f; f < data.length; f++)
{
input = JOptionPane.showInputDialog("Please enter your grade for each assignment");
data[f] = Float.parseFloat(input);
}
for (float f = 0.0f; f < data.length; f++)
{
sum += data[f];
average = sum / data.length;
无论我做什么,甚至将scoreSum设置为浮点数(这是不合逻辑的,因为已评分的工作量始终为整数),我仍然收到行的“有损”消息
data = new float[scoreSum];
data[f] = Float.parseFloat(input);
sum += data[f];
我现在不知道该怎么办。我只是想完成作业,离开班级,再也不要参加编码选修课了。留给专业人士。
在此先感谢您的帮助。