我试图在我想要优化成为交互式BMI计算器时尝试制作某种原始形式。 所以这是我的代码。
CODE:
import java.util.Scanner;
class bmi {
public static void main(String args []){
Scanner userinput = new Scanner(System.in);
double weight, height, bmi;
String sex;
System.out.println("Hello there!");
System.out.println("I am your Body-Mass index calculator!");
System.out.println("Would you please be so kind and tell me your weight? In kilograms if possible, I'm from Europe xD.");
weight = userinput.nextDouble();
System.out.println("Alrighty, now tell me, how tall are you? In meters this time.");
height = userinput.nextDouble();
System.out.println("Finally, can you tell me wether you are a boy or a girl, if that isn't too intrusive... *wink*");
sex = userinput.nextLine();
bmi = weight / (height * height);
if (sex = "boy") {
System.out.println("Thank you for providing me this information, bro!");
System.out.println(" ");
System.out.println("Hmm, it looks like your results came...");
}
else {
System.out.println("Thanks for the indormation, sys! ;)");
System.out.println(" ");
System.out.println("Hmm, it looks like your results came...");
}
System.out.println(" ");
if (bmi <= 18.5 && sex = "boy") {
System.out.println("Looking just at your height and weight, I can tell you are a skinny guy, in fact the data (your BMI is "bmi") shows that you are underweight. You might consider gaining some wight, it would be best for your health.");
}
else if (bmi <= 25 && sex = "boy") {
System.out.println("Damn son, you're looking good (in numbers xD)! With a BMI of "bmi", you are just about average, nothing to worry about here!);
}
else if (bmi <= 18.5 && sex = "girl") {
System.out.println("Looking at the numbers you gave me, it seems that you are underweight :/, in fact with a bmi of "bmi",your weight is below that of an average girl. Not having considered your age, there is no way I can tell you wether the condition is critical or not, but you should really consider gaining some weight.");
}
else if (bmi <= 25 && sex = "girl") {
System.out.println("Damn giiiirl, you rock! Having a BMI of "bmi", you are an ideal specimen of a healthy female figure. Keep it up!);
}
else if (bmi > 25 && sex = "girl") {
System.out.println("First of all, let me say it's not critical, but with a BMI of "bmi", you are considered overweight, try working out or going for a run from time to time, it sure will help your health.);
}
else if (bmi > 25 && sex = "boy") {
System.out.println("First of all, let me say it's not critical, but with a BMI of "bmi", you are considered overweight, try working out or going for a run from time to time, it sure will help your health.);
}
else if (bmi > 30) then {
System.out.println("I am so sorry to be the one to inform you, but with a BMI of "bmi",you are consiered obese by the National Health Orgnaization, at this point, your weight might be seriously affecting your health, and I strongly suggest you to take on a diet, start working out or if the case is extreme, seek professional help. Do it for yourself, I believe in you! <3");
}
System.out.println(" ");
System.out.println("Thank you for letting me help!");
}
}
好的,现在是报告:
javac JavaTutorial.java
JavaTutorial.java:39:错误:&#39;)&#39;预期
System.out.println("Looking just at your height and weight, I can tell you are a skinny guy, in fact the data (your BMI is "bmi") shows that you are underweight. You might consider gaining some wight, it would be best for your health.");
^
JavaTutorial.java:39:错误:不是声明
System.out.println("Looking just at your height and weight, I can tell you are a skinny guy, in fact the data (your BMI is "bmi") shows that you are underweight. You might consider gaining some wight, it would be best for your health.");
^
JavaTutorial.java:39:错误:&#39;;&#39;预期
System.out.println("Looking just at your height and weight, I can tell you are a skinny guy, in fact the data (your BMI is "bmi") shows that you are underweight. You might consider gaining some wight, it would be best for your health.");
^
JavaTutorial.java:43:错误:&#39;)&#39;预期
System.out.println("Damn son, you're looking good (in numbers xD)! With a BMI of "bmi", you are just about average, nothing to worry about here!);
^
JavaTutorial.java:43:error:unclosed string literal
System.out.println("Damn son, you're looking good (in numbers xD)! With a BMI of "bmi", you are just about average, nothing to worry about here!);
^
JavaTutorial.java:43:错误:&#39;;&#39;预期
System.out.println(&#34;该死的儿子,你看起来很好(数字xD)!BMI为&#34; bmi&#34;,你只是平均水平,没什么担心在这里!);
^
JavaTutorial.java:47:错误:&#39;)&#39;预期
System.out.println(&#34;看看你给我的数字,似乎你体重不足:/,实际上是bmi&#34; bmi&#34;,你的体重低于一个普通女孩。没有考虑过你的年龄,我无法告诉你病情是否严重,但你应该考虑增加体重。&#34;);
^
JavaTutorial.java:47:错误:不是声明
System.out.println(&#34;看看你给我的数字,似乎你体重不足:/,实际上是bmi&#34; bmi&#34;,你的体重低于一个普通女孩。没有考虑过你的年龄,我无法告诉你病情是否严重,但你应该考虑增加体重。&#34;);
^
JavaTutorial.java:47:错误:&#39;;&#39;预期
System.out.println(&#34;看看你给我的数字,似乎你体重不足:/,实际上是bmi&#34; bmi&#34;,你的体重低于一个普通女孩。没有考虑过你的年龄,我无法告诉你病情是否严重,但你应该考虑增加体重。&#34;);
^
JavaTutorial.java:51:错误:&#39;)&#39;预期
System.out.println(&#34;该死的giiiirl,你摇滚!BMI为&#34; bmi&#34;,你是健康女性形象的理想标本。坚持下去!); < / p>
^
JavaTutorial.java:51:错误:未关闭的字符串文字
System.out.println(&#34;该死的giiiirl,你摇滚!BMI为&#34; bmi&#34;,你是健康女性形象的理想标本。坚持下去!); ^ JavaTutorial.java:51:错误:&#39;;&#39;预期
System.out.println(&#34;该死的giiiirl,你摇滚!BMI为&#34; bmi&#34;,你是健康女性形象的理想标本。坚持下去!); ^ JavaTutorial.java:55:错误:&#39;)&#39;预期
System.out.println(&#34;首先,让我说它并不重要,但BMI为&#34; bmi&#34;,你被认为超重,尝试锻炼或者不时去跑步,这肯定会有助于你的健康。);
^
JavaTutorial.java:55:错误:未关闭的字符串文字
System.out.println(&#34;首先,让我说它并不重要,但BMI为&#34; bmi&#34;,你被认为超重,尝试锻炼或者不时去跑步,这肯定会有助于你的健康。); ^ JavaTutorial.java:55:错误:&#39;;&#39;预期
System.out.println(&#34;首先,让我说它并不重要,但BMI为&#34; bmi&#34;,你被认为超重,尝试锻炼或者不时去跑步,这肯定会有助于你的健康。); ^ JavaTutorial.java:59:错误:&#39;)&#39;预期
System.out.println(&#34;首先,让我说它并不重要,但BMI为&#34; bmi&#34;,你被认为超重,尝试锻炼或者不时去跑步,这肯定会有助于你的健康。); ^ JavaTutorial.java:59:error:unclosed string literal
System.out.println(&#34;首先,让我说它并不重要,但BMI为&#34; bmi&#34;,你被认为超重,尝试锻炼或者不时去跑步,这肯定会有助于你的健康。); ^ JavaTutorial.java:59:错误:&#39;;&#39;预期
System.out.println(&#34;首先,让我说它并不重要,但BMI为&#34; bmi&#34;,你被认为超重,尝试锻炼或者不时去跑步,这肯定会有助于你的健康。);
^
JavaTutorial.java:62:错误:不是声明
else if (bmi > 30) then {
^
JavaTutorial.java:62:错误:&#39;;&#39;预期
否则if(bmi> 30)然后{ ^ JavaTutorial.java:62:错误:&#39;否则&#39;没有&#39;如果&#39;
否则if(bmi> 30)然后{ ^
JavaTutorial.java:63:错误:&#39;)&#39;预期
System.out.println(&#34;我很遗憾成为通知你的人,但BMI为&#34; bmi&#34;,您被国家健康机构认定为肥胖,这一点,你的体重可能会严重影响你的健康,我强烈建议你节食,开始锻炼或者如果情况极端,请寻求专业帮助。为自己做,我相信你!&lt; 3& #34); ^ JavaTutorial.java:63:错误:不是声明
System.out.println(&#34;我很遗憾成为通知你的人,但BMI为&#34; bmi&#34;,您被国家健康机构认定为肥胖,这一点,你的体重可能会严重影响你的健康,我强烈建议你节食,开始锻炼或者如果情况极端,请寻求专业帮助。为自己做,我相信你!&lt; 3& #34);
^
JavaTutorial.java:63:错误:&#39;;&#39;预期
System.out.println(&#34;我很遗憾成为通知你的人,但BMI为&#34; bmi&#34;,您被国家健康机构认定为肥胖,这一点,你的体重可能会严重影响你的健康,我强烈建议你节食,开始锻炼或者如果情况极端,请寻求专业帮助。为自己做,我相信你!&lt; 3& #34); ^ JavaTutorial.java:58:错误:&#39;否则&#39;没有&#39;如果&#39;
else if (bmi > 25 && sex = "boy") {
^
JavaTutorial.java:54:错误:&#39;否则&#39;没有&#39;如果&#39;
else if (bmi > 25 && sex = "girl") {
^
JavaTutorial.java:46:错误:解析时到达文件末尾
else if (bmi <= 18.5 && sex = "girl") {
^
JavaTutorial.java:72:错误:解析时到达文件末尾
}
^
28个错误
答案 0 :(得分:0)
我为你修好了。有几个错误。
if(a == b){...}
then
String.equals
方法处理字符串。 String在java中保存为对象。将两个对象与==
进行比较永远不会返回true,除非它们是指向同一实例的两个变量。在这种情况下,您要比较对象的内容,这就是您应该使用string1.equals(string2)
scanner
不太熟悉,但它对我有用;)"something written here " + someVariable + "something written over there"
。这是固定代码:
public static void main(String[] args) {
Scanner userinput = new Scanner(System.in);
double weight, height, bmi;
String sex;
System.out.println("Hello there!");
System.out.println("I am your Body-Mass index calculator!");
System.out.println(
"Would you please be so kind and tell me your weight? In kilograms if possible, I'm from Europe xD.");
weight = userinput.nextDouble();
System.out.println("Alrighty, now tell me, how tall are you? In meters this time.");
height = userinput.nextDouble();
System.out.println(
"Finally, can you tell me wether you are a boy or a girl, if that isn't too intrusive... *wink*");
sex = userinput.nextLine();
bmi = weight / (height * height);
if ( sex.equals("boy")) {
System.out.println("Thank you for providing me this information, bro!");
System.out.println(" ");
System.out.println("Hmm, it looks like your results came...");
} else {
System.out.println("Thanks for the information, sys! ;)");
System.out.println(" ");
System.out.println("Hmm, it looks like your results came...");
}
System.out.println(" ");
if (bmi <= 18.5 && sex.equals("boy")) {
System.out.println(
"Looking just at your height and weight, I can tell you are a skinny guy, in fact the data (your BMI is "
+ bmi
+ ") shows that you are underweight. You might consider gaining some wight, it would be best for your health.");
} else if (bmi <= 25 && sex.equals("boy")) {
System.out.println("Damn son, you're looking good (in numbers xD)! With a BMI of " + bmi
+ ", you are just about average, nothing to worry about here!");
} else if (bmi <= 18.5 && sex.equals("girl")) {
System.out.println(
"Looking at the numbers you gave me, it seems that you are underweight :/, in fact with a bmi of "
+ bmi
+ ",your weight is below that of an average girl. Not having considered your age, there is no way I can tell you wether the condition is critical or not, but you should really consider gaining some weight.");
} else if (bmi <= 25 && sex.equals("girl")) {
System.out.println("Damn giiiirl, you rock! Having a BMI of " + bmi
+ ", you are an ideal specimen of a healthy female figure. Keep it up!");
} else if (bmi > 25 && sex.equals("girl")) {
System.out.println("First of all, let me say it's not critical, but with a BMI of " + bmi
+ ", you are considered overweight, try working out or going for a run from time to time, it sure will help your health.");
} else if (bmi > 25 && sex.equals("boy")) {
System.out.println("First of all, let me say it's not critical, but with a BMI of " + bmi
+ ", you are considered overweight, try working out or going for a run from time to time, it sure will help your health.");
} else if (bmi > 30) {
System.out.println("I am so sorry to be the one to inform you, but with a BMI of " + bmi
+ ",you are consiered obese by the National Health Orgnaization, at this point, your weight might be seriously affecting your health, and I strongly suggest you to take on a diet, start working out or if the case is extreme, seek professional help. Do it for yourself, I believe in you! <3");
}
System.out.println(" ");
System.out.println("Thank you for letting me help!");
}