2014-02-15 44 views
-1

在我的程序中,如果bool選項= false,我需要關閉程序。 「while(option == false)」語句位於代碼的底部。這裏是我的代碼:關閉c#程序

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

namespace Calculator 
{ 
class Program 
{ 
    static void Main(string[] args) 
    { 
     double numA = 0; 
     double numB = 0; 
     double answer = 0; 
     string functionType; 
     string aLine; 
     string bLine; 
     string next; 
     bool option = true; 

     while (option == true) 
     { 

      Console.WriteLine("Enter a function type: "); 
      functionType = Console.ReadLine(); 

      switch (functionType) 
      { 

       case "V": 
        Console.WriteLine(" + Addition" + '\n' + " - Subtraction" + '\n'    + " * Multiplication" + '\n' + "/ Division" + '\n' + " % Percent" + '\n' + " \\ Square Root" + '\n' + "^ Exponent"); 
        Console.WriteLine("Enter a function type: "); 
        functionType = Console.ReadLine(); 
        break; 

       case "v": 
        Console.WriteLine(" + Addition" + '\n' + " - Subtraction" + '\n' + " * Multiplication" + '\n' + "/ Division" + '\n' + " % Percent" + '\n' + " \\ Square Root" + '\n' + "^ Exponent"); 
        Console.WriteLine("Enter a function type: "); 
        functionType = Console.ReadLine(); 
        break; 

      } 

      Console.WriteLine("Enter the first number: "); 
      aLine = Console.ReadLine(); 
      numA = Double.Parse(aLine); Console.WriteLine("Enter the second number: "); 

      switch (functionType) 
      { 

       case "\\": 
        answer = Math.Sqrt(numA); 
        Console.WriteLine("The answer is: " + answer + '\n'); 

      Console.WriteLine("Would you like to do more calculations? (Y/N): "); 
      next = Console.ReadLine(); 

      switch (next) 
      { 
       case "Y": 
        option = true; 
        Console.WriteLine('\n'); 
        break; 

       case "y": 
        option = true; 
        Console.WriteLine('\n'); 
        break; 

       case "N": 
        option = false; 
        break; 

       case "n": 
        option = false; 
        break; 

      } 
      continue; 
        break; 

       case "%": 
        answer = numA * 100; 
        Console.WriteLine("The answer is: " + answer + '\n'); 

      Console.WriteLine("Would you like to do more calculations? (Y/N): "); 
      next = Console.ReadLine(); 

      switch (next) 
      { 
       case "Y": 
        option = true; 
        Console.WriteLine('\n'); 
        break; 

       case "y": 
        option = true; 
        Console.WriteLine('\n'); 
        break; 

       case "N": 
        option = false; 
        break; 

       case "n": 
        option = false; 
        break; 

      } 
        break; 
        continue; 

      } 

      Console.WriteLine("Enter the second number: "); 
      bLine = Console.ReadLine(); 
      numB = Double.Parse(bLine); 

      switch (functionType) 
      { 

       case "+": 
        answer = numA + numB; 
        break; 

       case "-": 
        answer = numA - numB; 
        break; 

       case "x": 
        answer = numA * numB; 
        break; 


       case "*": 
        answer = numA * numB; 
        break; 


       case "X": 
        answer = numA * numB; 
        break; 

       case "/": 
        answer = numA/numB; 
        break; 

       case "^": 
        answer = Math.Pow(numA, numB); 
        break; 
      } 

      Console.WriteLine("The answer is: " + answer + '\n'); 

      Console.WriteLine("Would you like to do more calculations? (Y/N): "); 
      next = Console.ReadLine(); 

      switch (next) 
      { 
       case "Y": 
        option = true; 
        Console.WriteLine('\n'); 
        break; 

       case "y": 
        option = true; 
        Console.WriteLine('\n'); 
        break; 

       case "N": 
        option = false; 
        break; 

       case "n": 
        option = false; 
        break; 

      } 


     } 

     while (option == false) 
     { 

     } 

    } 
} 
} 

原諒我,如果這是一個簡單的答案,但我一直在學習c#約2小時。

+0

是的,但只有關於c和C++的答案出現。 – user3259479

+1

我什麼都不明白。你想在最後的while循環中做些什麼嗎?如果你想關閉程序,然後刪除while循環,它將關閉 –

+0

這是一個控制檯應用程序,只是擺脫你的'while(option == false)',它應該會自動關閉,因爲你不在Main中什麼是保持你的程序活着。 – TyCobb

回答

4

最後你不需要while (option == false)循環。你需要做的是檢查每個用戶輸入後的值,只有在條件不滿足時才進行。即,每個開關語句後:

if (!option) 
    return; 

從外這將退出while循環,然後Main函數將退出,從而結束程序。