2016-11-23 47 views
-1

我做了一个简单的成绩系统的乐趣。我尝试应用总分数并将其添加到我的getApercent()方法中的公式中。但是,我不断收到错误,不知道该怎么办。如何从另一个方法获得结果到另一个使用java的方法?

package gradesystem; 

import java.util.Scanner; 

public class Gradesystem {  

    public static void main(String[] args) { 

     Scanner keyboard = new Scanner(System.in); 
     Gradesystem gs = new Gradesystem(); 
     // TODO Auto-generated method stub 
     int Acount,Bcount,Ccount,Dcount,Fcount; 
    double ap,bp,cp,dp,fp; 
     System.out.println("Enter the amount of A's"); 
     Acount = keyboard.nextInt(); 
     System.out.println("Enter the amount of B's"); 
     Bcount = keyboard.nextInt(); 
     System.out.println("Enter the amount of C's"); 
     Ccount = keyboard.nextInt(); 
     System.out.println("Enter the amount of D's"); 
     Dcount = keyboard.nextInt(); 
     System.out.println("Enter the amount of F's"); 
     Fcount = keyboard.nextInt();   

    int grades; 
     ap = getApercent(Acount); 
     System.out.println(ap); 
     bp = getBpercent(Bcount); 
     System.out.println(bp); 
     cp = getCpercent(Ccount); 
     System.out.println(cp); 
     dp = getDpercent(Dcount); 
     System.out.println(dp); 
     fp = getFpercent(Fcount); 
     System.out.println(fp);  
    } 

    public static void Totalgrades(int acount, int bcount, int ccount, int dcount, int fcount){ 

    int totalofgrades = acount + bcount + ccount + dcount + fcount;  
    System.out.print(totalofgrades);    
    } 

    public static double getApercent(int a){ 
     double ap;  
     ap = (a/a * 100) + 0.5; 
     return Math.round(ap);   
    } 

    public static double getBpercent(int b){    
     double bp; 
     bp = (b/b * 100) + 0.5; 
     return Math.round(bp);   
    } 

public static double getCpercent(int c){   
     double cp; 
     cp = (c/c * 100) + 0.5; 
     return Math.round(cp);   
    } 

public static double getDpercent(int d){   
     double dp; 
     dp = (d/d * 100) + 0.5; 
     return dp;   
    } 

public static double getFpercent(int f){   
     double fp; 
     fp = (f/f * 100) + 0.5; 
     return fp;   
    } 
} 
+4

'我总是收到错误,不知道该怎么办......我们也没有,因为你从来没有告诉我们错误是什么。确切的问题是什么? –

+0

那么一个/ a是基本的数学。结果将是1.例如。 10/10 = 1 – darkhouse

+1

在Totalgrades中,我试图在我的getApercent()方法中获取Acount,Bcount等的总数。我试图做到这一点“a/totalgrades()* 100 + 0.5 –

回答

0

这里有一点猜测。但计算百分比的方法似乎没有了。再次假设;但要计算整个百分比,您可以使用下面的公式percentage = part/whole * 100

例如,

我们有9个等级,3个是A的,3个是B的,2个是C的,1是D的,0是E的。

然后我预期的百分比是如下:

33% A // 3/9 * 100 
33% B // 3/9 * 100 
22% C // 2/9 * 100 
11% D // 1/9 * 100 
0% E // 0/9 * 100 

的另一件事要指出的是运营商的/用两个整数做整数除法。所以3/9 == 0

您可以用更通用的版本替换所有特定的方法。

public static double getPercentage(int gradeCount, int totalNumberOfGrades) { 
    double percentage = (gradeCount/(double) totalNumberOfGrades * 100); 
    return Math.round(percentage); 
} 
+0

非常感谢您的帮助! –

相关问题