开发者

Using strings and variables in println statements

开发者 https://www.devze.com 2023-04-10 16:00 出处:网络
I am writing a program that finds a user\'s ideal weight by asking for their height in inches and feet. So far everything looks alright except the final statement, in which I return to the user the av

I am writing a program that finds a user's ideal weight by asking for their height in inches and feet. So far everything looks alright except the final statement, in which I return to the user the average weight for a male and female, and also tell them the range of their weight between 15%. Here is the code that gives me a problem:

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.");

And here are the errors I receive while compiling:

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

Can someone please explain to me what is wrong with the code, and how would I go about fixing this?


The problem is that you're escaping your quotes wrong; that should be

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

for the quotemark.

EDIT: And as JB Nizet pointed out in a comment, you're missing the + after foot.

Also, you can simplify that expression a bit more with

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

rather than concatenating two separate strings.


With all those concatenations, I'd sugest using 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);

You also have a spelling error:

and it's ideal weight is

should be

and its ideal weight is

"its" has no apostrophe (ie not "it's") when used in the possessive form


The "/""" bits look wrong and could throw the parser off. Maybe you meant to use "\"" (or even simpler, '"')

0

精彩评论

暂无评论...
验证码 换一张
取 消