2017-08-09 92 views
3

我剛剛開始學習C#這一週,並試圖運行一個簡單的代碼,提示用戶輸入一個數字,如果他們輸入文本,或提示他們輸入一個正數,如果他們輸入一個負數(對文本是一個布爾操作,對於負數是一個if語句)。如果他們輸入一個有效(正數)的數字,程序會繼續執行其餘的步驟。儘管while循環移動到下一步沒有滿足條件

但是,通過這段代碼,如果用戶輸入一個負數,然後是一個文本,然後是另一個負數等,它似乎打破了循環,並繼續下一步操作。

該代碼是一個更大程序的一部分,所以我縮小了它的範圍,並且只提取了它運行的最關鍵部分。有人能夠發現我在這裏錯過的東西嗎?

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

namespace IncomeTaxCalculator 
{ 
    class IncomeTax 
    { 
     public static void Main() 
     { 
      double income; 
      income = income_input(); 
      show_output(income); 
     } 
     public static double income_input() 
     { 
      double income; string income_string; bool bad_value = true; 
      do 
      { 
       Console.Write("What is your total income: "); 
       income_string = Console.ReadLine(); 
       if (double.TryParse(income_string, out income)) 
       { 
        bad_value = false; 
       } 
       else 
       { 
        Console.WriteLine("Enter your income as a whole-dollar numeric figure."); 
       } 
       if (income < 0) 
       { 
        Console.WriteLine("Your income cannot be a negative"); 
       } 
      } while (bad_value || income < 0); 
      return income; 
     } 
       public static void show_output(double income) 
     { 
      Console.WriteLine("Your income is " + income); 
      Console.WriteLine("\n\n Hit Enter to exit."); 
      Console.ReadLine(); 
     } 
    } 
} 
+9

我已經可以告訴你,你在標題中所做的假設是錯誤的。也許調試它看到的值:) – EpicKip

+2

在C#中命名方法的標準是'PascalCase'('IncomeInput'而不是'income_input'),對於局部變量則是'camelCase'('incomeString'而不是'income_string') –

+2

您需要在循環的開始處將'bad_value'重新初始化爲true。 – juharr

回答

0

我意識到這已被接受,但這可以在一個更簡單的循環中完成。爲什麼不創建一個無限循環,並在滿足這些值時創建一個break/return。而不是檢查有效輸入的無效輸入搜索。

我不會詳細說明爲什麼這是一個更可接受的解決方案,請考慮給出的說明,如果您執行無效輸入,那麼您的指令是錯誤的。相反,檢查積極的結果。 Read This!!

static double income_input() 
{ 
    double income = double.NaN; 
    while (true) 
    { 
     Console.WriteLine("What is your income?:"); 
     if (double.TryParse(Console.ReadLine(), out income) && income > 0) 
      return income; 
     Console.WriteLine("Invalid input. Please enter a valid number greater than zero."); 
    } 
} 

真的所有我們在這裏所做的是建立與while(true)。所以現在循環永遠不會結束,除非我們明確告訴它。

接下來,您可以簡單地解析結果並確保double.TryParse成功的條件和income > 0。請注意,退貨只是退出循環。

現在編譯(注意最後沒有返回),因爲編譯器明白唯一的退出點是通過return語句。 Example Post

如果您想要儘可能使用最短的代碼,可以使用一些C#7語法來編寫inline variables

static double income_input() 
{ 
    while (true) 
    { 
     Console.WriteLine("What is your income?:"); 
     if (double.TryParse(Console.ReadLine(), out double income) && income > 0) 
      return income; 
     Console.WriteLine("Invalid input. Please enter a valid number greater than zero."); 
    } 
} 

快樂編碼!

0

更改您的代碼是這樣的:

double income; 
string income_string; 
do 
{ 
     Console.Write("What is your total income: "); 
     income_string = Console.ReadLine(); 
} while (!double.TryParse(income_string, out income) || income < 0); 
//rest of your code here, in another method that takes the valid income 

你應該分裂,從有(商業)邏輯在其中的一個採購收入的方法。

+1

如果輸入不可分析文本,循環將結束。 – CrudaLilium

+3

你的意思是'while(!double.TryParse(income_string,out income)|| income <0)' – juharr

+0

此外這裏不包括兩個特定的錯誤信息。 – juharr

4

這是發生了什麼事。當你輸入一個負數bad_value將被設置爲false。然後當您輸入一個非數字值income將由TryParse設置爲0。現在你的bad_value || income < 0的情況是錯誤的。要解決這個問題,你只需要在每個循環開始時重置bad_value爲true。

或者,您可以像RenéVogt建議將bad_value設置爲true,然後再檢查if是否爲負數,然後您就可以執行while(bad_value)

do 
{ 
    Console.Write("What is your total income: "); 
    income_string = Console.ReadLine(); 
    if (double.TryParse(income_string, out income)) 
    { 
     bad_value = false; 
    } 
    else 
    { 
     Console.WriteLine("Enter your income as a whole-dollar numeric figure."); 
     bad_value = true; 
    } 
    if (income < 0) 
    { 
     Console.WriteLine("Your income cannot be a negative"); 
     bad_value = true; 
    } 
} while (bad_value); 
+1

或在'else'部分中設置'bad_value'爲'true'。 –

+0

完美。謝謝 –

相關問題