0
私は数日前にJavaカップルを始めました。現在、この「コース」http://programmingbydoing.comに従っています。 haventはは何の問題に遭遇していた、まだ今は今のところ(常に出力として代わりにムースのリスを取得)タスク32If/elseステートメント
HERESに私のコードで立ち往生イム:
import java.util.Scanner;
パブリッククラスTwoQuestion32 {
public static void main(String[] args) {
boolean animal, vegetable, mineral, smallerthan;
String whatIsIt, biggerThan;
Scanner keyboard = new Scanner(System.in);
System.out.println("Hello and welcome, i've got 2 questions for you!");
System.out.println("Think of an object and i'll try to guess it");
System.out.println();
System.out.println("Question 1) Is it an animal, vegetable or mineral?");
System.out.print(">");
whatIsIt = keyboard.nextLine();
if (whatIsIt == "animal")
animal = true;
if (whatIsIt == "vegetable")
vegetable = true;
if (whatIsIt == "mineral")
mineral = true;
System.out.println("Question 2) Is it bigger than a breadbox?");
System.out.print(">");
biggerThan = keyboard.nextLine();
if (biggerThan == "yes")
smallerthan = false;
if (biggerThan == "no"){
smallerthan = true;}
System.out.print("My guess is that you are thinking of a ");
if (animal = true){
if (smallerthan = true)
System.out.println("squirrel");
}else {
System.out.println("moose");}
}
}
を
ありがとうございます!よりスマートな方法でコードを記述するヒントを聞くことも大好きです。フレンドリーで、私はちょうど始めたことを覚えておいてください!
編集:もう一度別のアプローチをとった。私の最初の試みは本当に奇妙でした。助けてくれてありがとう!
HERESに取り組んコード:
import java.util.Scanner;
パブリッククラスQuestions32 {あなたのif文あなたの代わりにそれ(==
)をチェックする、真のたびif (animal = true){
に動物を設定している中
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String whatIsIt, whatIsIt2;
String animal = "animal";
String mineral = "mineral";
String vegetable = "vegetable";
String bigger = "yes";
String smaller = "no";
System.out.println("Hello and welcome, i've got 2 questions for you!");
System.out.println("Think of an object and i'll try to guess it");
System.out.println();
System.out.println("Question 1) Is it an animal, vegetable or mineral?");
System.out.print(">");
whatIsIt = keyboard.nextLine();
System.out.println("Question 2) Is it bigger than a breadbox?");
System.out.print(">");
whatIsIt2 = keyboard.nextLine();
if (whatIsIt.equalsIgnoreCase(animal)){
if (whatIsIt2.equalsIgnoreCase(bigger)){
System.out.println("My guess is that you are thinking of a moose");
}else{ System.out.println("My guess is that you are thinking of a squirrel");
}
}
if (whatIsIt.equalsIgnoreCase(vegetable)){
if (whatIsIt2.equalsIgnoreCase(bigger)){
System.out.println("My guess is that you are thinking of a melon");
}else{ System.out.println("My guess is that you are thinking of a carrot");
}
}
if (whatIsIt.equalsIgnoreCase(mineral)){
if (whatIsIt2.equalsIgnoreCase(bigger)){
System.out.println("My guess is that you are thinking of a Camaro");
}else{ System.out.println("My guess is that you are thinking of a paper clip");
}
}
System.out.println("I would ask you if I'm right, but I dont actually care.");
}
}
文字列を '=='や '!='と比較しないでください。これは、2つのString変数が同じString *オブジェクト*を参照し、それがあなたが望むものではないことを、* references *と比較することを理解する。その代わりに、*関数の等価性*テストを実行するString equals(...)またはそのequalsIgnoreCase(...)メソッドのどちらかを使用してください - 文字列は同じ順序で同じ文字を持っていますあなたが欲しいもの。 –
割り当てと関係の平等の違いについても読んでください。ボーナスについては、yodaの表現を読んでください。たとえ午前3時のデバッギングセッションでのみ、その差異が恒久的に強化されます。 – Bathsheba
あなたはどのIDEを使用しているのかよくわかりませんが、if-conditionの中で割り当てを行うことについては大きな警告を受けているはずです。これらの警告を無視することは、特に初心者としてバグを導入するための確実な方法です。 –