有关C#的更多帮助 [英] More help with C#

查看:80
本文介绍了有关C#的更多帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我只是在不得已时才来这里(因为我花了无数的时间试图在Google中找到答案),但是我有一个很大的想法来编写程序以增进我对C#的理解.我陷入以下情况:用户将为a,b或c输入2个值.用户可以输入a和b,b和c或a和c.现在,如果用户输入a却不知道b(他会把和x放在这里,这就是我遇到的问题),那么程序将使用x并继续询问c值,然后通过简单的数学控制台会吐出b的值.

总结一下我的问题,我对C#如何获取x的输入,继续询问其他值然后找到x是什么感到困惑.

非常抱歉,我可能在这个问题上浪费了一些时间,但我才刚开始学习这种语言.

谢谢
-G

Alright guys I only come here as a last resort (as in I spend countless hours trying to find the answer in Google) when i have a big idea to make a program to further my understanding of C#. I am stuck in the following scenario: The user will input 2 values for a, b or c. The user can input a and b, b and c, or a and c. Now if the user inputs a and does not know b (here he would put and x, this is where I am having trouble) so the program would take the x and move on to ask for the c value, then with simple math the console would spit out the value for b.

To sum up my problem I am confused as to how C# takes the input of x , moves on to ask for the other value and then would find what x is.

Very sorry that I may be wasting some time with this question but I just started learning this language.

Thanks
-G

推荐答案

或者这样:(假设您正在做a + b = c)
实际上,将其更改为a * b = c或其他任何内容都非常容易:)
Or this : (asuming that you are doing a + b = c )
actually it''s pretty easy to change to a*b =c or whatever :)
class Program
{
    static void Main(string[] args)
    {

       string[]  vars = new string[3];
       vars[0] = Program.getRetVal("A = ");
       vars[1] = Program.getRetVal("B = ");
       vars[2] = Program.getRetVal("C = ");
       int index = 0;
        while (vars[index] != "x")
            index++;

        if (index == 2)
        {
            Console.WriteLine("Answer:" +
                              Convert.ToString(Convert.ToInt32(vars[0]) +
                                               Convert.ToInt32(vars[1])));
        }
        else
        {
              Console.WriteLine("Answer:" +
              Convert.ToString(Convert.ToInt32(vars[2]) -
                               Convert.ToInt32(vars[Convert.ToInt32(!Convert.ToBoolean(index))])));
        }
        Console.ReadKey();
    }
   static private string getRetVal(string info)
    {
        Console.Write(info);
        return  Console.ReadLine().ToLower();
    }
}



对于新手来说,唯一棘手的部分是我将整数转换为倒数的布尔值:cool:



the only tricky part for a newcomer here is my casting of an integer to an inverted boolean :cool:


嗯,可以有多种方法.听起来您将有一个基于a,b和c的方程式.一旦拥有两个值,您将显示第三个变量值.

现在,很少的事情,如果用户知道方程式,则可以显示并要求特定的值.让我们假设,用户没有,他将只输入任意两个变量值.在这种情况下,将"var"用作所有a,b和c的数据类型.当您获得任意两个变量作为适当的整数/浮点值时(您可以使用objects数据类型找到该变量),请使用这些变量进行计算,然后找到缺失的值.这样,您可以灵活地为用户输入x作为整数值,而在内部可以忽略它.
Well, there can be various ways. What it sounds like you will have an equation based on a,b & c. Once you have any two value, you will show the third variable value.

Now, few things, if the user knows the equation, you can show and ask for specific values. Lets assume, user doesnt and he will just input any two variable values. Such case, use ''var'' as the datatype for all a,b & c. When you get any two variables as proper integer/floating value (you can find that using objects datatype), use those for your calculation and then find the value for missing one. This way, you give flexibility to user to enter x for an integer value and internally you ignore it.


您可以对其进行设计,以便对于每个输入请求,用户都可以输入一个数字或留空或输入X(如果是控制台,只需按Enter键即可).对于每一行,您可以使用:
You can design it, so that for each of the input requests the user can either enter a number or leave blank or enter X (if console, just hit enter). For each line you can use:
Int32.TryParse() 


(或您使用的任何相关类型)输入值上,以查看其是否解析为数字.如果是这样,请接受;否则,请转移到下一个(如果尚未正确解析2).


(or whatever relevant type you use) on your input values to see if it parses to a number. If so, take it, if not, move to the next if 2 haven''t been properly parsed yet.


这篇关于有关C#的更多帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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