简体   繁体   English

C#:Console.Read()无法获得“正确”的输入

[英]C# : Console.Read() does not get the “right” input

I have the following code: The actual problem is the "non-quoted" code. 我有以下代码:实际的问题是“未引用”代码。 I want to get the player amount (max = 4), but when I ask via Console.Read() and I enter any Int from 1 to 4 I get as value: 48 + Console.Read() . 我想获取玩家数量(最大值= 4),但是当我通过Console.Read()询问并输入1到4之间的任何整数时,我得到的值是: 48 + Console.Read() They only thing how I can get the "real" input is using Console.ReadLine() , but this does not give me an Integer , no it returns a string, and actually do not know how to convert String ( Numbers ) to Integers in C#, because I am new, and because I only found ToString() and not ToNumber. 他们唯一的问题是如何使用Console.ReadLine()获得“真实”输入,但这并没有给我一个Integer ,它没有返回一个字符串,并且实际上不知道如何在其中将StringNumbers )转换为Integers C#,因为我是新手,并且因为我只找到ToString()而不是ToNumber。

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

namespace eve_calc_tool
{
    class Program
    {

        int players;
        int units;
        int active_units;
        int inactive_units;
        int finished_units;
        int lastDiceNumber = 0;
        bool game_state;

        public static void Main(string[] args)
        {
            int count_game = 0;

            //Console.Title = "Mensch ärger dich nicht";
            //Console.WriteLine("\tNeues Spiel wird");
            //Console.WriteLine("\t...geladen");
            //System.Threading.Thread.Sleep(5000);

            //Console.Clear();
            //Console.WriteLine("Neues Spiel wird gestartet, bitte haben sie etwas Geduld");
            //Console.Title = "Spiel " + count_game.ToString();

            //Console.Clear();
            //string prevText = "Anzahl der Spieler: ";

            //Console.WriteLine(prevText);

            string read = Console.ReadLine();

            /*Program game = new Program();
            game.players = read;

            game.setPlayers(game.players);

            if (game.players > 0 && 5 > game.players)
            {
                game.firstRound();
            }*/

            string readagain = read;




            Console.ReadLine();
        }
        /*
        bool setPlayers(int amount)
        {
            players = amount;

            if (players > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        bool createGame()
        {

            inactive_units = units = getPlayers() * 4;
            active_units = 0;
            finished_units = 0;

            game_state = true;

            if (game_state == true)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        int getPlayers()
        {
            return players;
        }


        private static readonly Random random = new Random();
        private static readonly object syncLock = new object();
        public static int RandomNumber(int min, int max)
        {
            lock (syncLock)
            { // synchronize
                return random.Next(min, max);
            }
        }


        int rollDice()
        {
            lastDiceNumber = RandomNumber(1,6);
            return lastDiceNumber;
        }

        int firstRound()
        {
            int[] results = new int[getPlayers()];

            for (int i = 0; i < getPlayers(); i++)
            {
                results[i] = rollDice();
            }
            Array.Sort(results);

            return results[3];
        }
         */
    }
}

You can use 您可以使用

int convertedNumber = int.parse(stringToConvert)

or 要么

int convertedNumber;

int.TryParse(stringToConvert, out covertedNumber)

to convert strings to integers. 将字符串转换为整数。

You should really use TryParse instead so that you can catch if the user doesn't input a number. 您应该真正使用TryParse来代替,以便在用户未输入数字的情况下进行捕获。 int.Parse will throw an exception if it tries to convert a string that is not numeric. 如果int.Parse尝试转换非数字字符串,则将引发异常。

 int convertedNumber = 0;

 if (!int.TryParse(stringToConvert, out convertedNumber))
 {
     // this code will execute if the user did not put
     //     in an actual number.  For example, if the user entered "a".
 }

The TryParse method returns a boolean value which will tell you whether the conversion was successful. TryParse方法返回一个布尔值,该值将告诉您转换是否成功。 If it was successful, the converted value will be passed through the out parameter. 如果成功,则转换后的值将通过out参数传递。

To convert your string to an integer, use int.Parse(yourString) . 要将字符串转换为整数,请使用int.Parse(yourString)

The reason you get "48 + Console.ReadKey" is that Console.ReadKey returns the code of the key that was pressed - in this case, the ANSI value of the number character that was pressed. 之所以会收到“ 48 + Console.ReadKey”,是因为Console.ReadKey返回了所按键的代码 -在这种情况下,是所按数字字符的ANSI值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM