3
因此,我對編程相當陌生,並且剛剛在學校開始上課。我們被告知要製作一個程序,只使用變量類型char,int,byte,float和boolean來打印:「H3110 wor1d 2.0 true」。這是我能夠想出的。無法將原始類型值連接成字符串
public class Homework3 {
public static void main(String[] args) {
char ab = 'H';
int cd = 3110;
char ef = 'w' ;
byte gh = 0;
char ig = 'r';
char l = '1';
char d = 'd';
float mn = 2.0f;
char op = ' ' ;
boolean qr = false;
String x = (ab + cd + ef + gh + ig + l + d + mn + op + qr);
System.out.println(x);
}
}
但是,當我嘗試運行它時,我總是收到錯誤消息。
String x = (ab + cd + ef + gh + ig + l + d + mn + op + qr);
^
first type: float
second type: boolean
1 error
我不知道我在做什麼錯,並想知道是否有更簡單的方法來編寫這個程序。我是不是真的比它更復雜?
或者更簡單地說,使用多個打印語句。 – chrylis