Java初学者,在最后一条if-else语句中,我无法理解;但并非两者皆有”;部分

Java初学者,在最后一条if-else语句中,我无法理解;但并非两者皆有”;部分,java,if-statement,Java,If Statement,我知道我的最后一个if-else语句是错误的,我就是想不出最后一个任务的编码是“is”+x+“可以被5或6整除,,但不是两者都可以。” 谢谢您可以使用以下逻辑 import java.util.*; public class Divisible { public static void main(String[] args) { // Divisible by 5 and 6 or not Scanner s = new Scanner(System.i

我知道我的最后一个if-else语句是错误的,我就是想不出最后一个任务的编码是“is”+x+“可以被5或6整除,,但不是两者都可以。”


谢谢

您可以使用以下逻辑

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");
        }

    }
}

您可以使用以下逻辑

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条件。第一个if条件可以简单地放在30,这是5和6的LCD(最小公分母)

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");
   }
}

我只是稍微修改了你的代码。主要是改变了if-else条件。第一个if条件可以简单地放在30,这是5和6的LCD(最小公分母)

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");
   }
}

这不是javascript抱歉,如果我弄错了问题公式,我是新来的((x%5==0)| |(x%6==0))&&!((x%5==0)和&(x%6==0))这不是javascript抱歉,如果我弄错了问题公式,我是新来的((x%5==0)| |(x%6==0))&!((x%5==0)和&(x%6==0))