2013-10-21 42 views
-1

我在玩C#,我做了這個。C# - 如何跳到第一行?

{ 
     string wichOp; 
     Console.WriteLine("Kaj je toni?"); 
     Console.WriteLine("Izber med: -A -B -C -D -E"); 
     wichOp = Console.ReadLine(); 

     wichOp = wichOp.ToLower(); 
     if (wichOp == "a") 
     { 
      Console.Write("Toni je BK"); 
     } 
     else if (wichOp == "b") 
     { 
      Console.Write("Toni je PEDER"); 
     } 
     else if (wichOp == "c") 
     { 
      Console.Write("Toniju Baloni"); 
     } 
     else if (wichOp == "d") 
     { 
      Console.Write("Toni je buzi"); 
     } 
     else if (wichOp == "e") 
     { 
      Console.Write("TONI ŠAMPION"); 
     } 
     else 
      Console.WriteLine("Nisi vnesil pravilno izbiro"); 

    } 
} 

}

我想要做的是與一個按鍵(R),我可以跳回到我的選擇(A,B,C,d,E)。然後輸入另一個選項,如果我按任何其他鍵將退出該程序。

+0

你比較字符串錯誤,你應該考慮輸入的情況下。 – asawyer

回答

1
static void Main(string[] args) 
    { 
     string wichOp; 
     bool running = true; 

     while (running) 
     { 

      Console.WriteLine("Kaj je toni?"); 
      Console.WriteLine("Izber med: -A -B -C -D -E"); 
      wichOp = Console.ReadLine(); 

      wichOp = wichOp.ToLower(); 
      if (wichOp == "a") 
      { 
       Console.Write("Toni je BK"); 
      } 
      else if (wichOp == "b") 
      { 
       Console.Write("Toni je PEDER"); 
      } 
      else if (wichOp == "c") 
      { 
       Console.Write("Toniju Baloni"); 
      } 
      else if (wichOp == "d") 
      { 
       Console.Write("Toni je buzi"); 
      } 
      else if (wichOp == "e") 
      { 
       Console.Write("TONI ŠAMPION"); 
      } 
      else 
       Console.WriteLine("Nisi vnesil pravilno izbiro"); 

      Console.WriteLine("\n\nPress r to repeat, other input will close the Program"); 
      string input = Console.ReadLine(); 
      if (input != "r") 
       running = false; 
     } 
    } 
0

可能是這樣的:

var wichOp = Console.ReadLine(); 
while(wichOp.Equals("R")) //WHILE "R" 
{ 
     var wichOp = Console.ReadLine(); 
     if (wichOp == "a") 
     { 
      Console.Write("Toni je BK"); 
     } 
     else if (wichOp == "b") 
     { 
      Console.Write("Toni je PEDER"); 
     } 

     ..... 

} 
1
string wichOp = "r"; 
while (wichOp == "r") 
{ 
    Console.WriteLine("Kaj je toni?"); 
    Console.WriteLine("Izber med: -A -B -C -D -E"); 
    wichOp = Console.ReadLine(); 
    wichOp = wichOp.ToLower(); 
    if (wichOp == "a") 
    { 
     Console.Write("Toni je BK"); 
    } 
    else if (wichOp == "b") 
    { 
     Console.Write("Toni je PEDER"); 
    } 
    else if (wichOp == "c") 
    { 
     Console.Write("Toniju Baloni"); 
    } 
    else if (wichOp == "d") 
    { 
     Console.Write("Toni je buzi"); 
    } 
    else if (wichOp == "e") 
    { 
     Console.Write("TONI ŠAMPION"); 
    } 
    else if (wichOp != "r") 
     Console.WriteLine("Nisi vnesil pravilno izbiro"); 
}