字符串Var与int var

时间:2014-09-08 15:44:49

标签: java

我希望在我的int变量旁边显示一个字符串变量,但它似乎不起作用。

public class ThreeLittleNumbers {
    public static void main(String[] argts) {
        String as = "Number 1: ";
        String bs = "Number 2: ";
        String cs = "Number 3: ";
        int a = 1;
        int b = 3;
        int c = 5;
        String tot = "Total: ";
        System.out.println(as+a);
        System.out.println(bs+b);
        System.out.println(cs+c);
        System.out.println(tot);
        System.out.print(a+b+c);
    }
}

1 个答案:

答案 0 :(得分:2)

如果我理解你的问题,那么

System.out.println(tot);
System.out.print(a + b + c);

应该是

System.out.print(tot);
System.out.println(a + b + c);

当我进行上述更改时,我会得到您似乎要求的输出 -

Number 1: 1
Number 2: 3
Number 3: 5
Total: 9

另一个选择是使用printf

之类的东西
System.out.printf("%s %d%n", tot, a + b + c);