gpt4 book ai didi

Visual Studio 中的 C# 脚本突然中途停止而没有错误

转载 作者:行者123 更新时间:2023-12-01 23:51:03 24 4
gpt4 key购买 nike

我正在尝试制作一个基本的计算器来练习我的 C# 技能。我昨天开始学习语言。我试过运行调试器,但什么也没有出现。该脚本总是在同一位置关闭,如下所示。这是我的脚本:

static void Main(string[] args)
{
string cType; //Type of calculus to be selected
int num01; //First input number
int num02; //Second input number
int result; //Function of num01 and num02

Console.WriteLine("Please select the type of calculus you wish to perform." +
"These are:\nM - Multiplication\nD - Division\nS - Subtraction\nA - Addition");
//Informing the user of calculus type they can select

cType = Console.ReadLine();
//Setting the string cType as what the user inputs

if (cType == "M")
//Detecting if the input is M to start multiplication script
{
Console.WriteLine("You have selected multiplication");
Console.Write("Please enter the first number: ");
num01 = Console.Read();
//Assign first number to string num01

// THE SCRIPT STOPS WORKING HERE

Console.Write("Please enter the second number: ");
num02 = Console.Read();
//Assign second number to string num02

result = num01 * num02;
//Calculate result

Console.Write("{0} * {1} = {2}", num01, num02, result);
//Output result as full sentence
}
}

最佳答案

您应该改用 .ReadLine(),然后尝试将其转换为整数。

if(!int.TryParse(Console.ReadLine(), out int num01)) {
Console.WriteLine("Input was not an integer. Please try again");
//retry logic
}

//num01 is now the integer value of the input

您可以删除顶部的 num01 赋值,因为不需要事先定义它。

关于Visual Studio 中的 C# 脚本突然中途停止而没有错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63499347/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com