2017-09-21 62 views
0
public class FinalMarkCalculator 
{ 
/** 
* This method is used to caculate the students mark 
*/ 
    public static void main(String[] args) 
    { 
     //Assigning the string a variable that holds the students name 
     String studentName; 

     //Initalizing a keyboard scanner 
     Scanner keyboardStudentName = new Scanner(System.in); 

     //System printing out the question 
     System.out.print("Enter the students name: "); 

     //Assigns the string studentName to the users input 
     studentName = keyboardStudentName.nextLine(); 

     //Assigning the double a variable that hold the students assignment marks as a integer 
     double assignmentMarks; 

     //Assigning the string a variable that hold the students assignment marks 
     String assignmentMarksInput; 

     //Initalizing a keyboard scanner with the variable 
     Scanner keyboardAssignmentMarks = new Scanner(System.in); 

     //Asking the user for assingment marks while printing students name 
     System.out.printf("Enter %s's mark for Assignments (Max. 140): ", studentName); 

     //Taking the user input and assigning it to our string assignmentMarksInput 
     assignmentMarksInput = keyboardAssignmentMarks.nextLine(); 

     //Converting the user input from a string to a double 
     assignmentMarks = Double.parseDouble(assignmentMarksInput); 

     //Assigning the double a variable that hold the students Mid-Term exam marks as a integer 
     double midTermExamMark; 

     //Assigning the string a variable that hold the students assignment marks from user input 
     String midTermExamMarkInput; 

     //Initalizing a keyboard scanner with the variable 
     Scanner keyboardExamMidTermMark = new Scanner(System.in); 

     //Asking the user for the Mid-Term Exam mark while printing students name 
     System.out.printf("Enter %s's mark for Mid-Term Exam (Max. 60): ", studentName); 

     //Taking the user input and assigning it to our midTermExamMarkInput 
     midTermExamMarkInput = keyboardExamMidTermMark.nextLine(); 

     //Converting the user input from a string to a double 
     midTermExamMark = Double.parseDouble(midTermExamMarkInput); 

     //Assigning the double a variable that hold the students Mid-Term exam marks as a integer 
     double finalExamMark; 

     //Assigning the string a variable that hold the students assignment marks from user input 
     String finalExamMarkInput; 

     //Initalizing a keyboard scanner with the variable 
     Scanner keyboardFinalExamMark = new Scanner(System.in); 

     //Asking the user for the Mid-Term Exam mark while printing students name 
     System.out.printf("Enter %s's mark for Final Exam (Max. 85): ", studentName); 

     //Taking the user input and assigning it to our midTermExamMarkInput 
     finalExamMarkInput = keyboardFinalExamMark.nextLine(); 

     //Converting the user input from a string to a double 
     finalExamMark = Double.parseDouble(finalExamMarkInput); 


     //Printing grade report title 
     System.out.println("Grade report" + "\n------------"); 

     //Printing the students name 
     System.out.println(studentName + "\n"); 

     //Printing the assignments mark 
     System.out.printf("%.2f/140 worth 15%%\n", assignmentMarks); 

     //Printing the Mid-Term exam mark 
     System.out.printf("%.2f/60 worth 40%%\n", midTermExamMark); 

     //Printing the Final exam mark 
     System.out.printf("%.2f/85 worth 45%%\n", finalExamMark); 

     double finalMark = (assignmentMarks*.15)+(midTermExamMark*.40)+(finalExamMark*.45); 

     //Printing the total calculated final mark 
     System.out.printf("\n\nFinal Mark: " + finalMark);    
    } 
} 

嗨,我所有的代码运行良好,但它不正确做数学,我似乎无法弄清楚为什么即使我把一切完美它仍然不等于100%,这是我在程序得益于首次尝试分级重量不正确计算

这是用公式IM,我认为这是应该怎么做: (assignmentMarks * .15)+(midTermExamMark * 0.40)+(finalExamMark * 0.45)

+0

论坛开始,你不需要多个扫描仪对象,只需重新使用 –

+0

我可以看到这样的一个例子吗? – Tyler

回答

0

数学应该是

double finalMark = (assignmentMarks/140*15)+(midTermExamMark/60*40)+(finalExamMark/85*45); 

此外,我认为这是最好不要申报(再后来)初始化变量 - 做一步

double assignmentMarks = Double.parseDouble(assignmentMarksInput); 

而且只需使用一台扫描仪

Scanner scanner = new Scanner(System.in); 

,并使用它的所有输入

String studentName = scanner.nextLine(); 
.... 
String assignmentMarksInput = scanner.nextLine(); 
+0

哦好吧非常感谢你,我会努力改进格式 – Tyler