我正在尝试打印每个主题的平均成绩。
当我尝试这样做时,我无法获得十进制值的输出。
四舍五入到最接近的值。
package cube;
import java.io.*;
import java.util.Scanner;
import java.util.Scanner;
public class ReportCard
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
double DB[][],nos=0;
String S="";
double total1=0, total2=0, total3=0, total4=0, total5=0;
Scanner s = new Scanner(System.in);
void Input()throws Exception
{
System.out.print("Enter The Number Of Students : ");
nos=Integer.parseInt(br.readLine());
DB=new double[(int) (nos+1)][20];
String arrayOfNames[] = new String[(int) nos];
for (int i = 0; i < arrayOfNames.length; i++) {
System.out.print("Enter the name of student:");
arrayOfNames[i] = s.nextLine();
System.out.print("\nEnter "+arrayOfNames[i]+"'s English Score : ");
DB[i][0]=Integer.parseInt(br.readLine());
total1=total1+DB[i][0];
System.out.print("Enter "+arrayOfNames[i]+"'s Science Score : ");
DB[i][1]=Integer.parseInt(br.readLine());
total2=total2+DB[i][1];
System.out.print("Enter "+arrayOfNames[i]+"'s Maths Score : ");
DB[i][2]=Integer.parseInt(br.readLine());
total3=total3+DB[i][2];
DB[i][3]=(int)(DB[i][0]+DB[i][1]+DB[i][2]);
total4=total4+DB[i][3];
DB[i][4]=((int)((DB[i][3])*100)/300);
total5=total5+DB[i][4];
}
System.out.println("\n\n\nStudent Name. English Science \t Maths Total Percentage Pass or Fail \n");
for(int i=0;i<nos;i++)
{
System.out.print(""+arrayOfNames[i]+"\t\t");Padd("English \t ",DB[i][0]);Padd("Science \t ",DB[i][1]);
Padd("Maths \t\t ",DB[i][2]);Padd("Total \t",DB[i][3]);Padd("Percentage\t",DB[i][4]);
if ((DB[i][0])< 50 | (DB[i][1])< 50 | (DB[i][2]) < 50) {
System.out.print("\t\tFail");
}
else {
System.out.print("\t\tPass");
}
System.out.println(S);
S="";
}
//System.out.println(total);
int j=0;
DB[j][0]=(int) (total1/nos);
DB[j][1]=(int) (total2/nos);
DB[j][2]=(int) (total3/nos);
DB[j][3]=(int) (total4/nos);
DB[j][4]=(int) (total5/nos);
System.out.println(DB[j][0]);
System.out.println(DB[j][1]);
System.out.println(DB[j][2]);
System.out.println(DB[j][3]);
System.out.println(DB[j][4]);
System.out.print("\nAverage ");
Padd("English ",DB[j][0]);Padd("Science ",DB[j][1]);Padd("Maths ",DB[j][2]);Padd("Total ",DB[j][3]);Padd("Percentage ",DB[j][4]);
}
void Padd(String S,double dB2)
{
double N=dB2;
int Pad=0,size=S.length();
while(dB2!=0)
{
dB2/=10;
Pad++;
}
System.out.print(" "+N);
for(int i=0;i<size-Pad-4;i++)
System.out.print(" ");
}
public static void main(String args[])throws Exception
{
ReportCard obj=new ReportCard();
obj.Input();
}
}
当我尝试将j的数据类型更改为double时,出现错误“类型不匹配:无法从double转换为int” 2个快速修复程序 1)将强制转换添加到int 2)将j更改为int
任何人都可以帮我解决这个问题。 谢谢。
答案 0 :(得分:1)
使用整数计算或强制转换为整数将产生整数输出。
所以DB[j][0]=(int) (total1/nos);
将是一个整数值。
DB[j][0] = (total1 / nos);
应该得出预期值。
问题不是j
变量(它是数组的索引),而是计算。
但是,您可以通过定义一些要使用的常量来使代码更具可读性:
private static final int ENG = 0;
private static final int SCI = 1;
private static final int MAT = 2;
private static final int AVG = 3;
DB[i][ENG] = Integer.parseInt(br.readLine());
...
或者更好地为Student
上课。例如:
class Student
{
final String name;
int englishScore = 0;
int scienceScore = 0;
int mathScore = 0;
public int getEnglishScore()
{
return englishScore;
}
public Student setEnglishScore(int englishScore)
{
this.englishScore = englishScore;
return this;
}
public int getScienceScore()
{
return scienceScore;
}
public Student setScienceScore(int scienceScore)
{
this.scienceScore = scienceScore;
return this;
}
public int getMathScore()
{
return mathScore;
}
public Student setMathScore(int mathScore)
{
this.mathScore = mathScore;
return this;
}
public String getName()
{
return name;
}
Student(String name)
{
this.name = name;
}
}
另外,为了进行测试,请始终单独输入(可以更改-也许您想从文件中读取?),数据处理和数据输出。