2011-10-03 78 views
3

我正在編寫一個程序,以英寸和英尺的高度查詢用戶的理想體重。到目前爲止,除了最後的聲明外,一切看起來都很好,在這個聲明中,我向用戶返回了男性和女性的平均體重,並告訴他們體重在15%之間的範圍。下面是給了我一個問題的代碼:在println語句中使用字符串和變量

System.out.println("For a male, the height " + foot "' " + 
        inches + "/""" + " and it's ideal weight is " + 
        mWeight + "lbs. Anything between " + minMaleLb + 
        "lbs and " + maxMaleLb + "lbs is okay."); 
System.out.println("For a female, the height " + foot "' " + 
        inches + "/""" + " and it's ideal weight is " + 
        fWeight + "lbs. Anything between " + minFeMaleLb + 
        "lbs and " + maxFeMaleLb + "lbs is okay."); 

這裏是我收到編譯時錯誤:

File: C:\Users\###\Java\IdealWeight.java [line: 26] 
Error: C:\Users\###\Java\IdealWeight.java:26: ')' expected 
File: C:\Users\###\Java\IdealWeight.java [line: 27] 
Error: C:\Users\###\Java\IdealWeight.java:27: not a statement 
File: C:\Users\###\Java\IdealWeight.java [line: 27] 
Error: C:\Users\###\Java\IdealWeight.java:27: ';' expected 
File: C:\Users\###\Java\IdealWeight.java [line: 29] 
Error: C:\Users\###\Java\IdealWeight.java:29: not a statement 
File: C:\Users\###\Java\IdealWeight.java [line: 29] 
Error: C:\Users\###\Java\IdealWeight.java:29: ';' expected 
File: C:\Users\###\Java\IdealWeight.java [line: 31] 
Error: C:\Users\###\Java\IdealWeight.java:31: ')' expected 
File: C:\Users\###\Java\IdealWeight.java [line: 32] 
Error: C:\Users\###\Java\IdealWeight.java:32: not a statement 
File: C:\Users\###\Java\IdealWeight.java [line: 32] 
Error: C:\Users\###\Java\IdealWeight.java:32: ';' expected 
File: C:\Users\###\Java\IdealWeight.java [line: 34] 
Error: C:\Users\###\Java\IdealWeight.java:34: not a statement 
File: C:\Users\###\Java\IdealWeight.java [line: 34] 
Error: C:\Users\###\Java\IdealWeight.java:34: ';' expected 
File: C:\Users\###\Java\Weight.java [line: 30] 
Error: C:\Users\###\Java\Weight.java:30: reached end of file while parsing 
File: C:\Users\###\Java\Weight.java [line: 34] 
Error: C:\Users\###\Java\Weight.java:34: reached end of file while parsing 

可有人請向我解釋什麼是錯的代碼,以及如何將我去解決這個問題?

+2

您還缺少一些+。例如:'foot''「'而不是'foot +''」'。 –

回答

18

問題是你逃避你的報價錯誤;應該是

... + "\"" + ... 

爲引用標記。

編輯:正如JB Nizet在評論中指出的,您在foot之後缺少+

此外,您還可以簡化表達更多一些

... + "\" and the ideal weight is " + ... 

,而不是連接兩個獨立的字符串。

1

"/"""位看起來不正確,可能會導致解析器關閉。也許你想用"\""(或者更簡單,'"'

+0

啊,我不知道在java中你可以像System.out.println(''')這樣的格式使用字符串,但它似乎只能用引號來工作,我會記住這一點,謝謝! – mooles

+3

@mooles:單引號分隔(單個)字符,而不是字符串,但是打印方法和StringBuilder方法具有接受字符的重載,並且Strings上的+運算符自動將非String操作數轉換爲字符串;因此,例如'String s =「」+ 123;'works。 – millimoose

+0

我不知道「以格式使用字符串」是什麼意思。這個答案不使用任何通常被稱爲「字符串格式化」的東西,而是'System.out.println '是一種方法,而不是「格式」。 –

12

與所有這些串連,我會sugest使用System.out.format(format, args);

System.out.format("For a male, the height %d' %d\" and its ideal weight is %dlbs. Anything between %dlbs and %dlbs is okay.", 
    foot, inches, mWeight, minMaleLb, maxMaleLb); 

你也有拼寫錯誤:

and it's ideal weight is 

應是

and its ideal weight is 

「它」沒有撇號(即不是「它」)以所有格形式使用時

+0

-1因爲與問題完全無關 – fluffy

+0

@fluffy過早發表。請再閱讀 – Bohemian

+0

語法完全關閉,你抱怨abou撇號? –

相關問題