C# : Console. Read() 未获取 "right"输入

标签 c#

我有以下代码: 实际的问题是“非引用”代码。 我想获取玩家数量(最大值 = 4),但是当我通过 Console.Read() 询问并输入 1 到 4 之间的任何 Int 时,我得到的值是:48 + Console .Read()。 他们唯一能获得“真实”输入的方法是使用Console.ReadLine(),但这并没有给我一个Integer,不,它返回一个字符串,并且实际上不知道如何在 C# 中将 String (Numbers) 转换为 Integers,因为我是新人,而且因为我只找到 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];
        }
         */
    }
}

最佳答案

您可以使用

int convertedNumber = int.parse(stringToConvert)

int convertedNumber;

int.TryParse(stringToConvert, out covertedNumber)

将字符串转换为整数。

关于C# : Console. Read() 未获取 "right"输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2438182/

相关文章:

c# - 选中时更改切换按钮的背景颜色

c# - 从 Matlab 到 C#

c# - 英语复数/单数变换函数

c# - 在最终不需要计算器的情况下将枚举定义为 2 的标志/幂的任何技巧?

c# 重写 url 参数

c# - 尝试对node.js中的字符串进行AES加密以匹配.net中的加密值

c# - 如何在 C# 中使用 T 类型进行类型转换并访问其属性和变量

c# - 这个算法实现是LRU还是MRU?

c# - webclient 无法使用文件(正在被另一个进程使用..)

c# - 枚举类型在 C# 中存储为 int 吗?