Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#返回一个完全不相关且看似随机的整数?_C#_Variables - Fatal编程技术网

C#返回一个完全不相关且看似随机的整数?

C#返回一个完全不相关且看似随机的整数?,c#,variables,C#,Variables,我目前是一名完全的C#初学者,但是我有这个问题,作为python用户,这个错误对我来说没有任何意义。我试图找到一个修复,但我不知道搜索什么或如何描述错误,所以请帮助 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace testin { class Program {

我目前是一名完全的C#初学者,但是我有这个问题,作为python用户,这个错误对我来说没有任何意义。我试图找到一个修复,但我不知道搜索什么或如何描述错误,所以请帮助

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

namespace testin
{
    class Program
    {
        static void Main(string[] args)
        {
            PrintState();
            CalculateAge();
            EzMaths();
            //Here I call the procedure which is declared later on
            Console.ReadLine();
        }
        static void PrintState()
        {
            /*Here I write the procedure of 'PrintState',
             * or print statement. The program will print "hey world"
             * to the console as a result. */
            Console.WriteLine("Hey world");
        }

        static void EzMaths()
        {
            Console.WriteLine("Enter the first number ");
            Console.Write("> ");
            int num1 = Console.Read();
            Console.WriteLine("Enter the second number ");
            Console.Write("> ");
            int num2 = Console.Read();
            int total = (num1 + num2);
            Console.WriteLine(total);
        }


        static void CalculateAge()
        {
            Console.WriteLine("Enter your year of birth ");
            Console.Write("> ");
            int date = Console.Read();
            int age = (2018 - date);
            Console.Write("You are " + (age));
            Console.ReadLine();


        }


    }
}

在CalculateAge函数中,需要使用console.ReadLine()而不是console.Read()。 请参阅此答案以了解Console.Read和Console.ReadLine之间的区别:

但是我建议您也添加一个tryParse,以便在将输入的值转换为int之前验证它是否为整数

大概是这样的:

    static void CalculateAge()
    {
        Console.WriteLine("Enter your year of birth ");
        Console.Write("> ");
        string input = Console.ReadLine();
        int date = 2018;
        Int32.TryParse(input, out date);
        int age = (2018 - date);
        Console.Write("You are " + (age));
        Console.ReadLine();
    }

如果不希望其默认值为2018,则可以使用另一个实现。比如添加一个while循环,while tryParse!=false,然后继续请求输入。

您需要在CalculateAge函数中使用console.ReadLine()而不是console.Read()。 请参阅此答案以了解Console.Read和Console.ReadLine之间的区别:

但是我建议您也添加一个tryParse,以便在将输入的值转换为int之前验证它是否为整数

大概是这样的:

    static void CalculateAge()
    {
        Console.WriteLine("Enter your year of birth ");
        Console.Write("> ");
        string input = Console.ReadLine();
        int date = 2018;
        Int32.TryParse(input, out date);
        int age = (2018 - date);
        Console.Write("You are " + (age));
        Console.ReadLine();
    }
如果不希望其默认值为2018,则可以使用另一个实现。比如添加一个while循环,while tryParse!=false,然后继续请求输入。

Console.Read()
读取下一个字符,但作为其
int
表示形式。您需要使用
Console.ReadLine(),但现在您面临一个不同的问题,
Console.ReadLine()
返回一个字符串而不是
int
,因此现在需要对其进行转换。在.NET世界中,您会这样做:

string userInput = Console.ReadLine();
int userInputNumber = Convert.ToInt32(userInput);
这是不安全的,因为用户可能输入的不是数字的东西会使程序崩溃。当然,如果您刚刚开始制作“Hello World”应用程序,这可能不是您最关心的问题。
如果您感兴趣,我将发布更好的版本:

string userInput = Console.ReadLine();
int userInputNumber;
if (int.TryParse(userInput, out userInputNumber))
{
    // Do code here with userInputNumber as your number
}
在C#(像所有现代语言一样)中,我们有一些方法可以使它变得更短,但也更难阅读:

if (int.TryParse(Console.ReadLine(), out int userInputNumber))
{
    //Do code here
}
Console.Read()
读取下一个字符,但作为其
int
表示形式。您需要使用
Console.ReadLine(),但现在您面临一个不同的问题,
Console.ReadLine()
返回一个字符串而不是
int
,因此现在需要对其进行转换。在.NET世界中,您会这样做:

string userInput = Console.ReadLine();
int userInputNumber = Convert.ToInt32(userInput);
这是不安全的,因为用户可能输入的不是数字的东西会使程序崩溃。当然,如果您刚刚开始制作“Hello World”应用程序,这可能不是您最关心的问题。
如果您感兴趣,我将发布更好的版本:

string userInput = Console.ReadLine();
int userInputNumber;
if (int.TryParse(userInput, out userInputNumber))
{
    // Do code here with userInputNumber as your number
}
在C#(像所有现代语言一样)中,我们有一些方法可以使它变得更短,但也更难阅读:

if (int.TryParse(Console.ReadLine(), out int userInputNumber))
{
    //Do code here
}

Console.Read
返回整数,但不读取整数,如所述
Console.Read()
返回一个整数,但它是下一个读取字符的字符代码。例如,如果键入零,
0
,则其字符代码为48。这就是您将得到的回报。您需要使用
Console.ReadLine()
,然后使用
int.TryParseValue()等方法解析该值
要将从用户输入读取的字符串转换为数字,Console.read将返回一个字符,然后您需要在访问它之前对其进行tryparse。您需要在
Console.read()
,ex
convert.ToInt32(Console.ReadLine())上对其进行整数转换
控制台。Read
返回整数,但不读取整数,如说明所示
Console.Read()
返回一个整数,但它是下一个读取字符的字符代码。例如,如果键入零,
0
,则其字符代码为48。这就是您将得到的回报。您需要使用
Console.ReadLine()
,然后使用
int.TryParseValue()等方法解析该值
要将从用户输入读取的字符串转换为数字,Console.read将返回一个字符,然后您需要在访问它之前对其进行tryparse。您需要在
Console.read()
,ex
convert.ToInt32(Console.ReadLine())上对其进行整数转换
Convert.ToInt32
在这里是一个糟糕的选择。永远不要相信用户的输入。它可能根本不是一个数字。然后最好向OP解释一下为什么要使用它。
Convert.ToInt32
在这里是一个糟糕的选择。永远不要相信用户的输入。它可能根本不是一个数字。然后,最好向OP解释使用它的原因。值得指出的是,将变量初始化为0没有任何作用:
out
将始终设置值,即使解析失败(编译器知道这一点,并且不会给出“使用未初始化变量”错误)。值得指出的是,将变量初始化为0没有任何作用:
out
将始终设置值,即使解析失败(编译器知道这一点,并且不会给出“使用未初始化变量”错误)。