0

ここで簡単なクイズを作成しようとしていますが、ユーザーがどのように入力しても、間違っていても、ユーザーには常に正しい答えがあると言われます。

これが私のソースコードです:ここに表示される最初のクラスはProgram.csです:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TheQuiz
{
class Program
{
    static void Main(string[] args)
    {

        Console.WriteLine("Welcome to the Quiz! \nYou'll be asked 20 questions! If you get one question wrong, you'll be out.\nHowever if you answer all 20 questions correctly, you'll be the MASTER!");
        Console.WriteLine("\nLet us begin! Are you ready?\n\n");

        Console.WriteLine("(Remember, TYPE in the correct answer. Every answer has ONE word only!\nAnd ALWAYS use a capital letter at the beginning of the answer: 'Answer')\n\n(Press Enter to continue...)");
        Console.ReadKey();
        Console.Clear();

        Console.WriteLine("In what country was Adolf Hitler born?");

        Console.ReadLine();

        if (Answers.AnswerOne.Equals("Austria"))
        {
            Console.WriteLine("Congratulations! {0} is the correct answer!", Answers.AnswerOne);
        }
        else if (!Answers.AnswerOne.Equals(Answers.AnswerOne))
        {
            Console.WriteLine("Sorry! You wrote the wrong answer!\nThe correct answer was {0]", Answers.AnswerOne);
        }
        Console.ReadKey();


    }
}
}

これが私の他のクラス、Answers.csです:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TheQuiz
{
public class Answers
{

    public static string AnswerOne = "Austria";
    public static string AnswerTwo = "";
    public static string AnswerThree = "";
    public static string AnswerFour = "";
    public static string AnswerFive = "";
    public static string AnswerSix = "";
    public static string AnswerSeven = "";
    public static string AnswerEight = "";
    public static string AnswerNine = "";
    public static string AnswerTen = "";
    public static string AnswerEleven = "";
    public static string AnswerTwelve = "";
    public static string AnswerThirteen = "";
    public static string AnswerFourteen = "";
    public static string AnswerFifteen = "";
    public static string AnswerSixteen = "";
    public static string AnswerSeventeen = "";
    public static string AnswerEighteen = "";
    public static string AnswerNineteen = "";
    public static string AnswerTwenty = "";

}
}

したがって、正解はオーストリアですが、ユーザーがドイツのような他の何かを入力した場合でも、それが正解であることを示しています。

前もって感謝します。

4

4 に答える 4

1

コードのどこにもユーザーの回答を保存しませんConsole.ReadLine();。正しい回答を記述してそれ自体と比較するだけです。

あなたはおそらく次のようなものが欲しい

string userAnswer = Console.ReadLine();

次に、正解とユーザーの回答の比較

if (Answers.AnswerOne.Equals(userAnswer))

文字列を等価にするための Equals メソッドは、文字と大文字と小文字を区別するため、ユーザーが「Austria」または「austria」と入力すると、プログラムはそれらが正しくないことを通知することに注意してください。

またはを使用string.Equals(a, b, StringComparison.CurrentCultureIgnoreCase); して両方の文字列を同じ大文字と小文字に変換するなど、 Case Insensitive Comparison を使用するように比較に指示することで、大文字と小文字が区別されることを回避します。String.ToLower(string)String.ToUpper(string)

else if (!Answers.AnswerOne.Equals(userAnswer))間違った答えが与えられたときに間違った答えのメッセージが表示されるようにするには、else if を to または just else に変更する必要もあります 。

于 2012-10-27T14:09:16.140 に答える
0

と比較Answers.AnswerOneしていますが、それが設定されているため"Austria"、これは常にtrue になります

あなたがする必要があるのは、ユーザーから ( からConsole.ReadLine)得たものを と比較することですAnswers.AnswerOne

于 2012-10-27T14:04:23.550 に答える
0

console.readline の値を文字列変数に入れ、それを answer1 の値と比較する必要があります。また、c# では大文字と小文字が区別されるため、大文字と小文字を必ず確認してください。

于 2012-10-27T14:04:55.687 に答える
0

Answers.AnswerOne.Equals("Austria") は常に true です。Answers は、正しい Answers を保持する、作成したクラスです。

あなたがしていないのは、ユーザーの応答を変数に保存することです。コンソールから読み取っているだけですが、比較のために保持していません。

于 2012-10-27T14:05:46.370 に答える