2014-02-15 48 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函数将退出,从而结束程序。