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”,程序会告诉他们它们不正确。

您可以通过告诉比较使用不区分大小写的比较来避免区分大小写,例如使用or 通过使用orstring.Equals(a, b, StringComparison.CurrentCultureIgnoreCase); 将两个字符串转换为相同的大小写。String.ToLower(string)String.ToUpper(string)

您还需要更改 else if to else if (!Answers.AnswerOne.Equals(userAnswer))或 to just else,以便在给出错误答案时显示错误答案消息。

于 2012-10-27T14:09:16.140 回答
0

您正在比较Answers.AnswerOne"Austria"这将始终是正确的,因为这就是它的设置。

您需要做的是将您从用户(从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") 总是正确的。Answers 是您创建的包含正确答案的类。

您没有做的是将用户响应存储在变量中。您只是从控制台读取它,而不是保留它以进行比较。

于 2012-10-27T14:05:46.370 回答