如果..其他问题,请帮忙 [英] If .. Else problem, please help

查看:77
本文介绍了如果..其他问题,请帮忙的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

static void Main(string[] args)
       { string continue1 = "yes";



           while (continue1 == "yes")

           {
               Console.WriteLine("your name?:");
               string jmeno = Console.ReadLine();
               Console.WriteLine(jmeno + " is the best!");

               Console.WriteLine("New name? (yes/no)");
               if (continue1 == "yes")

                   continue1 = Console.ReadLine();


               // the following doesn´t work

              else Console.WriteLine("What a shame :(");

           }
           Console.ReadKey();
       }





我试过的:



我想我没有正确理解其他是如何工作的......否则它会起作用:D请帮忙,我想要学习它的工作方式:(

我希望程序在你写不(或其他任何东西 - 这就是我觉得其他有效)时写下多么可耻,但没有成功程序停止。



What I have tried:

I think I don´t properly understand how does "else" work.. otherwise it would have worked :D Please help, I want to learn the way this works :(
I want the program to write "what a shame" when u Write "no" (or anything else - thats how I feel "else" works), but no success, program just stops.

推荐答案

你应该学会尽快使用调试器。而不是猜测你的代码在做什么,是时候看看你的代码了执行并确保它符合您的预期。



调试器允许您逐行执行,检查变量,您将看到有一个点它停止做你的事期待。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

当代码没有按预期执行时,您接近一个错误。



尝试在 之前阅读答案
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.

try to read the answer before the if.


你应该像这样格式化你的@



You should format your if\else like this

if (continue1 == "yes")
{
    continue1 = Console.ReadLine();
}
else
{
    Console.WriteLine("What a shame :(");
}





避免歧义。



It avoids ambiguity.


检查我在代码中发表的评论



check the comments i made in the Code

static void Main(string[] args)
       { string continue1 = "yes";
 

 
           while (continue1 == "yes")
 
           {
               Console.WriteLine("your name?:");
               string jmeno = Console.ReadLine();
               Console.WriteLine(jmeno + " is the best!");
 
               Console.WriteLine("New name? (yes/no)");
               continue1 = Console.ReadLine(); //read the user input before checking it with the if statment
               if (continue1 == "yes")
               {
                   //do something hwne the input is yes or in this case repeat the loop
               }
              else Console.WriteLine("What a shame :(");
 
           }
           Console.ReadKey();
       }


这篇关于如果..其他问题,请帮忙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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