在 java 的这个布尔事物中遇到问题

问题描述

如果学生的出勤率低于 75%,将不允许他/她参加考试。请从用户那里获得以下输入:

并且打印出的班级百分比是学生是否可以参加考试。

修改上述问题,让学生在有医疗原因时可以坐下。询问用户他/她是否有医疗原因('Y' 或 'N')并相应打印。

所以我在医学部分的这个问题上遇到了麻烦。有人可以告诉我如何使这段代码更好。这是我的代码

package A;
import java.util.Scanner;
public class Practice5 {

    public static void main(String[] args) {
        // A student will not be allowed to sit in exam if his/her attendence is less than 75%
        
        Scanner S = new Scanner(system.in);
        
        System.out.println("Enter number of class attendend");
        float Ca = S.nextFloat();
        
        System.out.println("Enter number of class held");
        float Ch = S.nextFloat();
        
        System.out.println("Did student have any medical cause \"true or false?\"");
        boolean medical = S.nextBoolean();
        
        if(Ca/Ch*100 >75 ) {
                System.out.println("You are allowed to sit in exam with "+Ca/Ch*100 +" Percentage of attendence");
        }
        
        if (medical) {
            System.out.println("You are allowed to sit in exam with "+Ca/Ch*100 +" Percentage of attendence");
        }
        
        else {
            System.out.println("You are not allowed to sit in exam with "+ Ca/Ch*100 + " Percentage of attendance");
        }
        
    }

}

解决方法

结合允许学生上课的条件。本题条件为medical == true 或参加课程的百分比应大于75。请参阅以下代码以供参考

package A;
import java.util.Scanner;
public class Practice5 {

    public static void main(String[] args) {
        // A student will not be allowed to sit in exam if his/her attendence is less than 75%
        
        Scanner S = new Scanner(System.in);
        
        System.out.println("Enter number of class attendend");
        float Ca = S.nextFloat();
        
        System.out.println("Enter number of class held");
        float Ch = S.nextFloat();
        
        System.out.println("Did student have any medical cause \"true or false?\"");
        boolean medical = S.nextBoolean();

        if (medical || (Ca/Ch*100 >75)) {
            System.out.println("You are allowed to sit in exam with "+Ca/Ch*100 +" Percentage of attendence");
        }
        else {
            System.out.println("You are not allowed to sit in exam with "+ Ca/Ch*100 + " Percentage of attendance");
        }
        
    }

}

希望这能回答您的问题。如果需要进一步说明,请在评论中联系。很乐意提供帮助