早上好/下午,
我希望通过引用和价值来澄清。如果我将变量值设置为" 0"正在发布的程序有效。但在用户输入后不会覆盖。所以我认为这个问题已经过去了,但是我的教授说在这个项目中一切都应该是静态的。
简短:如何在不使用void的情况下传递输入变量?
我很新。这只是第3章,所以任何帮助都会很棒。
namespace Lesson3
{
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;
}
}
}
答案 0 :(得分:1)
您没有看到任何更改的原因是因为您在获取用户输入之前调用了您的方法,因此userInput
始终等于0,您应该在<<1}后移动它们/ p>
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.ToInt32
更改为Convert.ToDouble
您在删除CS0165 error
时收到= 0
,因为您尝试在方法中使用未分配的值,现在在将值分配给userInput
之后移动了方法不应该再出现这个错误。
答案 1 :(得分:0)
您在分配用户输入之前使用变量userInput来调用您的方法。先接受输入,然后调用方法调用。
此外,您将userInput声明为double,但将实际用户输入解析为int。更改它以将其解析为double。
使用System;
命名空间Lesson3 {
internal 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;
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
Console.WriteLine(userInput + " inches converts into " + ansFeet + " feet.");
Console.WriteLine(userInput + " inches converts into " + ansYards + " yards.");
Console.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;
}
}
}