如何将用户输入的整数插入println语句?

时间:2014-02-10 04:41:18

标签: java

我想更换一下“第一个数字是第二个数字的倍数”。更像System.out.print(+ int a“是”+ int b的倍数); 有什么建议? 我正在使用的代码如下。

import java.util.Scanner; 

public class Multiples {
    public static void main( String[] args ) {
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter two integers " );
        int a = scan.nextInt(); // First number input by user
        int b = scan.nextInt(); // Second number input by user

        if ( a % b == 0 )// is a a multiple of b 
            System.out.print("The first number is a multiple of the second.");
        else 
            System.out.print("The first number is not a multiple of the second.");
    }
}    

1 个答案:

答案 0 :(得分:1)

您可以这样做:

System.out.print( a + " is a multiple of " + b); // Or System.out.println()

或使用printf()

System.out.printf("%d is a multiple of %d", a, b);