2014-04-29 38 views
-3

我遇到問題。我有這個計劃,我應該創建,它應該讀取兩個測驗的成績,這兩個測驗的總和等於25%的學生總分,期中考試是25%的學生成績和期末考試是總等級的50%。當我運行程序時,除了第一次測驗分數爲7分,第二次測驗分數爲8分,中期分數爲90分以及最終分數爲80分時,除了總分爲81.25%之外,其他所有內容都可以,但是會顯示.8125並自動給學生F.我不知道如何修復它....需要關於java分級程序的幫助嗎?我需要製作一個程序,讀取學生總成績的百分比

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

我認爲人們看你的標題,看到第一個兩句話,缺乏資本化,並下調投票你的問題認爲你要求的代碼,並沒有花時間來正確地格式化你的問題。您可能想要編輯它。 – DoubleDouble

+0

是的,我就像爲什麼我得到像一百萬倒票?我並沒有乞求人們尋找類似這裏數百萬人的代碼,我只是需要幫助。 – Madridista94

回答

0

如果你得到0.8125只是乘以100並追加'%'得到81.25%。

String gradeStr = (totalGrade*100) + "%"; 
System.out.println(gradeStr); 

爲了澄清,你是不是使用%作爲模運算符,但作爲一個字符串。

+0

你能詳細闡述一下嗎?並感謝您的快速響應,比如我會在哪裏插入百分比符號,我知道百分比符號給你剩下的部分... – Madridista94

+0

是的,我修復了它,謝謝 – Madridista94

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和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 + "%"); 
+0

@ user33863999嗯,它看起來像你有數學問題。 。 –

+0

其實我修好了,現在正確給我81.25%,謝謝你們!有趣的是,一個小錯誤如何破壞整個程序...大聲笑 – Madridista94

+0

乘以100小數點右移兩位謝謝! – Madridista94