猜猜游戏和数字比较

时间:2010-02-03 08:34:41

标签: c# algorithm

我不明白如何将文件中的数字与从函数传递的数字进行比较。

这是我的代码:

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

namespace Guess_Game
{
  class Program
  {
    static void Main(string[] args)
    {
        int quantity;
        int min, max;
        Console.WriteLine("Enter the Quantity of Numbers : ");
        quantity = int.Parse(Console.ReadLine());

        Console.WriteLine("Enter the Maximum and Minimum Number that you wish to be in your file : \n");
        Console.WriteLine("MIN : ");
        min = int.Parse(Console.ReadLine());
        Console.WriteLine("MAX : ");
        max = int.Parse(Console.ReadLine());
        Console.WriteLine("Now, Guess the Number : ");
        int number = int.Parse(Console.ReadLine());

        Console.WriteLine("Now Guess a Number That already contains in a File : ");
         number = int.Parse(Console.ReadLine());
         GuessGame(min, max, quantity, number);

        //if(number!= // number not equal to that number that contains in a file.. how to write it in that 
            //if condition..
    }

    static void GuessGame(int min,int max,int quantity,int number)
    {
        Random r = new Random();
        StreamWriter sw = new StreamWriter(@"D:\nasir\Guess Game\Guess Game\bin\Debug\ Guess Game.txt");

        for (int i = 0; i < quantity; i++)
        {
            int temp = r.Next(min, max);
            sw.WriteLine(temp);
        }
       // while(number != sw.WriteLine())
        {
            Console.WriteLine("Your Guess is Wrong!");
        }
        sw.Close();
    }
  }
}

3 个答案:

答案 0 :(得分:2)

你每次都在掷骰子(不公平,IMO)并且不会读取该文件,而只是写出来。让我们从这开始:

static void Main()
{
    Random r = new Random();
    bool lucky = false;
    int maxTries, minValue, maxValue, guess = 0;

    GetInput("Enter number of tries: ", out maxTries);
    GetInput("Enter minimum number : ", out minValue);
    GetInput("Enter maximum number : ", out maxValue);

    int magical = r.Next(minValue, maxValue); // only once
    for (int i = 1; i <= maxTries; i++)
    {
        GetInput("Enter your guess : ", out guess);
        if (guess == magical)
        {
            lucky = true;
            break;
        }
    }

    Console.WriteLine("you.Lucky = {0};", lucky);
    Console.ReadLine();
}

static void GetInput(string text, out int variable)
{
    do Console.Write(text);          // avoiding stackoverflow.com scroll
    while (!Int32.TryParse(Console.ReadLine(), out variable));
}

如果您想从文件中获取数字,请尝试以下操作:

List<int> magicals = new List<int>();
using (StreamReader reader = new StreamReader("GuessGame.txt"))
{
    int magical = 0;
    string line = "";
    while (!String.IsNullOrEmpty(line = reader.ReadLine()))
    {
        if (Int32.TryParse(line, out magical))
            magicals.Add(magical);
    }
}

并且测试是这样的:

if(magicals.Contains(guess))

答案 1 :(得分:0)

我不知道你的档案是怎样的......

图像:

12
17
9

然后你使用:

string[] fileNumbers = File.ReadAllLines("C:\MyGuessNumbers.txt");

所以在fileNumbers中你将所有数字都放到一个字符串数组中,只需循环并比较:)

TRY PARSE vs PARSE

来自您的代码:

int quantity;
Console.WriteLine("Enter the Quantity of Numbers : ");
quantity = int.Parse(Console.ReadLine());

如果Console.ReadLine()不是一个整数,你会得到一个异常,说该对象无法被解析,是写,一个“A”是一个字符串,而不是一个整数

如果您使用

int quantity = 0;
Console.WriteLine("Enter the Quantity of Numbers : ");
while(! int.TryParse(Console.ReadLine(), out quantity) )
   Console.WriteLine("That does not seam to be an integer, please only use numbers!\nEnter the Quantity of Numbers : ");

这将验证是否为数字,因为它将进行TRY解析,如果成功解析整数,它将返回布尔值TRUE或FALSE。

在这个例子中,我循环直到用户以编程语言输入数字,而TryParse返回false。

答案 2 :(得分:0)

你有一个小错误,你将整数与文件编写器sw进行比较,所以猜测总是错误的。  输入猜测后添加文件阅读器,以便完全关闭连接:

 sw.Close();      
 StreamReader sr = new StreamReader(@"C:Guess Game.txt");

然后制作compareson

作为礼物我改善你的大厅代码享受

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

 namespace Guess_Game
{
class Program
{
    static void Main(string[] args)
    {
        int quantity;
        int min, max;
        int[] number = new int[10];
        Console.WriteLine("Enter the Quantity of Numbers : ");
        quantity = int.Parse(Console.ReadLine());

        Console.WriteLine("Enter the Maximum and Minimum Number t \n");
        Console.WriteLine("MIN : ");
        min = int.Parse(Console.ReadLine());
        Console.WriteLine("MAX : ");
        max = int.Parse(Console.ReadLine());
        for (int i = 0; i < quantity; i++)
        {
            Console.WriteLine("Now, Guess the {0}Number : ", i + 1);
            number[i] = int.Parse(Console.ReadLine());
        }

        GuessGame(min, max, quantity, number);

        //if(number!= // number not equal to that number that contains in a file.. how to write it in that 
        //if condition..
    }

    static void GuessGame(int min, int max, int quantity, int[] number)
    {
        Random r = new Random();
        StreamWriter sw = new StreamWriter(@"C: Guess Game.txt");

        int[] temp = new int[quantity];
        for (int i = 0; i < quantity; i++)
        {
            temp[i] = r.Next(min, max);
            sw.WriteLine(temp[i]);

        }
        sw.Close();
        StreamReader sr = new StreamReader(@"C: Guess Game.txt");
        int trying = 3;
        int[] guess = new int[10];
        for (int i = 0; i <= quantity; i++)
            guess[i] = Convert.ToInt32(sr.ReadLine());

        for (int i = 0; i < quantity;i++ )
            for (int j = 0; j < quantity; j++)
                if (number[j] != guess[i] )
                {


         System.Console.WriteLine("you have wrong Answer,you have {0} try ", trying--);
                    for (int k = 0; k < quantity; k++)
                    {
                        Console.WriteLine("you {0} Number is", k+1);
                        number[k] = Convert.ToInt32(System.Console.ReadLine());
                    }

                    if(trying==0)

                    for (j = 0; j <quantity; j++)
           Console.WriteLine("Your Guess is right! the right answer is{0} ", guess[j]);




                }

        for (int j = 0; j < quantity; j++)
         Console.WriteLine("Your Guess is right! the right answer is{0} ", guess[j]);

  Console.ReadLine();  } 
}
}