java打印两个字符串,Java打印包含多个整数的字符串

Just starting learning java today and can't seem to figure this out. I am following the tutorial on learnjavaonline.org which teaches you a few things and then asks you to write a code to do a specific thing, it then checks the output to see if its correct. The thing is, if its not correct, it doesn't say why, or give you an example of the correct code.

It wants me to output a string saying "H3110 w0r1d 2.0 true" using all of the primitives

i came up with this

public class Main {

public static void main(String[] args) {

char h = 'H';

byte three = 3;

short one = 1;

boolean t = true;

double ten = 10;

float two = (float) 2.0;

long won = 1;

int zero = 0;

String output = h + three + one + ten + " " + "w" + zero + "r" + won + "d " + two + " " + t;

System.out.println(output);

}

}

but it outputs 86.0 w0r1d 2.0 true

how can i make it so it doesn't add all the integers, but displays them consecutively?

解决方案

You can either convert your numbers into a string using the toString or valueOf methods of the wrapper classes (guess you are not there yet), or just stuff all your primitives into the printline without the String output.

system.out.println(h + three + one + ten + " " + "w" + zero + "r" + won + "d " + two + " " + t);

All you need to look for is that there is a String in the printline statement. Meaning if you only want to print our number based datatype you can use system.out.println("" + youNumberVariable).

There would also be the option to add an empty string at the beginning of your declaration of output output = "" + theRest; to force all following values into the string like it does in the printline statement.

Most of it is not very pretty coding but will completly suffice for the learning process.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值