[英]This code shouldnt have an error. I cant figure out why this doesnt work
My coding teacher gave me homework to build ba working code.我的编码老师给了我作业来构建 ba 工作代码。 So i did and when i ran it it said there was an error.所以我做了,当我运行它时,它说有一个错误。 I couldn't find one and neither could my teachet.我找不到,我的老师也找不到。 So my mom recommended to ask here to see if maybe we messed something.所以我妈妈建议在这里问一下,看看我们是否搞砸了什么。 Sorry if i have spelling mistakes i am still learning English.对不起,如果我有拼写错误,我还在学习英语。 Ps i am learning the language c#. Ps 我正在学习 C# 语言。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace Dcoder
{
public class Program
{
public static void Main(string[] args)
{
string sibling1;
string sibling2;
int age_sibling1;
int age_sibling2;
Console.WriteLine( " insert your name and your age " );
sibling1 = string Console.ReadLine();
age_sibling1 = int.Parse(Console.ReadLine());
Console.WriteLine( " insert your sibrlings name and age " );
sibling2 = string Console.ReadLine();
age_sibling2 = int.Parse(console.ReadLine());
if (age_sibling1 > age_sibling2);
Console.WriteLine(sibling1 + " is bigger " );
Else;
if (age_sibling2 > age_sibling1);
Console.WriteLine(sibling2 + " is bigger " );
}
}
}
Made some changes to your code.对您的代码进行了一些更改。 Some suggestions: Int.Parse will throw an exception if user doesnt enter a number.一些建议: 如果用户没有输入数字,Int.Parse 将抛出异常。 I would suggest using try/catch or Int32.TryParse .我建议使用 try/catch 或Int32.TryParse 。
Another thing is if you are using variable1, variable2, variable3 etc. its time to create a method and avoid using variableN.另一件事是,如果您使用的是 variable1、variable2、variable3 等,那么是时候创建一个方法并避免使用 variableN。
string sibling1, sibling2;
int age_sibling1, age_sibling2;
Console.WriteLine("Insert sibling 1 name ");
sibling1 = Console.ReadLine();
Console.WriteLine("Insert sibling 1 age ");
age_sibling1 = int.Parse(Console.ReadLine());
Console.WriteLine("Insert sibling 2 name ");
sibling2 = Console.ReadLine();
Console.WriteLine("Insert sibling 2 age ");
age_sibling2 = int.Parse(Console.ReadLine());
if (age_sibling1 > age_sibling2)
Console.WriteLine($"{sibling1} is bigger ");
else
Console.WriteLine($"{sibling2} is bigger ");
// Wait for user.
Console.ReadKey();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.