我试图使用(ChooseDecoration)方法调用一个方法(DisplayMessage),它将返回值为1,2或其他值,但现在该方法每次只返回0。
我尝试过各种方法,比如在DisplayMessage方法中获取装饰值,但是程序规范需要一个单独的方法来找到这些方法。
namespace ConsoleApp6
{
public class ChristmasCard : ChristmasCardTesting
{
static void Main()
{
ToAndFrom();
double decorate = ChooseDecoration();
DisplayMessage(decorate);
Console.ReadLine();
}
public static void ToAndFrom()
{
Console.WriteLine("Please enter who you are sending the card to");
string to = Console.ReadLine();
Console.WriteLine("Please enter who it is from");
string from = Console.ReadLine();
}
public static void SetBorderCharacter()
{
}
static double ChooseDecoration()
{
Console.WriteLine("Please Choose decoration 1 or 2 ");
double decoration = Convert.ToDouble(Console.Read());
return decoration;
}
public static void DisplayMessage(double decoration)
{
if (decoration == 1)
{
ChristmasCardTesting.SantaA();
}
else if (decoration == 2)
{
ChristmasCardTesting.SantaB();
}
// else
// ChristmasCardTesting.SantaA();
}
public static void DoPromt()
{
}
public static void AddMessage()
{
}
public static void ClearMessage()
{
}
}
public class ChristmasCardTesting
{
public static void SantaA()
{
Console.WriteLine(@" ||::|:|| .--------, ");
Console.WriteLine(@" |:||:|:| |_______ / .-. ");
Console.WriteLine(@" ||::|:|| .'` ___ `'. \|('v')|/ ");
Console.WriteLine(@" \\\/\///: .'` `'. ;____`( )'____ ");
Console.WriteLine(@" \====/ './ o o \|~ ^' '^ // ");
Console.WriteLine(@" \\// | ())) . | Season's \ ");
Console.WriteLine(@" || \ `.__.' /| // ");
Console.WriteLine(@" || _{``-.___.-'\| Greetings \ ");
Console.WriteLine(@" || _.' `-.____.- '`| ___ // ");
Console.WriteLine(@" ||` __ \ |___/ \_______\ ");
Console.WriteLine(@" .' || (__) \ \| / ");
Console.WriteLine(@" / `\/ __ vvvvv'\___/ ");
Console.WriteLine(@" | | (__) | ");
Console.WriteLine(@" \___/\ / ");
Console.WriteLine(@" || | .___. | ");
Console.WriteLine(@" || | | | ");
Console.WriteLine(@" ||.-' | '-. ");
Console.WriteLine(@" || | ) ");
Console.WriteLine(@" || ----------'---------' ");
Console.ReadLine();
}
public static void SantaB()
{
Console.WriteLine(@" ");
Console.WriteLine(@" .------, ");
Console.WriteLine(@" .\/. |______| ");
Console.WriteLine(@" _\_}{_/_ _|_Ll___|_ ");
Console.WriteLine(@" / }{ \ [__________] .\/. ");
Console.WriteLine(@" '/\' / \ _\_\/_/_ ");
Console.WriteLine(@" () o o () / /\ \ ");
Console.WriteLine(@" \ ~~~ . / '/\' ");
Console.WriteLine(@" _\/ \ '...' / \/_ ");
Console.WriteLine(@" \\ {`------'} // ");
Console.WriteLine(@" \\ /`---/',`\\ // ");
Console.WriteLine(@" \/' o | |\ \`// ");
Console.WriteLine(@" /' | | \/ /\ ");
Console.WriteLine(@" __,. -- ~~ ~| o `\| |~ ~~ -- . __ ");
Console.WriteLine(@" | | ");
Console.WriteLine(@" \ o / ");
Console.WriteLine(@" `._ _.' ");
Console.WriteLine(@" ^~- . - ~^ ");
Console.WriteLine(@" ");
Console.ReadLine();
}
}
}发布于 2017-12-15 00:14:54
在使用Console.Read方法时,您应该谨慎使用,因为它只从标准输入流读取下一个键,但是阻塞直到用户按下Enter。第一个字符之后的任何其他字符随后都会通过对Console.Read的调用返回。
请看下面的示例:
Console.WriteLine("Enter 1 or 2: ");
int i = Console.Read(); // Here the user types: 12[Enter]
// The value of 'i' is now 49 (the ASCII value of 1)
// Later we do this:
Console.WriteLine("Enter 3 or 4:");
int j = Console.Read();
// The value of 'j' is now 50 (the ASCII value of 2), and the code keeps running!
// There is no pause to wait for user input, since there was already some in the cache您应该做什么而不是
从用户获得输入有两种更常见的方法:Console.ReadLine(),它返回用户输入的整个字符串(在他们按下‘Enter’之后);或者Console.ReadKey(),它返回他们按下的键的ConsoleKeyInfo表示。
您可以完全按照代码的方式使用ReadLine,这可能是最好的方法:
double decoration = Convert.ToDouble(Console.ReadLine());但是,如果您不希望它们必须按'Enter',您可以执行以下操作,通过在ToString()对象的KeyChar属性上调用ConsoleKeyInfo属性获得用户键入的下一个字符的字符串表示形式:
double decoration = Convert.ToDouble(Console.ReadKey().KeyChar.ToString());添加了一些验证
但是,如果用户输入一个不能转换为double的字符串,会发生什么情况?在这种情况下,Convert.ToDouble将抛出一个异常。为了避免这种情况,我们可以使用double.TryParse,如果转换成功,则返回true,并将out参数设置为转换后的值。现在我们可以这样做,以检查输入是否有效:
double decoration;
if (!double.TryParse(Console.ReadLine(), out decoration))
{
Console.WriteLine("The value you entered is not a valid double.");
}但我们现在得让他们再输入一次,对吧?如果它再次失败..。好吧,我们需要一个循环:
double decoration;
while (!double.TryParse(Console.ReadLine(), out decoration))
{
Console.WriteLine("The value you entered is not a valid double.");
Console.Write("Please Choose decoration 1 or 2: ");
}现在,它将继续循环,直到用户输入有效的双倍。
使代码可重用
好的,很好,现在我们有一种方法来验证输入,但是我们真的想在每次需要从用户那里获得双倍时编写循环代码吗?更好的方法可能是为我们创建一个方法,该方法接受一个字符串提示符以显示给用户,并返回正确的类型。我们甚至可以添加一些“界”参数,以强制数字介于最小值和最大值之间:
/// <summary>
/// Gets a strongly typed (double) value from the Console
/// </summary>
/// <param name="prompt">The initial message to display to the user</param>
/// <returns>User input converted to a double</returns>
public static double GetDoubleFromUser(string prompt = "Enter a number: ",
double minValue = double.MinValue, double maxValue = double.MaxValue)
{
double value;
// Write the prompt text and get input from user
Console.Write(prompt);
while (!double.TryParse(Console.ReadLine(), out value)
|| value < minValue || value > maxValue)
{
// If input can't be converted to a double, keep trying
Console.WriteLine("ERROR: Invalid value!");
Console.Write(prompt);
}
// Return converted input value
return value;
}现在,有了这些代码,我们所需要做的就是:
double decoration = GetDoubleFromUser("Please Choose decoration 1 or 2: ", 1, 2);发布于 2017-12-14 23:50:32
Console.Read返回一个int,但这不是数字键--它是char值,或者是EOF的-1。所以:问题不在于它返回零。问题是它正在返回密钥的ASCII值。你需要皈依。例如:
int i = Console.Read();
if(i < 0) return 0;
char c = (char)i;
return (int)(c - '0');https://stackoverflow.com/questions/47823808
复制相似问题