netbeans 如何将其转换为if语句?[closed]

5uzkadbs  于 2022-11-10  发布在  其他
关注(0)|答案(2)|浏览(128)

已关闭。此问题需要details or clarity。当前不接受答案。
**想要改进此问题吗?**通过editing this post添加详细信息并阐明问题。

去年关闭了。
Improve this question
所以我基本上刚开始编码,我发现你也可以把它变成if语句,但是我试过了,我还是弄不明白,这是一个程序,它提示用户输入整数,然后判断它是否能被10整除。(我是这样写的,然后尝试用if语句以另一种方式得到答案,在结尾处给出了布尔t/f,类似于下面的内容,但是我不知道怎么写)谁能告诉我另一种方法吗?
代码:

package truefalse1;

    import java.util.Scanner;

    public class Truefalse1 {

    public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            System.out.print("Enter your integer");
            int number = input.nextInt();
            System.out.println("Is 10 divisible by 5 and 6? " +
                 ((number % 5 == 0) && (number % 6 == 0)));
            System.out.println("Is 10 divisible by 5 or 6? " +
                 ((number % 5 == 0) || (number % 6 == 0)));
            System.out.println("Is 10 divisible by 5 of 6, but not both? " +
                 ((number % 5 == 0) ^ (number % 6 == 0))
   );
mbjcgjjk

mbjcgjjk1#

它是一个程序,提示用户输入整数,并确定它是否不能被10整除。
这不是你发布的代码的作用,它真正的作用是要求用户输入一个数字,然后它确定这个数字是否能被5或6整除。
它打印的消息有点奇怪:“10是否可被5和6整除?”,因为程序实际执行的操作与数字10无关。您是否希望打印“(您输入的数字)是否可被5和6整除?”?然后,您必须将最后三行更改为类似以下内容:

System.out.println("Is " + number + " divisible by 5 and 6? " +
    ((number % 5 == 0) && (number % 6 == 0)));

关于您的问题:可以在一个或多个if语句中使用表达式number % 5 == 0number % 6 == 0,如下所示:

if ((number % 5 == 0) && (number % 6 == 0)) {
    System.out.println(number + " is divisible by 5 and 6");
} else {
    System.out.println(number + " is not divisible by 5 and 6");
}

if ((number % 5 == 0) || (number % 6 == 0)) {
    System.out.println(number + " is divisible by 5 or 6");
} else {
    System.out.println(number + " is not divisible by 5 or 6");
}

if ((number % 5 == 0) ^ (number % 6 == 0)) {
    System.out.println(number + " is divisible by 5 or 6, but not both");
} else {
    System.out.println(number + " is not: divisible by 5 or 6, but not both");
}

要了解有关if的更多信息,请访问:Oracle Java Tutorial - The if-then and if-then-else Statements

djp7away

djp7away2#

import java.util.Scanner;

public class Truefalse1 {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
System.out.print("Enter your integer");
 int number = input.nextInt();
if(number%5==0 && number%6==0)
  System.out.println("Print here your output");
if(number%5==0 || number%6==0)
  System.out.println("Print here your output");
if((number%5==0) ^ (number%6==0))
   System.out.println("Print Your output Here");

相关问题