如何在用户输入上做出不同的结果 [英] How to make different outcomes on user input

查看:95
本文介绍了如何在用户输入上做出不同的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我几天前开始使用c#并且有这个问题





你可以看到为了说是或否我已经要求用户说1或2说是或否但是当用户说是或否而不是1或2时我怎么能做出不同的决定



我尝试了什么:



int answer;

Console.WriteLine(所以,你同意吗? ?;;

Console.WriteLine(是(按1)或否(按2));

answer = int.Parse(Console.ReadLine() );





if(answer == 1)

{

控制台。 WriteLine(好);



}

其他

{

Console.WriteLine(Dissapointing);

}

解决方案

尝试:

 string reply =; 
while(回复!=是&& reply!=no)
{
Console.Write(所以,你同意吗?);
reply = Console.ReadLine()。ToLower();
}


是的,我想出来了

i二手

< pre lang =c#>



Console.WriteLine(你同意吗?);

string choice = Console .ReadLine();



开关(选择)

{

caseyes:

Console.WriteLine(好工作);

休息;



默认:

Console.WriteLine(Dissapointing);

休息;

}

通过这个,如果我说是,那家伙回复了好工作但是如果我说任何其他的他都回答失望

Thx寻求帮助虽然


I started c # few days ago and had this question


as u can see in order to say yes or no i have asked the user to say 1 or 2 to say yes or no but how can i make the different decisons occur when the user says yes or no rather than 1 or 2

What I have tried:

int answer;
Console.WriteLine("So, Do you agree?");
Console.WriteLine("Yes(Press 1) or No (Press 2)");
answer = int.Parse(Console.ReadLine());


if (answer == 1)
{
Console.WriteLine("Good");

}
else
{
Console.WriteLine("Dissapointing");
}

解决方案

Try:

string reply = "";
while (reply != "yes" && reply != "no")
    {
    Console.Write("So, do you agree? ");
    reply = Console.ReadLine().ToLower();
    }


Yeah i figured it out
i used


Console.WriteLine("Do You Agree?");
string choice = Console.ReadLine();

switch (choice)
{
case "yes":
Console.WriteLine("Good Job");
break;

default:
Console.WriteLine("Dissapointing");
break;
}
Through this if i said yes the guy replied good job but if i said anyhting else he replied dissapointing
Thx for help though


这篇关于如何在用户输入上做出不同的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