Csharp:从一个循环中提取变量到另一个循环 [英] Csharp : extracting variable from one loop to another

查看:111
本文介绍了Csharp:从一个循环中提取变量到另一个循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到设置计算器的问题。变量(方法)是一个char,它在第一个while循环中输入。

在第二个while循环中我希望第一个循环中的变量(方法)被转移到第二个循环中。

但是有一个错误并且它表示变量是不受约束的。

请帮忙。这是代码:



Im having an issue with setting up a calculator. The variable(method) is a char and its being input on the first while loop.
on the second while loop i want the variable(method) from the first loop to be transfered into the second loop.
but there is an error and it says that the variable is unassigined.
please help. here is the code:

<pre lang="c#">
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Test
{
class Program
{
    static void Main(string[] args)
    {
        int continu = 0;
        int continu2 = 0;
        Console.WriteLine("Welcome To The Alpha Calculator!");
        Console.WriteLine("First,Type A Method");
        char method;
        while (continu2 != 0)
        {
            Console.Write("Type '+' or '-' or '*' or '/' :");
            method = char.Parse(Console.ReadLine());
            if (method == '+' || method == '/' || method == '*' || method == '-')
            {
                continu = 1;
                continu2 = 1;
                Console.WriteLine(method + " Picked");

            }
            else
            {
                Console.WriteLine("Unknown Method Try Again");


            }
            char method2 = method;
        }


        while (continu != 0)
        {

            Console.Write("Pick A First Digit:");
            double num1 = double.Parse(Console.ReadLine());
            Console.Write("Pick A Second Digit:");
            double num2 = double.Parse(Console.ReadLine());
            double sum = 0;
          if (method == '+')
            {
                sum = num1 + num2;
            }
            else if (method == '-')
            {
                sum = num1 - num2;
            }
            else if (method == '/')
            {
                sum = num1 / num2;
            }
            else if (method == '*')
            {
                sum = num1 * num2;
            }
            Console.WriteLine("The Solution is: " + sum);
            continu = 0;

        }
    }
}

}





我尝试了什么:



我尝试将循环变量(连续)的值更改为1但是没有用。



What I have tried:

I tried changing the values of the loops variables(continu) to 1 but didnt work.

推荐答案

因为你只在第一个while循环中声明方法char变量并为其赋值。如果由于任何原因第一次循环没有执行你在第二次变量中没有任何值 - 所以导致错误。



在第一次之前移动下面的行循环执行并将解决您的问题。



Its because you declare method char variable and assigned value to it in first while loop only. If for any reason first while loop does not execute you don't have any value in variable in 2nd while - so resulting in error.

Move below lines before first while loop executes and will solve your issue.

char method;

Console.Write("Type '+' or '-' or '*' or '/' :");
method = char.Parse(Console.ReadLine());
--- first while and so on below ---


查看你的代码(我会删除一些内容以使其更明显):

Look at your code (I'll chop out stuff to make it more obvious):
        int continu = 0;
        int continu2 = 0;
        char method;
        while (continu2 != 0)
        {
...
        }


        while (continu != 0)
        {
...
        }

因为 continu2 在到达时始终为零第一个循环,循环中的代码永远不会执行。

因为 continu 设置为零,所以第二个循环也会发生同样的事情。 br />


结果你的方法完全没有...

Because continu2 is always zero when it arrives at the first loop, the code inside the loop is never executed.
Because continu is set to zero, the same thing happens to the second loop.

And your method does absolutely nothing as a result ...


我注意到你的第一个while()循环是针对continu2的! = 0。

但是,当选择有效值时,将其设置为1,这样循环就会继续。

您需要将continu2设置为0或者,更好的是,在if块中休息一下。



char method2 = method; //但是method2不在程序其余部分的范围内。



它实际上是在告诉你'方法'是未分配的吗?工作变量有点模糊。



更多:

现在,当你使用char.Parse()方法时,你将会当内容不是int时会出错。你只想要角色。因此,除了上面的循环修复,只需从键盘获取char并测试其值。
I noticed your first while() loop is for continu2 !=0.
However, when a valid value is picked, you set it to 1, so the loop goes on.
You need to either set continu2 to 0 or, better still, put a break in the if block.

char method2 = method; // but method2 is not in scope of the rest of program.

Is it actually telling you that 'method' is unassigned? The work variable is a bit vague.

More:
Now, when you char.Parse() the method, you are going to get an error when the content is not an int. You simply want the character. So, along with the loop fixes, above, just get the char from the keyboard and test its value.


这篇关于Csharp:从一个循环中提取变量到另一个循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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