0

よし、4つの答えがある5つの質問からなるクイズを作ることになっている。次に、ユーザーはキーボード入力を介して質問に答えます。最後に、5 点満点中 5 点の場合、プログラムは「非常に良い」を出力し、4 点で「非常に良い」を出力し、3 点以下の場合は「地球温暖化に関する知識をブラッシュアップする時間」を出力する必要があります。質問の作成に使用された情報を含むサイトへのリンク。コーディングに含める必要があるのは、「Do-While」および「Switch」ステートメントです。もうすぐそこにあると思いますが、エラーが発生し続け、次に何をすべきかを理解するのに本当に苦労しています! 女性を助けますか?

編集ここに私が受け取ったエラーがあります: 「この行に複数のマーカーがあります-chooseAnswer1を解決できません-chooseAnswer1を変数に解決できません-answer1stringを変数に解決できません」 **

(私のコードは以下です)

import java.io.* ;
public class globalwarming {
    public static void main(String[] args) {

        InputStreamReader keyInput = new InputStreamReader(System.in) ;
        BufferedReader read = new BufferedReader(keyInput) ;

        try {
               System.out.println("1.   Carbon Dioxide (CO2)_____________;");
               System.out.println("A.   Is colorless, odorless, non-toxic, and non-combustible.");
               System.out.println("B.   Is produced when Carbon sources are burned (i.e. oil, coal, gas, wood…)");
               System.out.println("C.   Atmospheric concentration has increased by over 34% since 1960.");
               System.out.println("D.   All of the above");
               String chosenAnswer1 = read.readLine();
               int answer1 = 4;
               String answer1string = "" + answer1;
               String Question1answers;
               switch (answer1) {
                   case 1:  Question1answers = "A.  Is colorless, odorless, non-toxic, and non-combustible.";
                            break;
                   case 2:  Question1answers = "B.  Is produced when Carbon sources are burned (i.e. oil, coal, gas, wood…)";
                            break;
                   case 3:  Question1answers = "C.  Atmospheric concentration has increased by over 34% since 1960.";
                            break;
                   case 4:  Question1answers = "D.  All of the above";
                            break;
                   default: Question1answers = "No response selected";
                            break;
               } 
               System.out.println("2.   Greenhouse gases are;  ");
               System.out.println("A.   A myth created by popular media.");
               System.out.println("B.   Keep heat close to earth sustaining life, however is rapidly increasing heat levels, which is detrimental to the environment.");
               System.out.println("C.   Green colored gases that poison and kill plant life.");
               System.out.println("D.   Nothing to be concerned about, continue buying and consuming products that release CO2 emissions… Nothing to see here.");
               String chosenAnswer2 = read.readLine();
               int answer2 = 2;
               String answer2string = "" + answer2;
               String Question2answers;
               switch (answer2) {
                   case 1:  Question2answers = "A.  A myth created by popular media.";
                            break;
                   case 2:  Question2answers = "B.  Keep heat close to earth sustaining life, however is rapidly increasing heat levels, which is detrimental to the environment.";
                            break;
                   case 3:  Question2answers = "C.  Green colored gases that poison and kill plant life.";
                            break;
                   case 4:  Question2answers = "D.  Nothing to be concerned about, continue buying and consuming products that release CO2 emissions… Nothing to see here.";
                            break;
                   default: Question2answers = "No response selected";
                            break;
               } 
               System.out.println("3.   Smart Cars help combat global warming by,");
               System.out.println("A.   Reducing CO2 emissions slowing the rapid warming of the planets atmosphere.");
               System.out.println("B.   Consuming more energy thereby eliminating oil supplies.");
               System.out.println("C.   Require fewer resources to manufacture.");
               System.out.println("D.   None of the above.");
               String chosenAnswer3 = read.readLine();
               int answer3 = 1;
               String answer3string = "" + answer3;
               String Question3answers;
               switch (answer3) {
                   case 1:  Question3answers = "A.  Reducing CO2 emissions slowing the rapid warming of the planets atmosphere.";
                            break;
                   case 2:  Question3answers = "B.  Consuming more energy thereby eliminating oil supplies.";
                            break;
                   case 3:  Question3answers = "C.  Require fewer resources to manufacture.";
                            break;
                   case 4:  Question3answers = "D.  None of the above.";
                            break;
                   default: Question3answers = "No response selected";
                            break;
               } 
               System.out.println("4.   There is more carbon dioxide in the air today than;");
               System.out.println("A.   There ever has been before.");
               System.out.println("B.   Than at any other time in the last 800,000 years.");
               System.out.println("C.   Than there will be in 20 years.");
               System.out.println("D.   Both A and B.");
               String chosenAnswer4 = read.readLine();
               int answer4 = 2;
               String answer4string = "" + answer4;
               String Question4answers;
               switch (answer4) {
                   case 1:  Question4answers = "A.  There ever has been before.";
                            break;
                   case 2:  Question4answers = "B.  Than at any other time in the last 800,000 years.";
                            break;
                   case 3:  Question4answers = "C.  Than there will be in 20 years.";
                            break;
                   case 4:  Question4answers = "D.  Both A and B.";
                            break;
                   default: Question4answers = "No response selected";
                            break;
               } 
               System.out.println("5.   In the last century sea levels have risen how many inches?");
               System.out.println("A.   5 Inches");
               System.out.println("B.   0 Inches");
               System.out.println("C.   7 Inches");
               System.out.println("D.   22 Inches");
               String chosenAnswer5 = read.readLine();
               int answer5 = 3;
               String answer5string = "" + answer5;
               String Question5answers;
               switch (answer5) {
                   case 1:  Question5answers = "A.  5 Inches";
                            break;
                   case 2:  Question5answers = "B.  0 Inches";
                            break;
                   case 3:  Question5answers = "C.  7 Inches";
                            break;
                   case 4:  Question5answers = "D.  22 Inches";
                            break;
                   default: Question5answers = "No response selected";
                            break;
               }
        }
        int i = 5;
        String strI = "" + i;
        int count = 0;
        do {
            if (chosenAnswer1 == answer1string) {
                count++;
            }
            if (chosenAnswer2 == answer2) {
                count++;
            }
            if (chosenAnswer3 == answer3) {
                count++;
            }
            if (chosenAnswer4 == answer4) {
                count++;
            }
            if (chosenAnswer5 == answer5) {
                count++;
            }
        } while (count <= 5);
            if (count == 5) {
                System.out.println("Excellent!");
            } else if (count == 4) {
                System.out.println("Very good!");
            } else if (count > 3) {
                System.out.println("Time to brush up on your knowledge of global warming.");
                System.out.println("http://www.dosomething.org/actnow/tipsandtools/11-facts-about-global-warming");
            }
        /*
        System.out.println(Question1answers);
        System.out.println(Question2answers);
        System.out.println(Question3answers);
        System.out.println(Question4answers);
        System.out.println(Question5answers);
        */
    }
}
4

