如何在system.out.println中声明整数的值

时间:2014-09-06 06:39:53

标签: eclipse if-statement

如何制作它以便我可以将a和b的随机值插入system.out.println,这样它会在控制台窗口显示随机数是什么?

public class MoreIfStmts {

    /* Chase Pittman
    * This program compares two variables and tells which one is greater or if they're equal.
    */
    public static void main(String[] args) {
        int q=0;
        while (q < 10) {
            q=q+1;
    int a=0;
    int b=0;
                a=(int) (Math.random() * 10);
                b=(int) (Math.random() * 10);
        if (a > b)
        {
            System.out.println ("Blue is better than red.");
        } // This is the end of the then clause.
            else
            {
            if (a < b)
            {
                    System.out.println ("Red is better than blue.");
                } // This is the end of the if clause.

                    else
                    if (a == b)
                    {
                        System.out.println ("Blue is as good as red.");
                } // This is the end of the second else clause.
            } // This is the end of the first else clause.
        } // This is the end of while statement.
    } // This is the end of main.
} // This is the end of class MoreIfStmts.

2 个答案:

答案 0 :(得分:2)

System.out.println(&#34;红色优于蓝色。&#34; +&#34; a =&#34; + a +&#34; b =&#34; + b);

答案 1 :(得分:0)

您也可以使用格式化输出:

System.out.printf("Blue is better than red. a = %d, b = %d\n", a, b);