亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如果 (Integer Console.Readline "" or null) 不起作用

如果 (Integer Console.Readline "" or null) 不起作用

C#
狐的傳說 2021-07-28 21:05:05
在搜索和搜索(你知道它是怎么回事)之后,我無法弄清楚為什么這段代碼不起作用。我只是想讓它像這樣工作if (Nummer == "") {    Console.WriteLine("0");}就是這樣,它不起作用。我已經找了一個半小時了。不明白為什么有一個簡單的基本解釋。我只找到了如何用字符串或其他東西修復它,然后我嘗試轉換它,但它仍然不起作用。有人能幫助我嗎?感謝您對我有限的知識的耐心。謝謝你的時間static void Main(string[] args){    bool herhaal = true;    do    {                        Console.Write("Geef een getal : ");        int Nummer = Convert.ToInt16(Console.ReadLine());                   if (Console.ReadLine() == "" && Console.ReadLine() == null)        {            Console.WriteLine("0");        }        double kw = Math.Pow(Nummer, 2);        Console.WriteLine("Kwadraat van {0} is: {1}", Nummer, kw + Environment.NewLine);    } while (herhaal);}
查看完整描述

3 回答

?
弒天下

TA貢獻1818條經驗 獲得超8個贊

static void Main(string[] args)

{

    int Nummer;

    bool herhaal = true;

    do

    {                

        Console.Write("Geef een getal : ");          

        //only read from the Console ONCE per loop iteration, and always read to a string first

        string input = Console.ReadLine(); 


        //TryParse better than Convert for converting strings to integers

        if (!int.TryParse(input, out Nummer))        

        {

            Console.WriteLine("0");

        }

        else  //only do the second part if the conversion worked

        {

            double kw = Math.Pow(Nummer, 2);

            Console.WriteLine("Kwadraat van {0} is: {1}\n", Nummer, kw);

        }


    } while (herhaal);

}

要從 WinForms 應用程序執行此操作,如評論中所嘗試:


private void button1_Click(object sender, EventArgs e)

{

    double aantalgroep;

    if (!double.TryParse(textBox1.Text, out aantalgroep))        

    {

        textBox1.Text = "0";

    }

    else 

    {

        double kw = Math.Pow(aantalgroep, 2);

        textBox1.Text = String.Format("Kwadraat van {0} is: {1}", aantalgroep, kw);

    }

}


查看完整回答
反對 回復 2021-07-31
?
翻閱古今

TA貢獻1780條經驗 獲得超5個贊

根據 C# 文檔,Console.ReadLine執行以下操作

從標準輸入流中讀取下一行字符。

這意味著每次調用時Console.ReadLine,都會從控制臺讀取一個新行。從我在你的代碼中看到的,這不是你想要的行為。要解決您的問題,您應該將 的結果存儲Console.ReadLine在一個變量中并使用該變量而不是 ReadLine 方法。


查看完整回答
反對 回復 2021-07-31
  • 3 回答
  • 0 關注
  • 217 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號