2 に答える 2

2

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html Cases は、メニューの順序を意味するものではありません。「指定された変数がこのケースの値と等しい場合」のようなものです。

switch (STRING) {
case STRING_A: do something.. break;
case STRING_B:  do something.. break;
}
switch (INTEGER) {
case 1: do something.. break;
case 2:  do something.. break;
}

この変数は、try コンテキストで定義されます。トライ以外では使用できません。

String chosenAnswer1 = read.readLine();

次のようなことをする必要があります。

 String chosenAnswer1 = null;
        try {
        ...
        chosenAnswer1 = read.readLine();

他のすべての selectedAnswers と answer1strings についても同じ

try に catch/finally がありません。そうでなければ、なぜ try を使用するのでしょうか?

http://docs.oracle.com/javase/tutorial/essential/exceptions/try.html

read.readLine();これらの行は、try catch でラップするか、メソッドで例外のスローを許可する必要があります。

文字列は不変であるため、stringA==stringB は文字列が同じかどうかを評価しません。2 つの文字列が同じオブジェクトかどうかを評価します....

String a = "hello";
String b = "hello";
//a==b is false.
//a.equals(b) is true
String a = "hello";
String b = a;
//a==b is true.
//a.equals(b) is true
于 2013-10-02T00:06:52.133 に答える
1

for 文字列の使用が==正しくありません。.equals()たとえば、この行とその下の他のすべての行を使用して、文字列が等しいかどうかをチェックする必要があります。

if (chosenAnswer1 == answer1string) {

これは

if (chosenAnswer1.equals(answer1string)) {
于 2013-10-02T00:04:12.053 に答える