import java.util.*;
public class Divisible {

    public static void main(String[] args) {
        // Divisible by 5 and 6 or not
        Scanner s = new Scanner(System.in);
        int x;
        System.out.print("Enter an integer: ");
        x = s.nextInt();
        if ((x % 5==0) && (x % 6==0)){
            System.out.print("is "+x+" divisible by 5 and 6? ");
            System.out.print("true");
        }else{
            System.out.print("is "+x+" divisible by 5 and 6? ");
            System.out.print("false");
        }// Divisible by 5 or 6
        if ((x % 5==0) || (x % 6==0)){
            System.out.print("\nIs "+x+" divisible by 5 or 6? ");
            System.out.print("true");
        }else{
            System.out.print("Is "+x+" divisible by 5 or 6? ");
            System.out.print("false");
        }// Divisible by 5 or 6,but not both
        if ((x % 5==0) || (x % 6==0)){ //here is my problem, i cant figure out the code for "not both" part
            System.out.print("Is "+x+" divisible by 5 or 6, but not both? ");
            System.out.print("true");
        }else{
            System.out.print("Is "+x+" divisible by 5 or 6, but not both? ");
            System.out.print("false");
        }

    }
}


我知道我的最后一个if-else陈述是错误的,我只是无法弄清楚最后一个任务的编码是“ + x +”是否可以被5或6整除,但不能同时被两者整除?”

谢谢

最佳答案

您可以使用以下逻辑

if(x%5 == 0 && x%6 == 0){
 SOP("number is divisible by both 5 and 6);
}else{
   if(x%5 == 0){
     SOP("Number is divisible only by 5");
   }else if(x%6 == 0){
    SOP("Number is divisible only by 6");
   }else{
    SOP("Number is not divisible 5 nor by 6");
   }
}

10-06 09:13