我有问题。我有这个我应该创建的程序,它应该读两个测验的成绩,这两个测验总计得到学生总成绩的25%,期中考试占学生总成绩的25%成绩和期末考试占总成绩的50%。当我运行该程序时,一切都很好,除非我为第一个测验成绩提供7个,第二个测验成绩为8个,期中为90个,期末为80个,总成绩应为81.25%但是它显示为.8125并自动给学生一个F.我不知道如何解决它......
import java.util.Scanner;
public class StudentClass {
// calculate grade first then the if else statements for the letter grade
public static Scanner grades = new Scanner (System.in);
public static double quiz1, quiz2, midterm, finalExam, Grades, totalGrade, bothQuizzes, PfinalExam, PmidTerm;
public static String studentname;
public static int Score;
public String getstudentname( )
{
return studentname;
}
public double getquiz1()
{
return quiz1;
}
public double getquiz2 ()
{
return quiz2;
}
public double midterm()
{
return midterm;
}
public double finalExam()
{
return finalExam;
}
public void setquiz1 (double quiz1)
{
this.quiz1 = quiz1;
}
public void setquiz2 (double quiz2)
{
this.quiz2 = quiz2;
}
public void setmidterm()
{
this.midterm = midterm;
}
public void setfinalExam()
{
this.finalExam = finalExam;
}
public void setGrades ()
{
}
public String toString(){
return this.quiz1 + " " + this.quiz2 + " " + this.midterm + " " + this.finalExam;
}
public static void readInput(){
System.out.println("Please enter the grade you got for the first quiz: ");
quiz1 = grades.nextInt();
while (quiz1 <0 || quiz1>10)
{
System.out.println("Please enter a grade between zero and ten: ");
quiz1 = grades.nextInt();
}
System.out.println("Please enter the grade you got for the second quiz: ");
quiz2 = grades.nextInt();
while (quiz2 <0 || quiz2>10)
{
System.out.println("Please enter a grade between zero and ten: ");
quiz2 = grades.nextInt();
}
System.out.println("Please enter the grade you got on your midterm: ");
midterm = grades.nextInt();
while (midterm <0 || midterm>100)
{
System.out.println("Please enter a grade between 0 and 100: ");
midterm = grades.nextInt();
}
System.out.println("Please enter the grade you got on your final exam: ");
finalExam = grades.nextInt();
while (finalExam < 0 || finalExam > 100)
{
System.out.println("Please enter a grade between 0 and 100: ");
finalExam = grades.nextInt();
}
}
public static void output()
{
System.out.println(" your score for the first quiz was " + quiz1 );
System.out.println("your score for the second quiz was " + quiz2);
System.out.println(" your score for the midterm was " + midterm );
System.out.println("your score for the final exam was " + finalExam);
bothQuizzes = ((quiz1 + quiz2)/20)*.25;
PmidTerm = (midterm/100) *.25;
PfinalExam = (finalExam/100) * .50;
totalGrade = bothQuizzes + PmidTerm + PfinalExam;
System.out.println("Your total grade for these grades is " + totalGrade);
double letterGrade = totalGrade;
if (letterGrade >= 90)
{
System.out.println("Your grade is an A");
// grade = "A";
}
else if (letterGrade >= 80)
{
System.out.println("Your grade is a B");
}
else if (letterGrade >= 70)
{
System.out.println("Your grade is a C");
}
else if (letterGrade >= 60)
{
System.out.println("Your grade is a D");
}
else
{
System.out.println("Your grade is an F");
}
}
}
答案 0 :(得分:3)
bothQuizzes = ((quiz1 + quiz2)/20)*.25;
PmidTerm = (midterm/100) *.25;
PfinalExam = (finalExam/100) * .50;
totalGrade = bothQuizzes + PmidTerm + PfinalExam;
假设您输入0到100之间的整数值,这将为您提供:
bothQuizzes:0到2.5之间的值 PmidTerm:介于0和.25之间的值 PfinalExam:介于0和.50之间的值
你显然在这里有一些数学问题。
您应该将此处的第一行更改为
bothQuizzes = ((quiz1 + quiz2)/200) *.25
看起来你在某处掉了0。这将为您提供0到0.25之间的值
这意味着totalGrade将介于0和1.00之间。你应该将它乘以100,所以你有:
totalgrade = 100*(bothQuizzes + PmidTerm + PfinalExam)
以百分比显示:
System.out.println("Your total grade for these grades is " + totalGrade + "%");
答案 1 :(得分:0)
如果你得到0.8125只乘以100并附加一个&#39;%&#39;获得81.25%。
String gradeStr = (totalGrade*100) + "%";
System.out.println(gradeStr);
为了澄清,你不是像模数运算符那样使用%
,而是作为一个字符串。