2017-03-02 102 views
-6
static void Main(string[] args) 
    { 
     double a, b, c, Result; 
     while (true) // and is good idea to use while to keep backing to main ?? 
     { 
       Console.WriteLine("\t\t\t\t\t\t\tCalcualtor"); 
       Console.WriteLine("\t\t\t\t\t\t\t----------"); 
      Console.WriteLine("put Number Plz"); 
      a = Convert.ToInt32(Console.ReadLine()); 
      Console.WriteLine("put other Number Plz"); 
      b = Convert.ToInt32(Console.ReadLine()); 
      Console.WriteLine("Choose :*,+,-,/"); 
      string uservalue = Console.ReadLine(); 
      if (uservalue == "*") 
      { 
       Result = a * b; 
       Console.Write("Resultat= " + a * b); 
      } 
      else if (uservalue == "+") 
      { 
       Result = a + b; 
       Console.Write("Resultat= " + (a + b)); 
      } 
      else if (uservalue == "-") 
      { 
       Result = a - b; 
       Console.Write("Resultat= " + (a - b)); 
      } 
      else if (uservalue == "/") 
      { 
       Result = a/b; 
       Console.Write("Resultat= " + (a/b)); 
      } 
      else 
       Console.WriteLine("put right operator !!!"); 

//我需要在這裏開始我已經做過的所有事情意味着循環即時仍然新與C#任何建議?完成計算後保持循環

  Console.ReadLine(); 
      Console.Clear(); 
     } 
    } 
} 

}

好這個問題,我問它的時間以前,我不知道很多關於C#所以我已經解決它

+3

我沒有看到一個問題呢,只是一串代碼。另外,不要使用'goto' – BradleyDotNET

+0

問題是在別的我需要回到控制檯用戶值,所以我怎麼做? – Elmissouri

+0

這不是一個連貫的問題。您已經打印了計算結果... – BradleyDotNET

回答

0

其現在的工作完美

static void Main(string[] args) 
    { 
     double a, b,Result; 
     while (true) 
     { 
      Console.WriteLine("\t\t\t\t\t\t\tCalcualtor"); 
      Console.WriteLine("\t\t\t\t\t\t\t----------"); 
      Console.WriteLine("put Number Plz"); 
      a = Convert.ToInt32(Console.ReadLine()); 
      Console.WriteLine("put other Number Plz"); 
      b = Convert.ToInt32(Console.ReadLine()); 
      Console.WriteLine("Choose :*,+,-,/"); 
      bool successful; 
      do 
      { 
       string uservalue = Console.ReadLine(); 
       if (uservalue == "*") 
       { 
        Result = a * b; 
        Console.Write("Resultat= " + a * b); 
        successful = true; 
       } 
       else if (uservalue == "+") 
       { 
        Result = a + b; 
        Console.Write("Resultat= " + (a + b)); 
        successful = true; 
       } 
       else if (uservalue == "-") 
       { 
        Result = a - b; 
        Console.Write("Resultat= " + (a - b)); 
        successful = true; 
       } 
       else if (uservalue == "/") 
       { 
        Result = a/b; 
        Console.Write("Resultat= " + (a/b)); 
        successful = true; 
       } 
       else 
       { 
        Console.WriteLine("put right operator !!!"); 
        successful = false ; 
       } 


       } 
      while (!successful); 
      { 
      Console.ReadLine(); 
      Console.Clear(); 
      } 

      } 
     }  
    } 
} 
0

如果你正在尋找一個完全新手的解決方案,您可能要提高對手術成功的標誌,而包裝操作員輸入到另一個循環:

bool successful; 
do 
{ 
    string uservalue = Console.ReadLine(); 
    if (uservalue == "*") 
    { 
     Result = a * b; 
     Console.Write("Resultat= " + a * b); 
     successful = true; 
    } 
    //other operators 
    else 
     Console.WriteLine("put right operator !!!"); 
} while (!successful) 

此外,我會建議開關/箱sonstruction,這樣的:

string uservalue = Console.ReadLine(); 
switch (uservalue) 
{ 
    case "*": 
     Console.Write("Resultat= " + (a * b)); 
     break; 
    //other operators 
    default: 
     Console.WriteLine("put right operator !!!"); 
     break; 
} 

PS:你要指定當值的變量「結果」,而第二個輸出字符串時,如果你已經在你的變量有值計算你的價值觀兩次,一次,你應該叫Console.Write("Resultat= " + Result);

+0

我很好嘗試添加到我的代碼我欣賞幫助 – Elmissouri

0

我想創建函數來讀取用戶輸入:

static double GetNumber(string prompt) { 
    double answer; 
    Console.WriteLine(prompt); 
    while (true) { 
     if (double.TryParse(Console.ReadLine(), out answer)) { 
      return answer; 
     } 
     Console.WriteLine("Error: Try again"); 
    } 
} 
static string GetOperator(string prompt) { 
    string answer; 
    Console.WriteLine(prompt); 
    while (true) { 
     answer = Console.ReadLine(); 
     if ("*/+-".Contains(answer)) { 
      return answer; 
     } 
     Console.WriteLine("Error: Try again"); 
    } 
} 

然後你的主變:

static void Main(string[] args) 
{ 
    while (true) 
    { 
     Console.WriteLine("\t\t\t\t\t\t\tCalcualtor"); 
     Console.WriteLine("\t\t\t\t\t\t\t----------"); 
     double a = GetNumber("put Number Plz"); 
     double b = GetNumber("put other Number Plz"); 
     string uservalue = GetOperator("Choose :*,+,-,/"); 
     if (uservalue == "*") 
     { 
      Console.Write("Resultat= " + a * b); 
     } 
     else if (uservalue == "+") 
     { 
      Console.Write("Resultat= " + (a + b)); 
     } 
     else if (uservalue == "-") 
     { 
      Console.Write("Resultat= " + (a - b)); 
     } 
     else if (uservalue == "/") 
     { 
      Console.Write("Resultat= " + (a/b)); 
     } 
     Console.ReadLine(); 
     Console.Clear(); 
    } 
+0

我很好,謝謝你的幫助 – Elmissouri