测验总是告诉玩家他/她是正确的

时间:2012-10-27 14:01:33

标签: c#

我正在尝试在这里做一个简单的小测验,但无论用户输入什么类型,用户都会被告知他们有正确的答案,即使这是错误的。

这是我的源代码: 你在这里看到的第一堂课是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 个答案:

答案 0 :(得分:1)

您的代码中没有任何地方存储用户答案,您只需编写Console.ReadLine();并将正确答案与自身进行比较。

你可能想要像

这样的东西

string userAnswer = Console.ReadLine();

然后在正确答案和用户答案之间进行比较

if (Answers.AnswerOne.Equals(userAnswer))

请注意,字符串相等的Equals方法是字符敏感且区分大小写的,因此如果用户键入“Austria”或“austria”,程序会告诉他们它们不正确。

通过告知比较使用不区分大小写的比较,例如使用string.Equals(a, b, StringComparison.CurrentCultureIgnoreCase);或使用String.ToLower(string)String.ToUpper(string)将两个字符串转换为相同的大小写,可以避免此情况敏感。< / p>

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

答案 1 :(得分:0)

您正在将Answers.AnswerOne"Austria"进行比较,始终为真,因为是它设置的内容。

您需要做的是将您从用户(从Console.ReadLine)获得的内容与Answers.AnswerOne进行比较。

答案 2 :(得分:0)

您必须将console.readline的值放入字符串变量中,然后将其与answer1值进行比较。此外,请确保您检查案例,因为c#具有案例敏感性。

答案 3 :(得分:0)

Answers.AnswerOne.Equals(“奥地利”)永远是真的。答案是您创建的包含正确答案的类。

您未执行的操作是将用户响应存储在变量中。你只是从控制台阅读它,但没有保留它进行比较。