每次用户输入新号码时写入号码

目前我有以下方法:


using System;


public class Program

{

    public static void Main()

    {

        Console.WriteLine("Please enter magical number");

        string input = "";

        int number = int.Parse(Console.ReadLine());


        if(number > 0){

            for (int i = 1; i <= number; i++)

            {

                Console.Write("{0}, ", i);

            }


            Console.WriteLine("Pick another number");

            input = Console.ReadLine();

        } else {

            Console.WriteLine("Wow that number is too low for me!");

        }

    }

}

在第一次运行时,一切都按预期工作,但是当用户输入新编号时,方法停止。我该如何防止这种情况?我做错了什么?


慕妹3242003
浏览 85回答 2
2回答

繁花如伊

在第一次运行时,一切都按预期工作,但是当用户输入新编号时,方法停止。我该如何防止这种情况?通过使用循环而不是 .您还应该 使用 来处理无效输入:ifint.TryParsedo{&nbsp; &nbsp; Console.WriteLine("Please enter magical number or enter exit to stop");&nbsp; &nbsp; string enteredNumber = Console.ReadLine()?.Trim();&nbsp; &nbsp; if ("exit".Equals(enteredNumber, StringComparison.InvariantCultureIgnoreCase))&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp;&nbsp; &nbsp; }&nbsp; &nbsp; bool valid = int.TryParse(enteredNumber, out int number);&nbsp; &nbsp; while (!valid)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Please enter a valid integer");&nbsp; &nbsp; &nbsp; &nbsp; valid = int.TryParse(Console.ReadLine()?.Trim(), out number);&nbsp; &nbsp; }&nbsp; &nbsp; if (number > 0)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(string.Join(", ", Enumerable.Range(1, number)));&nbsp; &nbsp; }&nbsp; &nbsp; else&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Wow that number is too low for me!");&nbsp; &nbsp; }} while (true);我还通过使用修复了末尾最后一个逗号的问题。string.Join

慕哥6287543

不要把所有东西都放到一个单一的,提取方法:Mainprivate static int? ReadInteger(string title) {&nbsp; if (string.IsNullOrWhiteSpace(title))&nbsp; &nbsp; Console.WriteLine("Please, input integer value or \"quit\"");&nbsp; else&nbsp; &nbsp; Console.WriteLine($"{title}. Print \"quit\" to exit.");&nbsp; while (true) {&nbsp; &nbsp; string value = Console.ReadLine().Trim();&nbsp; &nbsp; if (string.Equals(value, "quit", StringComparison.OrdinalIgnoreCase))&nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; else if (int.TryParse(value, out int result))&nbsp; &nbsp; &nbsp; return result;&nbsp; &nbsp; Console.WriteLine("Sorry, the format is incorrect. Please, try again.");&nbsp; }}private static void Report(int number) {&nbsp; Console.WriteLine(number <= 0&nbsp;&nbsp; &nbsp; ? "Wow that number is too low for me!"&nbsp; &nbsp; : String.Join(", ", Enumerable.Range(1, number)));}现在,让我们实现一个简单的方法:Mainpublic static void Main() {&nbsp; // Just a simple "for" loop:&nbsp; //&nbsp; - start with "Please enter magical number"&nbsp; //&nbsp; - keep on until user puts "quit"&nbsp; //&nbsp; - ask "Pick another number" for another numbers&nbsp;&nbsp;&nbsp; for (int? value = ReadInteger("Please enter magical number");&nbsp; &nbsp; &nbsp; &nbsp;value.HasValue;&nbsp; &nbsp; &nbsp; &nbsp;value = ReadInteger("Pick another number") {&nbsp; &nbsp; Report(value.Value);&nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP