怎么把带有值的多个变量输出?
那代码怎么写呢?
那代码怎么写呢?
2015-09-22
double avg1=10.5; int num1=10; double num2=num1; int avg2=(int)avg1; System.out.println(avg1); System.out.println(avg2); System.out.println(num1); System.out.println(num2);
System.out.println(avg1);
System.out.println(avg2);
System.out.println(num1;)
System.out.println(num2);
↑
输出变量avg1,avg2,num1,num2。用eclipse运行了下,得到
不知道对不对...
输出多个变量,如变量间需要换行,这简便写法如下:
double avg1=10.5; int num1=10; double num2=num1; int avg2=(int)avg1; System.out.println(avg1 + "\n" + avg2 + "\n" + num1 + "\n" + num2);
输出结果为
10.5
10
10
10.0
如果需要将多个变量的值不换行连起来输出,则将输出语句中的"\n"转为""即可,如下:
System.out.println(avg1 + "" + avg2 + "" + num1 + "" + num2);
输出结果为
10.5101010.0
希望能有帮助!
举报