2013-10-02 34 views
0

好吧,我应该做一个包含5个问题和4个可能的答案的测验。用户将通过键盘输入回答问题。最后,如果他们有五分之五的项目应该打印出来,4的“优秀”应该输出“非常好”,在3或更少的时候应该打印出“时间来刷新你对全球变暖的认识”以及链接到用于创建问题的信息的网站。包括在编码中必须是“Do-While”和“Switch”语句。我认为它几乎在那里,但我不断收到错误,我真的很难找出接下来要做的事情!帮助一位女士出去?循环测验,Java

编辑这里是我收到的错误: “在这一行 多个标志 - chosenAnswer1解决不了 - chosenAnswer1不能被解析为 可变 - 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); 
     */ 
    } 
} 
+1

最好的办法是学习使用调试器并使用它。 –

+1

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html – porfiriopartida

+0

请将您遇到的错误添加到问题中,这有助于回答很多问题。 –

回答

1

您使用==字符串不正确。字符串应该使用.equals()例如这条线的平等,进行检查,并在其下方所有其他:

if (chosenAnswer1 == answer1string) { 

这应该是

if (chosenAnswer1.equals(answer1string)) { 
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(); 

同样对于所有其他chosenAnswers和answer1strings

你试试缺少捕获/终于,否则为什么你会使用try ?

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

read.readLine();这些线需要通过尝试捕捉或方法应该允许异常抛出包裹。

字符串是不可改变的,因此如果字符串相同stringA == stringB不评估,如果两个字符串是相同的对象,但它会评估....

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