简体   繁体   中英

I'm trying to write a program about modulus

I need help. I'm trying to write a program which depending on the value you're writing shows true or false depends on that if the value devision of a 7 without reminder. I wrote this , but it doesn't work properly:

import java.util.Scanner;

public class ex05 {

    public static void main(String[] args) {

    Scanner sc = new Scanner (System.in);   
    System.out.println("Please enter a value:");
        int  x = sc.nextInt();

        int a = x / 7;

        if (x % a == 0) 
        {
            System.out.println("true");
        }
        else {
            System.out.println("false");
        }
    }
}

I think you get it, without explanation. Simple math.

public class ex05 {

public static void main(String[] args) {

Scanner sc = new Scanner (System.in);   
System.out.println("Please enter a value:");
    int  x = sc.nextInt();


    if (x % 7 == 0) 
    {
        System.out.println("true");
    }
    else {
        System.out.println("false");
    }
}
}

( a % b

% is the modulus operator and it checks the remaining value after dividing a with b )

There is no need of using a . The % operator gives the remainder. There is no use in using / . Do

int  x = sc.nextInt();

if (x % 7 == 0){
    System.out.println("true");
}else{
    System.out.println("false");
}

or just

System.out.println(x % 7 == 0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM