C#-最后一个while循环和';变量x';做 使用系统; 命名空间控制台应用程序1 { 班级计划 { 静态void Main() { 风险值分录=0; 试一试{ Console.Write(“输入打印\“耶!\”:)的次数”; var entryParsed=int.Parse(Console.ReadLine()); if(entryParsed

C#-最后一个while循环和';变量x';做 使用系统; 命名空间控制台应用程序1 { 班级计划 { 静态void Main() { 风险值分录=0; 试一试{ Console.Write(“输入打印\“耶!\”:)的次数”; var entryParsed=int.Parse(Console.ReadLine()); if(entryParsed,c#,loops,while-loop,var,C#,Loops,While Loop,Var,在最后几行代码中,我不理解“var x”和while循环的作用或表示。这个代码示例来自一个树屋挑战,但是“var x”如何使程序按预期工作呢?谢谢你的帮助!:) var只是一种语法快捷方式,可以避免显式定义x的类型-编译器可以确定x的类型,因为它隐含在所分配的值中 对于您的循环,可以简化为: using System; namespace ConsoleApplication1 { class Program { static void Main

在最后几行代码中,我不理解“var x”和while循环的作用或表示。这个代码示例来自一个树屋挑战,但是“var x”如何使程序按预期工作呢?谢谢你的帮助!:)

var
只是一种语法快捷方式,可以避免显式定义
x
的类型-编译器可以确定
x
的类型,因为它隐含在所分配的值中

对于您的循环,可以简化为:

using System;

namespace ConsoleApplication1
{
    class Program
    {        
        static void Main()
        {            
            var entry = 0;
            try {
                Console.Write("Enter the number of times to print \"Yay!\": ");
                var entryParsed = int.Parse(Console.ReadLine());

                if (entryParsed < 0) 
                {
                    Console.Write("You must enter a positive number.");
                }  
                else 
                {
                    entry += entryParsed;
                }
            }
            catch (FormatException)
            {
                Console.Write("You must enter a whole number.");
            }

            var x = 0;
            while (true) 
            {
                if (x < entry) 
                {
                    Console.Write("Yay!");
                    x++;
                }
                else
                {
                    break;
                }
            }
        }
    }
}
var x=0;
while(x++

通常,您会避免显式的
while(true)
循环,因为随着代码变得越来越复杂,它会增加一种风险,即循环中的退出条件永远不会满足,并且最终会出现一个无休止的循环-更好的做法是让退出表达式清晰可见,而不是将其隐藏在循环中的某个地方。

执行相同操作的惯用方法是

var x = 0;

while (x++ < entry) {
    Console.Write("Yay!");
}
for(int x=0;x
C#中的关键字表示“推断类型”

你发布的代码非常。。。使人联想到初学者的。最后一个区块是一个循环。无可争辩。客观上,任何替代方案都是低劣的

这里有另一种写作方法。诚然,搞笑过度,但你明白了:

var x = 0; //Means the same thing as latter 
int x = 0; //The compiler actually CONVERTS the former to this in an early pass when it strips away syntactic sugar
使用系统;
使用System.Linq;
使用System.Collections.Generic;
名称空间示例
{
公共课程
{
公共静态void Main(字符串[]args)
{
int entry=CollectUserInput(“输入打印\“耶!\”:“,
(int x)=>x>0,“请输入一个正数:”;

for(int i=0;iit将循环打印'yay'输入时间。for循环将被清除使用调试器单步执行代码,您将确切看到它的作用。x是一个计数器,在每次打印'yay'后递增。x=entry后,while循环中的逻辑将强制其退出(中断)。如果您发现while循环逻辑有点复杂,可以使用简单的for循环代替。
var x
是一个隐式分配的类型计数器变量,它会自动将x分配给type
int
。while循环可以用这个for循环代替,以(x-1)次打印文本:
for(var x=0;x
。哦,是的,因为所有这些疯狂的代码帮助我作为初学者理解xD
var x = 0; //Means the same thing as latter 
int x = 0; //The compiler actually CONVERTS the former to this in an early pass when it strips away syntactic sugar
using System;
using System.Linq;
using System.Collections.Generic;

namespace Example
{
    public class Program
    {
        public static void Main(string[] args)
        {
            int entry = CollectUserInput<int>("Enter the number of times to print \"Yay!\": ",
                (int x) => x > 0, "Please enter a positive number: ");

            for (int i=0; i<entry; i++) {
                Console.Write("Yay!");
            }
        }

        /// <summary>
        /// Prompts user for console input; reprompts untils correct type recieved. Returns input as specified type.
        /// </summary>
        /// <param name="message">Display message to prompt user for input.</param>
        private static T CollectUserInput<T>(string message = null)
        {
            if (message != null)
            {
                Console.WriteLine(message);
            }
            while (true)
            {
                string rawInput = Console.ReadLine();
                try
                {
                    return (T)Convert.ChangeType(rawInput, typeof(T));
                }
                catch
                {
                    Console.WriteLine("Please input a response of type: " + typeof(T).ToString());
                }
            }
        }

        /// <summary>
        /// Prompts user for console input; reprompts untils correct type recieved. Returns input as specified type.
        /// </summary>
        /// <param name="message">Display message to prompt user for input.</param>
        /// <param name="validate">Prompt user to reenter input until it passes this validation function.</param>
        /// <param name="validationFailureMessage">Message displayed to user after each validation failure.</param>
        private static T CollectUserInput<T>(string message, Func<T, bool> validate, string validationFailureMessage = null)
        {
            var input = CollectUserInput<T>(message);
            bool isValid = validate(input);
            while (!isValid)
            {
                Console.WriteLine(validationFailureMessage);
                input = CollectUserInput<T>();
                isValid = validate(input);
            }
            return input;
        }
    }
}