我是新來的,編程一個c#程序。任何人都可以幫助我在c#控制檯的猜謎遊戲?我需要限制玩家的猜測次數爲10次,得分爲-10的猜測次數。當玩家獲勝時,如果他們想再次玩,則迴應否,它將結束遊戲顯示所有玩家的得分和他們的平均得分。我如何開始評分代碼?有人可以給我任何想法或我可以用於模式的代碼示例嗎?謝謝如何存儲猜謎遊戲的分數?
這裏是我的代碼:
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();
}
}
}
我建議你不要不用數組來表示每場比賽的比分,而是使用「列表」。這樣,您可以不斷添加新分數,並且列表會自動變長,然後您可以簡單地使用LINQ'.Average()'方法來獲取平均值。 –
Enigmativity
2014-12-07 06:22:31