我对编码很新,但是我在课堂上。我在这里需要帮助visual studio C# [英] I am very new to coding, but im in a class. I need help here visual studio C#

查看:69
本文介绍了我对编码很新,但是我在课堂上。我在这里需要帮助visual studio C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我已经制作了一个类似于刚开始的人的计算器,但我试图添加一些东西,所以它不仅仅是一个计算器。



我制作了计算器并且工作正常

但是在数字成倍增加之后我想得到一个符合正确数字的答案。



我想要的是将42(生命的意思,哈哈)作为这个计算器中的正确答案



所以当我fx乘以6 x 7它等于42我希望它去恭喜你,你已经找到了生命的意义

但是当答案大于42时,我应该稍微低一点,当然还要低于42高一点







我的代码:



我尝试过:



使用系统;



命名空间测试

{

class program

{

private static int num_2;



static void Main(string [] args)

{

{

int num_1;

int num_2;



Console.Write(写下你的第一个数字);

num_1 =转换.ToInt32(Console.ReadLine());

Console.Write(写下你的第二个数字);

num_2 = Convert.ToInt32(Console.ReadLine()) ;

Console.WriteLine(Resultat:+ num_1 * num_2);

Console.ReadLine();





int x = 42;



if(x == 42)

{

Console.WriteLine(正确,你已经找到生命的意义);

}



else if(x> = 42)

{

Console.WriteLine(尝试稍微高一点);
{
$ b $ Console.WriteLine(尝试低一点);

}



Console.ReadLine();



}



}

}

}

Basically I have made a calculator like pretty much like anyone who is just starting out, but im trying to add something to it so its not just a calculator.

I made the calculator and it works fine
But after the numbers have been multiplied i would like an answer that goes with the right number.

What I want is to have 42 (the meaning of life, haha) to be the "correct" answer in this calculator

So when I fx multiply 6 x 7 and it equals 42 i want it to go "Congrats, you've found the meaning of life
But when the answer is greater than 42 i should go "a little lower" and of course less than 42 "a little higher"



my code:

What I have tried:

using System;

namespace Test
{
class Program
{
private static int num_2;

static void Main(string[] args)
{
{
int num_1;
int num_2;

Console.Write("write your first number ");
num_1 = Convert.ToInt32(Console.ReadLine());
Console.Write("Write your second number ");
num_2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Resultat: " + num_1 * num_2);
Console.ReadLine();


int x = 42;

if (x == 42)
{
Console.WriteLine("Correct, you've found the meaning of life");
}

else if (x >= 42)
{
Console.WriteLine("Try a little higher");
}

else if (x <= 42)
{
Console.WriteLine("Try a little lower");
}

Console.ReadLine();

}

}
}
}

推荐答案

你硬编码x为42,x需要是计算的结果



更改



You're hard-coding "x" to be 42, x needs to be the result of the calculation

Change

Console.WriteLine("Resultat: " + num_1 * num_2);
Console.ReadLine();


int x = 42;





to





to

int x = num_1 * num_2;
Console.WriteLine("Resultat: " + x.ToString());
Console.ReadLine();


好的,首先要注意的是当你执行
OK, the first thing to note is that when you execute
Console.ReadLine();

控制台应用程序将停止,直到用户按下ENTER。因此,在 WriteLine if 代码之间,在用户按下之前,您将看不到额外 ENTER - 没有提示他这样做!

The console app will "stop" until the user presses ENTER. So with that between your WriteLine and your if code, you won't see the "extras" until the user presses ENTER - which he isn't prompted to do!

Console.WriteLine("Resultat: " + num_1 * num_2);
Console.ReadLine();  <<<<<==== REMOVE THIS LINE


int x = 42;

if (x == 42)

其次,您的代码专门将x设置为42:

Secondly, your code specifically sets x to 42:

int x = 42;

因此它将始终针对相同的值进行测试!相反,将X设置为计算结果。

第三,如果在主 if 测试中测试相等性,则它无法匹配任何相等在 else 部分中 - 所以不要在 else 条件,使用<和>相反。



试试这个:

So it will always test against the same value! Set X to the result of the calculation instead.
Thirdly, if you test for equality in the main if test, then it cannot match any equality in the else parts - so don't test for "<=" or ">>=" in the else conditions, use "<" and ">" instead.

Try this:

int x = num_1 * num_2;
Console.WriteLine("Resultat: {0}", x );
if (x == 42)
    {
    Console.WriteLine("Correct, you've found the answer to life, the universe, and everything.");
    }
else if (x < 42)
    {
    Console.WriteLine("Try a little higher.");
    }
else
    {
    Console.WriteLine("Try a little lower.");
    }
Console.ReadLine();


这篇关于我对编码很新,但是我在课堂上。我在这里需要帮助visual studio C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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