本文介绍了如何在方程式中使用用户输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!

我是一名初学者软件开发人员,并且正在为一些简单"的软件而苦苦挣扎.我的练习是要求用户输入从1到1200的距离,然后将答案减去1200,然后我会得到答案.例如,如果我键入 在500中,那么答案将是700,减去的数字将为我提供另一个答案,即彼得的旅程比迈克斯更长或更短的次数.这是我的代码的一部分,我不知道该怎么做.解决.

I'm a beginner software developer and i'm struggling with some "simple" codeing.My exercise was that i asked user to type the distance from 1 to 1200.Then the answer would be substracted from 1200 and i would get the answer.For example if i typed in 500,then the answer would be 700 and the number that was substracted would give me another answer of how many times the journey of Peter is longer or shorter than Mikes.Here's a part of my code,the part that i dont know how to solve.

Console.Write(键入彼得斯旅程的长度(1-1200)");

Console.Write("Type the length of Peters journey (1-1200)");

num01 = Convert.ToInt32(Console.ReadLine());  < ---我不知道我为什么写它,以及它是否有帮助.

int num01 =我不知道如何使num01等于用户输入,如果我不知道自己将无法完成代码.

num01 = Convert.ToInt32(Console.ReadLine());  <--- I dont know why i wrote that and if it helps or not.

int num01 = I dont know how to make num01 equal to user input and if i dont know that i wont be able to finish the code.

int num02 = 1200;

int num02 = 1200;

int num03 = num02-num01; (例如,如果用户回答500,则700 = 1200-500,因此Peters旅程为500,而Mikes旅程为700.那么我需要程序说出Peters旅程比Mikes短或长多少倍,具体取决于答案 用户).

int num03 = num02 - num01; (For example if the user answers 500 then 700 = 1200 - 500 and so Peters journey is 500 and Mikes journey is 700.Then i need the program to say how many times is Peters journy shorter or longer than Mikes , depending on the answer of the user).

PS:对不起,英语不好.     

推荐答案

如果您输入700,则Console.ReadLine将返回字符串"700".

if you entered 700 then Console.ReadLine will return the string "700".

您应该使用int.TryParse将其转换为整数.  因为它将处理用户输入诸如"asdfasdf"之类的愚蠢内容的情况,通过返回false.

you should use int.TryParse to convert it to an integer.  Because it will handle the case where the user enters something silly like "asdfasdf" by returning false.

string userInputText = Console.ReadLine();
int num01;
if( int.TryParse( userInputText, out num01 ) {
    // num01 now has a numeric value
    // corresponding to the text that the user entered.
} else {
    // User entered something that's not a number.
    // Display an error message.
    // and probably exit early so we don't continue.
}



这篇关于如何在方程式中使用用户输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 22:43