私は、ユーザーに質問して合計を求めるJavaプログラムを持っています。私は、プログラムの最後に答えられた質問の正しい合計を表示しようとしています。しかし、私はどのようにするか分からない、いくつか私を助けてください! 私のコードがlのように見える例です:しかし、これは私の実際のコードではありません、これは別のソースからです!Java!ユーザーが質問に答えるのを終えた後、正解と不正解の質問を表示するには
Import java.util.Scanner;
public class App
{
public static void main(String[] args) {
// The array of questions.
String questions[] = {
"Plants derive most of their dry mass from the air.",
"Aluminium is the most common metal in the Earth's crust.",
"Vitamin C has be shown to prevent colds.",
"We lose the most heat through our heads.",
"Dogs are unable to digest chocolate.",
"Apple pips contain cyanide.",
"Cholesterol is a nat
"When you're on a diet, you lose weight by oxidising fat to a gas and exhaling it.",
"Human beings are unable to sense when the oxygen level of the air is low.",
"Most of the Y chromosome is passed unchanged from father to son" };
// The array of answers. The entries correspond to the questions.
boolean answers[] = { true, true, false, false, false, true, false,
true, true, true };
// Display the opening text.
System.out.println("Answer each of the following questions with 't' (true) or 'f' (false)");
// We'll use this to get user input.
Scanner input = new Scanner(System.in);
// Add up the user's score here as we go along.
int score = 0;
// The is the index into the questions array and the answers array.
int questionNumber = 0;
// Create a blank line.
System.out.println();
// The do-while loop will keep running while questionNumber is less
// than the question array length.
do {
// Display the question
System.out.println(questions[questionNumber]);
// Display a little prompt to make it clearer that the user has to
// enter something.
System.out.print("> ");
// Get the user's answer.
String userAnswer = input.nextLine();
// Check that the user has entered t or f.
if (!userAnswer.equals("t") && !userAnswer.equals("f")) {
System.out.println("Please enter t for true or f for false.\n");
// Invalid input!
// Skip the rest of this loop iteration and ask the same question again.
continue;
}
// Check the answer.
if (userAnswer.equals("t") && answers[questionNumber] == true) {
// If the answer's t and the right answer is "true", the answer was correct.
score++;
System.out.println("correct\n");
} else if (userAnswer.equals("f") && answers[questionNumber] == false) {
// If the answer's f and the correct answer is "false", the answer was correct.
System.out.println("correct\n");
score++;
}
else {
// Wrong answer!
System.out.println("incorrect!\n");
}
// Now we can move to the next question when we go round the loop again.
questionNumber++;
} while (questionNumber < questions.length); // end of do-while.
// This isn't really necessary, but closing the Scanner prevents a warning icon in Eclipse.
input.close();
// Tell the user their score.
System.out.println("You scored: " + score);
// Rank the score! Only one of the alternatives below will execute.
// Java will check them in order from top to bottom.
if(score < 5) {
// Less than 5 -- not so good.
System.out.println("Hmmm, maybe you're the artistic type. Try the test again!");
}
else if(score < 8) {
// The score wasn't less than 5, but it IS less than 8.
System.out.println("Not bad! But have another go.");
}
else if(score <= 9) {
// The score wasn't less than 8, but it IS less than, or equal to, 9.
System.out.println("Pretty good! But no perfect. Try again!");
}
else {
// The score was more than 9 -- must be 10 because we've only got 10 questions.
System.out.println("You're a certified science genius!");
}
}
}
:あなたはこのような出力を行うことができます)DO-whileループの外challenge-science-quiz.html)(目に見えるwhe n "Click for example solution"ボタンをクリックします)。あなたのコードではありません。 – RealSkeptic
ええ、それはまさにそれのような何かに取り組んでいます!だから私は無料のソースコードとしてそれを見つけました。なぜそれを例として掲示しないのかを理解して、何を言いたいのですか? –
ここに私の電話を使って、私のコードは私のシステムにあります。私はそれがあなたの問題ではないことを願っています! –