2012-10-19 23 views
0

我需要做的是3次運行一個循環,併爲三個年齡組0-1011-2021-65用下面的代碼開關循環:如何構建運行三次,測試年齡輸入

class Program 
    { 
     private static object N; 
     static void Main(string[] args) 
     { 
      string answer;         
      { 
       Console.WriteLine("please enter your name:"); 
       string name = Console.ReadLine(); 

       Console.WriteLine("please enter your surname:"); 
       string surname = Console.ReadLine(); 

       Console.WriteLine("please enter your age:"); 
       string age = Console.ReadLine();   

       Console.WriteLine("please enter your adress:"); 
       string adress = Console.ReadLine(); 

       Console.WriteLine("hallo,{0} {1},veel sucess met C#", name, surname); 

       Console.WriteLine("zijn deze gegevens juist? J/N"); 
       answer = Console.ReadLine(); 
      } 
      while(answer == "N"); 
     } 
    }  

我試過一些東西來測試年齡,但我總是有錯誤。另外,我不知道如何正確編寫循環代碼。

有人可以請指出我對這兩個問題的正確方向嗎?

對不起,我的英語很差我是荷蘭人。

注意:OP在其中一個答案中提到,這不是一項家庭作業。

+1

嘗試一些新手c#程序員的書 –

+1

看起來像家庭作業。如果你的問題是關於家庭作業,你可以問問,但要確保你不要求別人爲你寫代碼。 – Abel

+2

你的代碼不會編譯!你忘了做關鍵字。如果你想這樣做循環exacly 3次使用FOR循環 – Jacek

回答

1

開關語句對特定值進行操作,而不是範圍,因此您希望使用系列if語句代替。

使用計數器(在循環外部聲明並在每次執行時遞增)來確定循環運行了多少次(或使用for循環)。

編輯

class Program 
{ 
    private static object N; 

    static void Main(string[] args) 
    { 
     string answer; 
     int runCount= 0; 

     do 
     { 
      ++runCount; 
      Console.WriteLine("please enter your name:"); 
      string name = Console.ReadLine(); 

      Console.WriteLine("please enter your surname:"); 
      string surname = Console.ReadLine(); 

      Console.WriteLine("please enter your age:"); 
      int age = int.Parse(Console.ReadLine()); 

      if(age >= 0 && age <= 10) 
      { 
       Console.WriteLine("Child"); 
      } 
      else if(age <= 20) 
      { 
       Console.WriteLine("Young adult"); 
      } 
      else if(age <= 65) 
      { 
       Console.WriteLine("Adult"); 
      } 

      Console.WriteLine("please enter your adress:"); 
      string adress = Console.ReadLine(); 

      Console.WriteLine("hallo,{0} {1},veel sucess met C#", name, surname); 

      Console.WriteLine("zijn deze gegevens juist? J/N"); 
      answer = Console.ReadLine(); 
     } 
     while(runCount < 3 && answer == "N"); 
    } 
} 
+0

尼斯充分的工作示例。但在這樣的情況下,我們寧願不做家庭作業的工作,而是試着指出正確的方向(我知道,道德警察;))。無論如何,+1。 (PS:'字符串時間'應該可以是'int age')。 – Abel

+0

@Abel如果你喜歡,我可以把它取出來。我有同樣的保留,因此最初沒有發佈。 – mlorbetske

+0

現在的問題是,我已經把你已經做的裝罐,但視覺工作室會給錯誤運算符'<='不能應用於類型'字符串'和'int'的操作數\t我不知道它到底是什麼意味着可以這麼說道德警察礦業人民我爲自己做我的這不是一個家庭作業 – Sojab0on

1

你可能想要的東西,像下面這樣:

var ageGroups = new [] { "0-10", "11-20", "21-65"}; 

foreach(var ageGroup in ageGroups) 
{ 
    // do your thing. This will loop three times, once for each age group 
    // example of switch (but: code-smell, you probably want class hierarchy later) 
    switch(ageGroup) 
    { 
     case "0-10": 
     // do somethihg 
     break; 
    } 
} 

或者,如MrFox說,當你沒有預先定義的年齡組(但我不看你的問題)然後:

for(var i = 0; i < 3; i++) 
{ 
    // do your thing 
    // example of if-statement for range (cannot do with switch): 
    int convertedAge; 
    if(int.TryParse(age, out convertedAge)) 
    { 
     if(convertedAge >= 0 && convertedAge <= 10) 
     { 
      // do your thing 
     } 
    } 
} 
+0

年齡將是某人輸入的值,例如,如果他們輸入5,則不匹配「0-10」。 – MrFox

+0

@MFFOX:你可能是對的。我又增加了一個例子。 – Abel

0

你的接口是mult我的語言對用戶的一些文本是用英文寫的,其他的用荷蘭文寫的,決定你的想法或者讓用戶選擇他的語言。

至於編程技巧,你應該谷歌一般的C#構造。此course是非常好的。作爲一名程序員,最好學習英語,不管你來自哪個國家。

class Program 
{ 
    static int age; 
    static void Main(string[] args) 
    { 
     string answer; 
     do 
     { 
      Console.WriteLine("please enter your name:"); 
      string name = Console.ReadLine(); 

      Console.WriteLine("please enter your surname:"); 
      string surname = Console.ReadLine(); 

      Console.WriteLine("please enter your age:"); 
      age = Convert.ToInt32(Console.ReadLine()); 

      Console.WriteLine("please enter your adress:"); 
      string adress = Console.ReadLine(); 

      Console.WriteLine("hallo,{0} {1},veel sucess met C#", name, surname); 

      Console.WriteLine("zijn deze gegevens juist? J/N"); 
      answer = Console.ReadLine(); 
     } 
     while (answer == "N"); 

     for (int i = 0; i < 3; i++) 
     { 
      if (age >= 0 && age <= 10) 
      { 
       // First age group. 
      } 
      if (age >= 11 && age <= 20) 
      { 
       // Second age group. 
      } 
      if (age >= 21 && age <= 65) 
      { 
       // Third age group. 
      } 
     } 
    } 
} 

編輯:謝謝現在有一個辦法使while循環正確。我不清楚for循環應該在哪裏。

+0

請注意,此代碼仍然存在錯誤的while循環,它會永久運行或立即中斷。另外,當你所做的只是測試一個已經設置的變量時,爲什麼你需要一個for循環? for循環是多餘的;)。我認爲for循環應該繞過整個代碼塊。 – Abel

+0

是的,我在旁邊的一個新項目中接受了它來檢查差異。 我改變了我的意願,但它停止與運營商在年齡的情況下 – Sojab0on