Home > database >  How do I convert this into an if statement? [closed]
How do I convert this into an if statement? [closed]

Time:09-29

So I basically just started coding, and I saw that you can turn this into an if statement too! but i tried and I couldn't figure it out? It's a program that prompts the user into writing and integer and determines whether it is not divisible by 10. (I wrote it this way, and then tried to use the if statement to get answers in another way, gives me boolean t/f at the end similar to below, but i couldn't figure out how to write it properly) can anybody show me another way?

The code:

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

CodePudding user response:

It's a program that prompts the user into writing and integer and determines whether it is not divisible by 10.

That's not what the code you posted does. What it really does, is ask the user to enter a number, and then it determines if that number is divisible by 5 or 6.

The messages it prints are a little bit strange: "Is 10 divisible by 5 and 6?" because what the program actually does has nothing to do with the number 10. Did you want to print "Is (the number you entered) divisible by 5 and 6?" instead? Then you'd have to change the last three lines to something similar to this:

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

About your question: You can use the expressions number % 5 == 0 and number % 6 == 0 in one or more if statements like this:

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

To learn more about if, see: Oracle Java Tutorial - The if-then and if-then-else Statements

CodePudding user response:

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");
  • Related