0

好吧,我应该做一个由 5 个问题和 4 个可能答案组成的测验。然后用户将通过键盘输入回答问题。最后,如果他们有 5 分中的 5 分,则程序应该超过,“优秀”在 4 时应该输出“非常好”,在 3 分或更少时应该超过“是时候复习你对全球变暖的知识了”以及链接到包含用于创建问题的信息的站点。编码中必须包含“Do-While”和“Switch”语句。我认为它几乎就在那里,但我不断收到错误,我真的很难弄清楚下一步该做什么!帮小姐姐解围?

编辑这是我收到的错误: “此行的多个标记 - 无法解析 selectedAnswer1 - selectedAnswer1 无法解析为变量 - 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 案例并不意味着菜单中的顺序,它们就像“如果给定变量等于这个案例值”

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();

所有其他选择的答案和 answer1strings 相同

您的尝试缺少捕获/最终,否则您为什么要使用尝试?

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

read.readLine();这些行需要用 try catch 包裹,或者该方法应该允许抛出异常。

字符串是不可变的,因此 stringA==stringB 不会评估字符串是否相同,它会评估两个字符串是否相同 Object....

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

==对字符串的使用不正确。.equals()应使用例如此行及其下方的所有其他行来检查字符串是否相等:

if (chosenAnswer1 == answer1string) {

这应该是

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