具有评分策略的类的Java评分程序

时间:2012-06-28 20:20:35

标签: java methods

我必须为具有以下评分政策的班级编写评分程序。

  

1.有2个测验,每个测验基于10分。

     

2.有1个期中考试和1个期末考试,每个考试分为100分。

     

3.期末考试占50%,期中考试为25%,2次测验共计25%。 (不要   忘记规范测验分数。)

将根据以下标准给出一个字母等级: 90 - 100 A
80 - 89 B. 70-79 C 60 - 69 D. 0 - 59 E

该程序将从文本文件中读取学生的分数,并输出学生的记录,其中包括姓名,2个测验和2个考试分数以及学生的整个课程和最终字母等级的平均数字分数。 我想为学生记录定义和使用一个类。所有分数均为整数,学生姓名不超过10个字符。我必须证明你的输出文件。

这最初由导师给出,我不知道在哪里出演

import java.until.*;
import java.io.*;

public class Assign7{
   public static void main(String[] args)throws Exception{
    Record.setGP(1.25, 1.25, 0.25, 0.50);

 Scanner myIn = new Scanner( new File("scores.txt") );

 public static void main getLetterGrade  
   if (finalScore > 90)  
       letter = 'A'; 
  else if (finalScore > 80) 
     letter = 'B';
  else if (finalScore > 70)  
     letter = 'C';
  else if (finalScore > 60)
     letter = 'D';
  else  
     letter = 'F';

    System.out.println( myIn.nextLine() +"  avg  "+"letter");

    while( myIn.hasNext() ){
       Record myR = new Record(myIn.next(), myIn.nextInt(), myIn.nextInt(), myIn.nextInt(),
myIn.nextInt());

       System.out.println( myR );

    } 
  }
}

2 个答案:

答案 0 :(得分:8)

让我们走到这一步:

// Why not "class" (lower case)?
// Should "class Assign7" be "public" (is it the main class in the module)?
// Did you put any "import" statements above this?
Class Assign7{

   // Do you want to declare any member data before you start your "main()" function?
   public static void main(String[] args)throws Exception{

   // Is "Record.setGP()" a "static method"?
   Record.setGP(1.25, 1.25, 0.25, 0.50);

  // Isn't this class-wide data?  If so, shouldn't you put it *above* "main()"?    
  int quiz1, quiz2;
  int tMidterm, tFinal,tQuiz
  int midterm = 0;
  int finalExam = 0;
  String name;
  char grade;


  // Do you really want to just bomb out of the program if this fails?
  Scanner myIn = new Scanner( new File("scores.txt") );

   // Whoa!!!!  Why are we starting a new function, *inside of "main()"*?!?!
   void getScore()  
   {  
   tQuiz = ((quiz1 + quiz2)/20)*.25;  
   tMidTerm = (midTermExam/100)*.25;  
   tFinal = (finalExam/100)*.50;  
   finalScore = tQuiz + tMidTerm + tFinal;  
   }

   // It looks like we're starting a new function here, too.
   // Where's your parenthesis and curly brace after "getLetterGrade"????  
   void getLetterGrade  


   if (finalScore >= 90)  
   {  
   grade = 'A';  
   }  

   // Supposing the grade was "99"?  
   // Would the student get a "B" here?  Or even a "D" below???
   if (finalScore >= 80)  
   {  
   grade = 'B';  
   }
   ...

建议:

  1. 编写只是“编译”的最小的最小程序。

  2. 一次添加一件事。验证更改是否有效。

  3. 采取“婴儿步骤”以获得完整的解决方案。

  4. 根据需要发回有关具体问题的具体问题。

答案 1 :(得分:7)

您的代码存在很多问题,似乎根本不了解Java语法。不要试图定义主要的INSIDE方法。它们应该在主要之外定义。

我真的不知道从哪里开始,所以我建议你需要改变一件事:

if (finalScore >= 90)  
{  
grade = 'A';  
}  
if (finalScore >= 80)  
{  
grade = 'B';  
}
# etc...

finalScore100。然后,第一个if中的条件将评估为truegrade将被分配'A'。然后下一个if语句:100大于80,所以这也评估为true。你看到了问题吗? grade将重新分配到'B'

请阅读文档,了解else ifelse语句如何为您提供帮助:http://docs.oracle.com/javase/tutorial/java/nutsandbolts/if.html (例子是如何实际分配成绩,你正在处理的确切问题!)