gpt4 book ai didi

c# - 如何存储猜谜游戏的分数?

转载 作者:行者123 更新时间:2023-11-30 22:01:55 24 4
gpt4 key购买 nike

我是新来的,也是编写 C# 程序的新手。谁能帮我在 C# 控制台中玩猜谜游戏?我需要将玩家的猜测限制为 10 次尝试,并且得分是猜测次数的负 10。当玩家赢了,如果想再玩就回答no,则游戏结束,显示所有玩家的得分和他们的平均得分。我怎样才能开始计分代码?有人可以给我任何想法或可用于模式的代码示例吗?谢谢你

这是我的代码:

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

namespace Midterm
{
class Program
{
int answer;
int guess;
static void Main(string[] args)
{
Program p = new Program();
p.Opening();
p.randomize();

while (true)
{
p.userData();
p.Display();
}

}

//method for the introduction
private void Opening()
{
Console.WriteLine("Welcome to the Guessing Game! Pless any key to continue!");
Console.ReadLine();
}

//method for getting user input
private void userData()
{
Console.WriteLine("Enter your guess number.");
guess = int.Parse(Console.ReadLine());

}

//method for display
private void Display()
{

if (guess == answer)
{
Console.WriteLine("Congratulation. You won!");
Console.WriteLine("Score: ");
PlayAgain();
}
else if (guess > answer)
{
Console.WriteLine("Your guess is high");
}
else if (guess < answer)
{
Console.WriteLine("Your guess is low");
}

}

private void Score()
{
int[] score = new int[100];

}

private void AverageScore()
{


}

//method for playing again question
private void PlayAgain()
{
Console.WriteLine("Do you want to play again? Yes or No");
string respond = Console.ReadLine().ToLower();
if (respond == "yes")
{
Opening();
randomize();

while (true)
{
userData();
Display();
}

}
else if (respond == "no")
{
EndProgram();
}
else
{
Console.WriteLine("You enter a wrong respond.");
Console.WriteLine("Will automatic exit you");
EndProgram();
}
}

//method for the random number generator
private void randomize()
{
Random rand = new Random();
answer = rand.Next(1, 500);
}

//method for end program
private void EndProgram()
{
Console.WriteLine("\n");
Console.WriteLine("Press any key to Exit");
Console.ReadKey();
}

}
}

最佳答案

向 userData() 方法添加一个计数器。每次猜测后增加计数器。

在 Display() 方法中使用计数器计算分数。

考虑从 Display() 中调用 userData()。

if (attempts < 10 && answer != guess) 
Console.WriteLine("some feedback");
userData();

这是主观的,但如果我正在编写这个程序,我可能会围绕评分系统构建反馈循环。

示例伪代码:

private void start() {
display welcome message;
answer = new random number;
guess = getInput();
score = 10;
win = false;

while (score > 0 && win == false) {
if ( !evaluateGuess(answer, guess) ) {
guess = getInput();
score--;
}
else if ( evaluateGuess(answer, guess) ) {
display winning message and score;
win = true;
playAgain();
}
}
}

private void evaluateGuess(answer, guess) {
if (answer == guess) {
display feedback;
return true;
}
else {
display other feedback;
return false;
}
}

关于c# - 如何存储猜谜游戏的分数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27339795/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com