简体   繁体   English

Console.Write错误

[英]Console.Write Error

I'm Kind of a beginner so i first apologize for what may seem to be a noobish question. 我是一个初学者,所以我首先为可能是一个讨厌的问题道歉。 Im trying to test a function by writing the result to the console, but i keep running into errors 我试图通过将结果写入控制台来测试功能,但我一直遇到错误

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GameRPS
{
    class Program
    {
        static void Main(string[] args)
        {
        }

        static int Equation(int Damage, int Temp)
        {
            if(Temp > 0)
            {
                int a, b, c;
                a = Damage / 2;
                b = a + Temp;
                c = b - 50;
                return c;

            }
            else
            {
                int d, e, f;
                d = Damage / 2;
                e = d - Temp;
                f = e - 50;
                return f;
            }

            int g;
            g = Equation(75, 25);
            console.WriteLine(g);
        }
    }
}

It gives me an error saying that "unreachable code" was detected, any ideas on this? 这给我一个错误,说检测到“无法访问的代码”,对此有什么想法吗? Thanks. 谢谢。

Your last three lines should be part of Main method 最后三行应该是Main方法的一部分

So your code should be: 因此,您的代码应为:

class Program
{
    static void Main(string[] args)
    {
        int g;
        g = Equation(75, 25);
        Console.WriteLine(g);
    }

    static int Equation(int Damage, int Temp)
    {
        if (Temp > 0)
        {
            int a, b, c;
            a = Damage / 2;
            b = a + Temp;
            c = b - 50;
            return c;

        }
        else
        {
            int d, e, f;
            d = Damage / 2;
            e = d - Temp;
            f = e - 50;
            return f;
        }

    }
}

You are getting the unreachable code error because you are returning from your method Equation in if and else part. 您将收到无法到达的代码错误,因为您将从ifelse部分的Equation返回。 The control would never reach those three lines. 控件永远不会到达这三行。 (also console should be Console ) console也应该是Console

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM