c#code(Noob)中的问题
我的程序在给出结果之前关闭,年龄差异是错误的 我检查了所有地方,他们说使用c#code(Noob)中的问题,c#,.net,C#,.net,我的程序在给出结果之前关闭,年龄差异是错误的 我检查了所有地方,他们说使用Console.Read(),Console.ReadLine(),或者Console.ReadKey(),我在他们说之前就这么做了,但仍然不起作用。其他人说使用系统(“暂停”),但这只是给了我一个错误 它还说当我进入21岁时,21-12=38 using System; using System.Collections.Generic; using System.Linq; using System.Text; usin
Console.Read()
,Console.ReadLine()
,或者Console.ReadKey()
,我在他们说之前就这么做了,但仍然不起作用。其他人说使用系统(“暂停”)
,但这只是给了我一个错误
它还说当我进入21岁时,21-12=38
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static String name;
static int age;
static void Main(string[] args)
{
Console.WriteLine("Welcome to the application!");
Console.Write("What's your name? ");
// Setting the name string to the line read from the console.
name = Console.ReadLine();
Console.Write("\n So how old are you " + name + "? ");
// Doing the same thing we did last time with the age.
age = Console.Read();
compareAges();
Console.Read();
}
static void compareAges()
{
if(age < 12)
{
Console.WriteLine("I'm older than you! I'm 12.");
Console.ReadLine();
}
else if(age == 12)
{
Console.WriteLine("We're the same age!!!");
Console.ReadLine();
}
else
{
int ageDifference = age - 12;
Console.WriteLine("You're " + ageDifference + " years older than me!");
Console.ReadLine();
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
命名空间控制台应用程序1
{
班级计划
{
静态字符串名;
静态智力;
静态void Main(字符串[]参数)
{
WriteLine(“欢迎使用该应用程序!”);
控制台。写下(“你叫什么名字?”);
//将名称字符串设置为从控制台读取的行。
name=Console.ReadLine();
控制台。写(“\n那么你多大了“+name+”?”);
//和上次我们在年龄上做的一样。
age=Console.Read();
比较();
Console.Read();
}
静态空比较()
{
if(年龄<12岁)
{
我比你大,我12岁;
Console.ReadLine();
}
否则,如果(年龄==12)
{
WriteLine(“我们同龄!!!”;
Console.ReadLine();
}
其他的
{
年龄差异=年龄-12岁;
WriteLine(“你比我大“+年龄差+”岁!”);
Console.ReadLine();
}
}
}
}
请注意,如果我在这里出现了一些缩进错误,那么很抱歉,但实际代码的缩进是正确的。您的问题在这里:
age = Console.Read();
这里您只需将年龄设置为输入字符的ASCII码,因为Read
从控制台读取一个符号并返回其ASCII码
应该是这样
age = Convert.ToInt32(Console.ReadLine());
问题在于
age = Console.Read();
好的,Console.Read()
只读取一个符号-字符2
,在你的例子中是50
int,你有
'2' - 12 == 38 // '2' == 50
补救措施:阅读您案例中的整个字符串,即“21”
String line = Console.ReadLine();
然后将其解析为整数:
// Parse() is the simplest; TryParse() is a better:
// what if a user entered "bla-bla-bla"?
age = int.Parse(line);
这是我的看法。也许不是最好的,但它很有效:
using System;
using System.IO;
namespace ConsoleApplication1{
class Program{
public static int Main(){
Program.determineAgeDifference dta = new Program.determineAgeDifference();
string[] input = new string[2];
Console.Write("So, what's your name?\n\n>> ");
input[0] = Console.ReadLine();
Console.Clear();
Console.Write("How old are you?\n\n>> ");
input[1] = Console.ReadLine();
Console.WriteLine(dta(int.Parse(input[1].TrimStart(' ').TrimEnd(' ')));
Console.ReadLine();
return 0;
}
private string determineAgeDifference(int age){
string returnValue = "";
if (age < 12){
returnValue = "I'm older than you. I'm 12!";
}
else if (age == 12){
returnValue = "We are the same age!!!";
}
else{
returnValue = ("You're " + (age - 12).ToString() + "Years older than me!");
}
return returnValue;
}
}
}
使用系统;
使用System.IO;
命名空间控制台应用程序1{
班级计划{
公共静态int Main(){
Program.determineAgeDifference dta=新程序.determineAgeDifference();
字符串[]输入=新字符串[2];
控制台。写(“那么,你叫什么名字?\n\n>>”;
输入[0]=Console.ReadLine();
Console.Clear();
控制台。写入(“您多大了?\n\n>>”;
输入[1]=Console.ReadLine();
Console.WriteLine(dta(int.Parse(输入[1])。TrimStart(“”)。TrimEnd(“”));
Console.ReadLine();
返回0;
}
私有字符串确定差异(整数年龄){
字符串returnValue=“”;
if(年龄<12岁){
returnValue=“我比你大,我12岁!”;
}
否则,如果(年龄==12){
returnValue=“我们年龄相仿!!!”;
}
否则{
returnValue=(“你是”+(年龄-12.ToString()+“比我大几岁!”);
}
返回值;
}
}
}
调试器说了什么?使用age=Convert.ToInt32(Console.ReadLine())
作为控制台。read()给了你角色,代码调试器什么也没说太笨拙了。Tushar Gupta、Andy Korneyev和Dmitry Bychenko回答了这个问题。你本可以给出更好的问题标题。天哪,人们对它恨之入骨-6!无论如何,谢谢!:)@约瑟夫科金斯非常有趣。你确定你就是我建议的那个字符串吗?注意-应该是ReadLine()
,而不是Read()
。