2017-12-04 7 views
-8

私は対話型のBMI計算機になるように最適化したいと思っています。 これは私のコードです。私は28のエラーがあります。これは私の最初のプログラムです。もし可能であれば、elpを出してください。

コード:

import java.util.Scanner;

クラスBMI {

公共の静的な無効メイン(文字列引数[]){

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

}

}

OK、今ここで報告である:

javac JavaTutorial.java

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:エラー: ')'

 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("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

 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を予想 ')'

 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が期待しました:エラー: ';'

のSystem.out.printlnは(! "!息子をくそー、あなたは()数字のxDさんに良い探しているのBMIで『』BMI、あなたはここで心配するちょうど約平均、何もない)と予想。

                                     ^

JavaTutorial.java:47:エラー:/、BMIと実際には: ')'

のSystem.out.println( "あなたは私を与えた数字を見ると、あなたが低体重であると思わ期待あなたの年齢を考慮していないので、状態が重要であるかどうかを伝えることはできませんが、体重を増やすことを考慮する必要があります。 ");

                              ^

JavaTutorial.java:47:エラー:と実際には、/:ない声明

のSystem.out.printlnは( "あなたは私を与えた数字を見ると、あなたが低体重であると思わあなたの体重は平均的な女の子のそれより低いです。あなたの年齢を考慮したではない、私は条件天気をあなたに伝えることができる方法はありません重要かではありませんが、あなたは本当にいくつかの重量を増して検討すべきである ");。

                               ^

JavaTutorial。java:47:エラー: ';'

System.out.println( "あなたが私に与えた数字を見れば、あなたは体重が減っているようだ/実際には" bmi "のbmiで体重が平均的な女の子よりも低いあなたの年齢を考慮して、状態が重要であるかどうかを伝える方法はありませんが、体重を増やすことを本当に考慮する必要があります。

                                                                                  ^

JavaTutorial.java:51:エラー: ')'

のSystem.out.println(予想BMI "「くそーgiiiirlを、あなたはのBMIを持つロック!"、あなたは理想的な標本です健康的な女性像、それをしてください)。

                ^

JavaTutorial.java:51:。!エラー:リテラル閉じられていない文字列の

のSystem.out.println( "、あなたがくそgiiiirlを揺する持つBMI! "bmi"、あなたは彼の理想的な標本ですalthy女性フィギュアがんばり続ける!); ^ JavaTutorial.java:51:エラー: ';'予想

のSystem.out.println( "、あなたがくそgiiiirlを揺するのBMI持つ! "BMI" を、あなたは健康的な女性像の理想的な標本です、それを続けてください。!); ^ JavaTutorial.java: 55:エラー: ')'が予想される

System.out.println( "まず、それは重要ではないが、BMIが" bmi "であるとすれば、あなたは太り過ぎと見なされます。あなたの健康に役立つでしょう。)

                     ^

JavaTutorial.java:55:エラー:閉じられていない文字列リテラル

のSystem.out.println( "BMIあなたを「まず第一に、しかしのBMIと、私はそれは重要ではないと言うてみましょう"あなたの健康に役立つでしょう。); ^ JavaTutorial.java:55:エラー: ';'最初に、それは重要ではないが、BMIが "bmi"であるとすれば、あなたは太り過ぎと見なされているか、時には動いているか、それは確かにあなたの健康に役立つでしょう。 ^ JavaTutorial.java:59:エラー:「)」(System.out.printlnは

を期待 "まず第一に、私はそれは重要ではないと言うてみましょう、しかし、のBMIと『BMI』、あなたが太りすぎと考えられています、ワークアウトまたは随時実行のために行くしてみてください、それは確かにあなたの健康を助ける); ^ JavaTutorial.java:59:エラー:閉じられていない文字列リテラル

のSystem.out.printlnの(「第すべて、私はそれが重要ではないと言ってみましょう、しかし、BMIが "bmi"の場合、あなたは太り過ぎと見なされ、運動を試みたり、時々走り回ったりして、あなたの健康を助けます。 ^ JavaTutorial.java:59:エラー: ';'最初に、それは重要ではないが、BMIが "bmi"であるとすれば、あなたは太り過ぎと見なされているか、時には動いているか、それは確かにあなたの健康を助けるでしょう。);

                                        ^

JavaTutorial.java:62:エラー:文のない

else if (bmi > 30) then { 

        ^

JavaTutorial.java:62:エラー: ';'予想される他

(BMI> 30)次に、{^ JavaTutorial.java:62場合:エラー: '他' せず、その後他

IF(BMI> 30){ ^

'であれば'

JavaTutorial.java:63:error: ')' expected

System.out.println( "私はあなたに知らせてもらえますが、BMIが" bmi "であると申し訳ありません。国民健康組織化(National Health Orgnaization)は、あなたの体重があなたの健康に深刻な影響を与えている可能性があります。ダイエット中に働き始めたり、極端な場合は専門的な助けを求めてください。自分でやってください、私はあなたを信じています! < 3「); ^ JavaTutorial.java:63:エラー:文のない

のSystem.out.printlnは(」私はあなたを通知するために一つであることがとても残念ですが、「BMI」のBMIとあなたは国家健康組織化によって肥満に悩まされていますが、体重があなたの健康に深刻な影響を与えている可能性があります。私はダイエットをしたり、仕事を始めたり、極端な場合はプロの助けを求めることを強くお勧めします。それを自分のために、私はあなたを信じている< 3" );

                      ^

JavaTutorial.java:63:!エラー: ';'

System.out.println( "BMIがBMIであることを知らせてくれて申し訳ありませんが、あなたは国家健康組織化によって肥満に悩まされています。あなたの健康に深刻な影響を与えることを強くお勧めします。私はダイエットをすることを強くお勧めします。仕事が始まったり、極端な場合は専門家の助けを求めてください。 .java:58:エラー: 'else'なし 'if'

else if (bmi > 25 && sex = "boy") { 


^

JavaTutorial.java:54:エラー: 'else' withouトン

else if (bmi > 25 && sex = "girl") { 


^

JavaTutorial.java:46 'の場合':エラー:解析中に到達したファイルの末尾

else if (bmi <= 18.5 && sex = "girl") { 


^

JavaTutorial.java:72:エラー:ファイルの終わりに達し解析中

}

^

28エラー

+0

は、このフォーラムのために、話題が何であるかを考えてみましょう:https://stackoverflow.com/help/on-topicは – tkruse

+0

あなたのコードで再び見ると、」複数のエラーがあり、これはあなたのコーディングスタイルがおそらく修正される必要があることを示唆しています。大量のコードを入力してテストしないでください。代わりに、NetBeansやEclipseなどの最新のIDEを使用できない場合(コンパイルの問題を直ちに警告します)、コード**を早く、頻繁に**、そして最も重要なのは**現在のコンパイルの問題が修正されるまで、新しいコードを追加しないでください**。さもなければ、エラーを修正するのは難しいラットの巣で終わるでしょう。 –

+0

私はあなたが –

答えて

0

私はあなたのためにそれを修正しました。間違いがいくつかありました。

  1. 比較は常にこのような2つの等号必要がある場合:String.equalsメソッドを使用してJava
  2. 常にcomare文字列での句の場合にはthenはありませんif(a == b){...}
  3. を。 StringはJavaのオブジェクトとして保存されます。 ==の2つのオブジェクトを比較すると、同じインスタンスを指す2つの変数でない限り、真を返すことはありません。この場合、オブジェクトの内容を比較しているので、なぜstring1.equals(string2)
  4. Idkですが、scanner.nextLine()は機能しませんでしたが、scanner.next()は使用してください。あなたは一言を求めているので、何の問題も起こらないはずです。私はscannerにあまり慣れていませんが、私のために働いていました;)
  5. 文字列に変数を挿入しようとすると、プラス記号("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!"); 

    } 
+1

を参照してください。あなたのコードをよりきれいにするために「スイッチケース」を調べることをお勧めします – Naeramarth

関連する問題