错误CS1513(快速问题) [英] Error CS1513 (quick question)

查看:139
本文介绍了错误CS1513(快速问题)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我对c#比较新,并尝试运行真正的简单代码,但是当我尝试运行它时,即使我在第29行显示错误消息}预期看不到任何地方放置一个花括号。我能以任何方式解决它和/或使我的整体代码更好吗?谢谢



So I'm relatively new to c# and tried to run a really simple code, but when I try to run it, it Shows an error message "} expected" at line 29 even though I don't see anywhere to put a curly bracket. any way I could resolve it and/or make my overall code better? thanks

using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string Name;
            string Family;
            string Button;
            int Answer;
            Answer = 54;


            Console.WriteLine("what is your name?");

            Name = Console.ReadLine();

            Console.WriteLine("what is your family name?");

            Family = Console.ReadLine();

            Console.WriteLine("whats 9+45?");
            {
                if (Answer == 54);
                { Console.WriteLine("{0} {1} is Smart! :D ");
                } <===== //it asks me to put a '}' after the'}'
          
             else 
               { Console.WriteLine("{0} {1} isn't so Smart :( ");
                }
                Console.WriteLine("Press Enter to Close");

                Button = Console.ReadLine();
            }

            



        }
    }
}





我尝试过的事情:



我试过把它放在那里,但要求但是它发出了一堆错误



What I have tried:

I tried putting it there like it asks but it puts out a bunch of errors

推荐答案

if (Answer == 54)

导致错误。

请删除它,你可以编译。

is causing the error.
Please remove it and you can compile.


添加到TheRealSteveJudge所说的内容:你还没有正确使用括号。

To add to what TheRealSteveJudge said: you are not using brackets properly anyway.
Console.WriteLine("whats 9+45?");
{
    if (Answer == 54);
    { Console.WriteLine("{0} {1} is Smart! :D ");
    } <===== //it asks me to put a '}' after the'}'

 else
   { Console.WriteLine("{0} {1} isn't so Smart :( ");
    }
    Console.WriteLine("Press Enter to Close");

    Button = Console.ReadLine();
}

这里不需要外部设置括号 - 它们不用于任何目的。您只在将要执行的语句分组时使用它们 - 例如,循环或条件语句。 随机添加它们不会使代码更具可读性!



作为初学者,请将它们用于每个条件,并缩进代码:

You don't need the outer set brackets here - they don't serve any purpose. You only use them when you are "grouping together" statements to be executed - in a method for example, a loop, or a conditional statement. Adding them "at random" doesn;t make your code more readable!

As a beginner, use them for every conditional, and indent your code:

static void Main(string[] args)
    {
    Console.WriteLine("what is your name?");
    string Name = Console.ReadLine();
    Console.WriteLine("what is your family name?");
    string Family = Console.ReadLine();
    int Answer = 54;

    Console.WriteLine("whats 9+45?");
    if (Answer == 54)
        {
        Console.WriteLine("{0} {1} is Smart! :D ");
        }
    else
        {
        Console.WriteLine("{0} {1} isn't so Smart :( ");
        }
    Console.WriteLine("Press Enter to Close");

    string Button = Console.ReadLine();
    }

或者像这样:

Or like this:

static void Main(string[] args)
{
    Console.WriteLine("what is your name?");
    string Name = Console.ReadLine();
    Console.WriteLine("what is your family name?");
    string Family = Console.ReadLine();
    int Answer = 54;

    Console.WriteLine("whats 9+45?");
    if (Answer == 54)
    {
        Console.WriteLine("{0} {1} is Smart! :D ");
    }
    else
    {
        Console.WriteLine("{0} {1} isn't so Smart :( ");
    }
    Console.WriteLine("Press Enter to Close");

    string Button = Console.ReadLine();
}

请注意,我将变量的定义移动到了您要使用它们的位置 - 只是让您更容易看到实际使用它们时发生了什么。使用这样的小方法,这并没有太大的区别,但随着它们的增长,通过代码页面来寻找定义变得很痛苦。它还会阻止您在超出范围时使用它们以及导致的错误消息。



此外,局部变量的名称应以小写字母开头:

Note that I moved the definition of variables to where you are going to use them - that just makes it easier to see what is going on when you actually use them. With small methods like this, that doesn't make a lot of difference, but as they grow it becomes a pain to wade back through pages of code to find the definition. It also prevents you using them when they are "out of scope" and the error messages that causes.

In addition, names of local variables should start with lower case:

        static void Main(string[] args)
            {
            Console.WriteLine("what is your name?");
            string name = Console.ReadLine();
            Console.WriteLine("what is your family name?");
            string family = Console.ReadLine();
            int answer = 54;
...



你的下一个问题是你的WriteLine声明:


And your next problem is your WriteLine statements:

    Console.WriteLine("{0} {1} is Smart! :D ");
    }
else
    {
    Console.WriteLine("{0} {1} isn't so Smart :( ");
    }

你告诉writeline包含变量值,但你不提供变量!要么这样做:

You tell the writeline to include variable values, but you don't supply the variables! Either do this:

    Console.WriteLine("{0} {1} is Smart! :D ", name, family);
    }
else
    {
    Console.WriteLine("{0} {1} isn't so Smart :( ", name, family);
    }

或者这个

Or this

    Console.WriteLine(


{name} {family}是聪明的!:D);
}
其他
{
Console.WriteLine(
"{name} {family} is Smart! :D "); } else { Console.WriteLine(


这篇关于错误CS1513(快速问题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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