获得两个变量的百分比

时间:2018-03-29 07:18:57

标签: java

我尝试过使用我的变量和我在这里读到的关于获取百分比的一些其他技巧,这里的代码是不行的。当然还有很多东西。我已经尝试过从铸造到我所展示的各种方式! (第二次编辑!)这是完整的代码!你是对的是工作,但在这不是!即使等级变量是浮点数或整数,仍然无效。更改了等级变量,因此它们不显示小数!输出(下图)显示等级,但不显示百分比。感谢您的反馈.OUTPUTOUTPUTOUTPUT:请输入0 - 100的分数!输入-1结束程序! 99请输入0 - 100的等级!输入-1结束程序! -1等级总数为2个A' s = 1的数量为%0.0 B' s = 0的数量为%0.0 C' s = 0的数量为%0.0的数量D' s = 0,即%0.0 F' s = 0的数量,即%0.0

public class Main{

public static void main(String[] main)
{

    Scanner input = new Scanner(System.in) ;

    int totalGrades = 1 ;

    int gradeA = 0 ;
    float gradeAPercentage = (gradeA * 100f) / totalGrades ; 

    int gradeB = 0 ;
    float gradeBPercentage = gradeB / totalGrades ;

    int gradeC = 0 ;
    float gradeCPercentage = gradeC / totalGrades ;

    int gradeD = 0 ;
    float gradeDPercentage = gradeD / totalGrades ;

    int gradeF = 0 ;
    float gradeFPercentage = gradeF / totalGrades ;

    int a = 0 ;

    do{
        System.out.println("Please enter a grade from 0 - 100! Enter -1 to end the program!") ;
        int grade = input.nextInt() ;

        if( (grade == -1) && (totalGrades == 0) ){
            System.out.println("You entered no grades!") ;
            totalGrades--;
            break ;
        } 
        else if(grade == -1){
            a++ ;
        }
        else if(grade <= 59){
            gradeF++ ;
            totalGrades++ ;
        }
        else if( (grade >= 60) && (grade <= 69) ){
            gradeD++ ;
            totalGrades++ ;
        }
        else if( (grade >= 70) && (grade <= 79) ){
            gradeC++ ;
            totalGrades++ ;
        }
        else if( (grade >= 80) && (grade <= 89) ){
            gradeB++ ;
            totalGrades++ ;
        }
        else if( (grade <= 100) && (grade >= 90) ){
            gradeA++ ;
            totalGrades++ ;
        }
        else{
            System.out.println("Your input is Invalid") ;
            continue ;
        }
    }while(a < 1 ) ;

    System.out.println("The total number of grades is " + totalGrades) ;
    System.out.println("Number of A's = " + gradeA + " which is %" + gradeAPercentage) ;
    System.out.println("Number of B's = " + gradeB + " which is %" + gradeBPercentage) ;
    System.out.println("Number of C's = " + gradeC + " which is %" + gradeCPercentage) ;
    System.out.println("Number of D's = " + gradeD + " which is %" + gradeDPercentage) ;
    System.out.println("Number of F's = " + gradeF + " which is %" + gradeFPercentage) ;
    }
 }

2 个答案:

答案 0 :(得分:1)

完成了隐式转换,不需要额外的浮动转换。您提供的代码工作正常。如果使用

打印结果
System.out.println(gradeAPercentage);

你会得到,你想要计算的东西。

答案 1 :(得分:0)

您可以执行以下操作:

import java.util.Scanner;

public class Main {

    public static void main(String[] main) {
        Scanner input = new Scanner(System.in);
        int grade = 0, gradeA = 0, gradeB = 0, gradeC = 0, gradeD = 0, gradeF = 0, totalGrades = 0;

        do {
            System.out.print("Please enter a grade from 0 - 100 (Enter -1 to end the program): ");
            grade = input.nextInt();
            if (grade < -1 || grade > 100) {
                System.out.println("Your input is Invalid");
                continue;
            }
            if (grade >= 0 && grade <= 59) {
                gradeF++;
            } else if (grade >= 60 && grade <= 69) {
                gradeD++;
            } else if (grade >= 70 && grade <= 79) {
                gradeC++;
            } else if (grade >= 80 && grade <= 89) {
                gradeB++;
            } else if (grade <= 100 && grade >= 90) {
                gradeA++;
            }
            if (grade != -1) {
                totalGrades++;
            }
        } while (grade != -1);

        System.out.println("The total number of grades is " + totalGrades);
        if (totalGrades > 0) {
            System.out.println("Number of A's = " + gradeA + " which is %" + ((float) gradeA / totalGrades) * 100);
            System.out.println("Number of B's = " + gradeB + " which is %" + ((float) gradeB / totalGrades) * 100);
            System.out.println("Number of C's = " + gradeC + " which is %" + ((float) gradeC / totalGrades) * 100);
            System.out.println("Number of D's = " + gradeD + " which is %" + ((float) gradeD / totalGrades) * 100);
            System.out.println("Number of F's = " + gradeF + " which is %" + ((float) gradeF / totalGrades) * 100);
        }
    }
}

示例运行:

Please enter a grade from 0 - 100 (Enter -1 to end the program): 32
Please enter a grade from 0 - 100 (Enter -1 to end the program): 33
Please enter a grade from 0 - 100 (Enter -1 to end the program): 34
Please enter a grade from 0 - 100 (Enter -1 to end the program): 99
Please enter a grade from 0 - 100 (Enter -1 to end the program): -1
The total number of grades is 4
Number of A's = 1 which is %25.0
Number of B's = 0 which is %0.0
Number of C's = 0 which is %0.0
Number of D's = 0 which is %0.0
Number of F's = 3 which is %75.0