公式:可能丢失精度

时间:2016-04-04 02:38:12

标签: java

我正在尝试制作一个可以计算学生成绩的程序,Lab和Nonlab科目。 这是我到目前为止所做的,

    String nonlab,lab,choices;
    String quiz1,recitation1,act1,exam1;
    int choice;
    int q1,r1,a1,e1,cs1,gr1,eq1,LAB,lec,six;

    System.out.println("CHOOSE GRADE COMPUTATION");
    System.out.println("[1] NON-LAB COMPUTATION");
    System.out.println("[2] LAB SUBJECT");

    System.out.print("ENTER YOUR CHOICE: ");
    choices=input.readLine();
    choice=Integer.parseInt(choices);

    System.out.println("     ");

    //NON LAB
    if (choice==1){
        System.out.println("******* BEGIN COMPUTATION *******");


    System.out.print("ENTER QUIZ: ");
    quiz1=input.readLine();
        q1=Integer.parseInt(quiz1);

    System.out.print("ENTER RECITATION: ");
    recitation1=input.readLine();
        r1=Integer.parseInt(recitation1);

    System.out.print("ENTER ACTIVITY: ");
    act1=input.readLine();
        a1=Integer.parseInt(act1);

    System.out.print("ENTER MAJOR EXAM: ");
    exam1=input.readLine();
        e1=Integer.parseInt(exam1);

    System.out.println("     ");
    System.out.println("********** RESULT ***************");

    cs1 = (q1+r1+a1)/3;
    gr1 = ((cs1*2)+e1)/3;

    System.out.println("CLASS STANDING: "+cs1);
    System.out.println("GRADE: "+ gr1);
    System.out.print("EQUIVALENT: ");
        if (gr1>97)
            System.out.println("1.00");
        else if (gr1>=94)
            System.out.println("1.25");
        else if (gr1>=91)
            System.out.println("1.50");
        else if (gr1>=88)
            System.out.println("1.75");
        else if (gr1>=85)
            System.out.println("2.00");
        else if (gr1>=82)
            System.out.println("2.25");
        else if (gr1>=79)
            System.out.println("2.50");
        else if (gr1>=76)
            System.out.println("2.75");
        else if (gr1<=75)
            System.out.println("5.00");

    System.out.print("REMARKS: ");
        if (e1<75)
        System.out.println("FAILED");
            else if (e1>75)
        System.out.println("PASSED");
        }

    //LAB SUBJECT
    else if (choice==2){
            System.out.println("******* BEGIN COMPUTATION *******");


    System.out.print("ENTER QUIZ: ");
    quiz1=input.readLine();
        q1=Integer.parseInt(quiz1);

    System.out.print("ENTER RECITATION: ");
    recitation1=input.readLine();
        r1=Integer.parseInt(recitation1);

    System.out.print("ENTER ACTIVITY: ");
    act1=input.readLine();
        a1=Integer.parseInt(act1);

    System.out.print("ENTER MAJOR EXAM: ");
    exam1=input.readLine();
        e1=Integer.parseInt(exam1);

    System.out.println("     ");
    System.out.println("********** RESULT ***************");

    cs1 = (q1+r1)/2;
    lec = ((cs1*2)+e1)/3;
    six = lec*(0.60);
    LAB = a1*(0.40);
    gr1 = 60%+LAB;


    System.out.println("CLASS STANDING: "+cs1);
    System.out.println("GRADE: "+ gr1);
    System.out.print("EQUIVALENT: ");
        if (gr1>97)
            System.out.println("1.00");
        else if (gr1>=94)
            System.out.println("1.25");
        else if (gr1>=91)
            System.out.println("1.50");
        else if (gr1>=88)
            System.out.println("1.75");
        else if (gr1>=85)
            System.out.println("2.00");
        else if (gr1>=82)
            System.out.println("2.25");
        else if (gr1>=79)
            System.out.println("2.50");
        else if (gr1>=76)
            System.out.println("2.75");
        else if (gr1<=75)
            System.out.println("5.00");

    System.out.print("REMARKS: ");
        if (e1<75)
        System.out.println("FAILED");
            else if (e1>75)
        System.out.println("PASSED");
    }
    else (choice>=3)
        System.out.println("WRONG ENTRY!");


}

}

我收到的错误是:不是声明

        else (choice>=3)
             ^
';' expected
        else (choice>=3)

...当我尝试删除else语句时,会出现以下错误:

possible loss of precision
found   : double
required: int

six = lec*(0.60);
          ^

possible loss of precision
found   : double
required: int

    LAB = a1*(0.40);
                ^

我哪里出错了?

2 个答案:

答案 0 :(得分:0)

您已将LAB,lec,six声明为整数值。 对整数执行浮点数操作会导致该错误。 在将值赋值回int之前,只需对浮点运算的结果进行类型转换。

解决方案1:六=(int)lec *(0.60);

理想情况下,你应该声明六和lec为double。

答案 1 :(得分:0)

  1. 其他地方应该没有条件。如果你想引入一个条件,请使用else。
  2. 所以将语句修改为`else if(condition&gt; 3)System.out.println(&#34; WRONG ENTRY!&#34;);

    1. 可能会导致精度损失:在整个程序中,您使用的是整数,而像可能出现浮动结果的分区一样。
    2. 在您提到的示例中,您可以简单地将结果浮点数从double转换为int

      int six = (int) (lec*(0.6));
      

      但是,总的来说,建议下面一行中的许多变量可以更好地用作floatdouble

      int q1,r1,a1,e1,cs1,gr1,eq1,LAB,lec,six;