好吧,我应该做一个由 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);
*/
}
}