ad

C#如何将未知值变量(用户输入)传递给返回方法-英雄云拓展知识分享

匿名投稿 339 2024-01-21

早上好/下午,

我很乐意经过参考和价值进行一些澄清。如果我将变量值设置为“ 0”,但在用户输入以后不会覆盖,则发布的程序可行之有效的。因此,这种情况下,我相信这个问题是经过的,但是我的教授说,该计划的一切都应当是静态的。

简称:如何在不使用void的情形下传递输入变量?

我很新。这只是第3章,因此任何帮助都会很棒。

namespace Lesson3

C#如何将未知值变量(用户输入)传递给返回方法-英雄云拓展知识分享

{

class Program

{

public static void Main(string[] args)

{

// declarations

// 12 inches to a foot

// 36 inches to a yard

// 63360 inches per 1 mile

double userInput= 0; // if "0" is removed the program will not function CS0165 error

double feet = 12;

double yards = 36;

double miles = 63360;

double ansFeet = userDivFeet(ref feet, ref userInput);

double ansYards = userDivYards(ref yards, ref userInput);

double ansMiles = userDivMiles(ref miles, ref userInput);

Console.WriteLine("Please enter the number of inches to convert: ");

userInput = Convert.ToInt32(Console.ReadLine());

// output each calculation and display with console to hold the results

WriteLine(userInput + " inches converts into " + ansFeet + " feet.");

WriteLine(userInput + " inches converts into " + ansYards + " yards.");

WriteLine(userInput + " inches converts into " + ansMiles + " miles.");

Console.ReadKey();

}

public static double userDivFeet(ref double userInput, ref double feet)

{

return userInput / feet;

}

public static double userDivYards(ref double userInput, ref double yards)

{

return userInput / yards;

}

public static double userDivMiles(ref double userInput, ref double miles)

{

return userInput / miles;

}

}

}

看答案

您看不到任何更改的缘由是由于您在获得用户输入之前调用方法 userInput 将始终等于0,您应当在

userInput = Convert.ToInt32(Console.ReadLine());

所以看起来像这样

public static void Main(string[] args)

{

// declarations

// 12 inches to a foot

// 36 inches to a yard

// 63360 inches per 1 mile

double userInput = 0; // if "0" is removed the program will not function CS0165 error

double feet = 12;

double yards = 36;

double miles = 63360;

Console.WriteLine("Please enter the number of inches to convert: ");

userInput = Convert.ToInt32(Console.ReadLine());

double ansFeet = userDivFeet(ref feet, ref userInput);

double ansYards = userDivYards(ref yards, ref userInput);

double ansMiles = userDivMiles(ref miles, ref userInput);

// output each calculation and display with console to hold the results

WriteLine(userInput + " inches converts into " + ansFeet + " feet.");

WriteLine(userInput + " inches converts into " + ansYards + " yards.");

WriteLine(userInput + " inches converts into " + ansMiles + " miles.");

Console.ReadKey();

}

正如Polyfun所说,您不需要使用Ref,您的老师说这一定是静态的缘由是,您直接从主要的角度调用这些方法。

如果您希望用户能够以1.25之类的小数输入数字,则应更改 Convert.ToInt32Convert.ToDouble

你得到了 CS0165 error 删除 = 0 由于您试图在方法中使用未分配的值 userInput 您不应当再收到此毛病了。


🚀🌟 点击注册 免费试用超级应用平台-英雄云企业级hpapaas 🌟🚀 😃👉🌐

免责声明:

本网址(www.yingxiongyun.com)发布的材料主要源于独立创作和网友匿名投稿。此处提供的所有信息仅供参考之用。我们致力于提供准确且可信的信息,但不对材料的完整性或真实性作出任何保证。用户应自行验证相关信息的正确性,并对其决策承担全部责任。对于由于信息的错误、不准确或遗漏所造成的任何损失,本网址不承担任何法律责任。本网站所展示的所有内容,如文字、图像、标志、音频、视频、软件和程序等的版权均属于原创作者。如果任何组织或个人认为网站内容可能侵犯其知识产权,或包含不准确之处,请即刻联系我们进行相应处理。

标签:企业应用开发 定制化解决方案 统计图网状图
上一篇:为何属性有一种方法?-英雄云拓展知识分享
下一篇:疏忽基于位图的视频播放器-英雄云拓展知识分享
相关文章

 发表评论

暂时没有评论,来抢沙发吧~

×