Java prints a string containing multiple integers

Just started learning Java, it seems that I can't figure this out I'm learning learnjaonline Org, which teaches you something, then requires you to write a code to do a specific thing, and then check the output to see if it is correct The problem is, if it's not correct, it doesn't explain why, or give you an example of the correct code

It expects me to output a string "h3110 w0r1d 2.0 true" using all 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 do this? It doesn't add all integers, but displays them continuously?

Solution

You can use the toString or valueof methods of the wrapper class to convert numbers to strings (guess you haven't), or just fill all primitives into printline without outputting string

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

What you need to look for is a string in the printline statement This means that if you only want to print numeric based data types, you can use system out. println(“”youNumberVariable).

You can also choose to add an empty string at the beginning of the output = "" therest declaration; Cast all subsequent values to a string, just as in a printline statement

Most of them are not very beautiful coding, but they can fully meet the learning process

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>