开发者

C# : Console.Read() does not get the "right" input

开发者 https://www.devze.com 2022-12-22 16:28 出处:网络
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: 4

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(). 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.

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_gam开发者_开发知识库e = 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. int.Parse will throw an exception if it tries to convert a string that is not numeric.

 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. If it was successful, the converted value will be passed through the out parameter.


To convert your string to an integer, use 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号