【问题标题】:C# While loop not outputing anything unexpectedlyC#While循环没有意外输出任何东西
【发布时间】:2018-05-03 13:26:52
【问题描述】:

在我运行过程中遇到了更多问题。这次我在论坛上环顾了几分钟以寻找答案,但没有成功找到任何东西(我的意思是其中大部分都包含更复杂的 while 循环方法,所以我不知道会发生什么。 ) 这是c#代码:

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

namespace CodingExcercise
{
class Program
{
    static void Main(string[] args)
    {
        int answer;
        int number;
        Console.WriteLine("Type in the number you desire the multiplication table for!");
        number = Convert.ToInt32(Console.ReadLine());
        int count = 1;
        while (count != 10) ;
       answer = number * count;
        Console.WriteLine("{0}*{1}={2}", number, count, answer);
        count = count + 1;
    }
}
}

仅此而已(我主要只是通过基本练习来进行这些操作)我正在尝试发布一个最多为 10 的数字。

另外,我在这里得到更快的回复,然后查看所有已关闭的线程,直到找到正确的线程,抱歉,如果已经有这样的线程。 谢谢!

【问题讨论】:

  • 您的while (count != 10) ; 是一个无限循环。它将永远执行;。您需要将要重复的代码放在{ } 之间。如果没有括号,则只考虑下一条语句,; 为空语句。
  • while (count != 10) ;
  • 我叫恶作剧
  • 此外,如果您希望您的代码只运行 10 次,那么这就是 for 循环的典型情况。哦,正确缩进你的代码,这样每个块的开始和结束位置就很清楚了。最后,最好使用count++ 来增加一个变量。
  • 是的,我现在注意到了这两个问题,我忘记了分号不用于某些事情。我也完全忘记了 { } 非常感谢:D

标签: c# visual-studio


【解决方案1】:

试试这个:

 static void Main(string[] args)
    {
        int answer;
        int number;
        Console.WriteLine("Type in the number you desire the multiplication table for!");
        number = Convert.ToInt32(Console.ReadLine());
        int count = 1;
        while (count != 10)
        {
            answer = number * count;
            Console.WriteLine("{0}*{1}={2}", number, count, answer);
            count = count + 1;
        }
        Console.ReadLine();
    }

【讨论】:

    【解决方案2】:

    您似乎错过了输入“;”在while循环之后。用这个替换你的while循环:

    while (count != 10) 
    {
       answer = number * count;
       Console.WriteLine("{0}*{1}={2}", number, count, answer);
       count = count + 1;
    }
    

    【讨论】:

      猜你喜欢
      • 2018-03-02
      • 2021-01-16
      • 2023-03-07
      • 2016-04-02
      • 2017-01-28
      • 1970-01-01
      • 1970-01-01
      • 2021-05-10
      • 2020-09-07
      相关资源
      最近更新 更多