c# - do-while 循环(计算器)net.framework 的问题
问题描述
我的问题:即使我输入 +、-、*、/,我也陷入了 do-while 循环(选择一个运算符)。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Rechner_mit_Loop
{
class Program
{
static void Main(string[] args)
{
bool b_Konv1;
bool b_Konv2;
bool b_Konv3;
string s_Zahl1;
string s_Zahl2;
string s_Zahl3;
string s_OP;
string s_Ende;
int i_Zahl1;
int i_Zahl2;
int i_Zahl3;
int i_ZahlE;
int i_ZahlD;
do
{
do
{
Console.WriteLine("Erste Zahl eingeben!");
s_Zahl1 = Console.ReadLine();
b_Konv1 = int.TryParse(s_Zahl1, out i_Zahl1);
} while (!b_Konv1);
do
{
Console.WriteLine("Operator wählen!");
s_OP = Console.ReadLine();
} while (s_OP != "+" || s_OP != "-" || s_OP != "*" || s_OP != "/");
if (s_OP == "Hallo")
{
do
{
do
{
Console.WriteLine("Zweite Zahl eingeben!");
s_Zahl2 = Console.ReadLine();
b_Konv2 = int.TryParse(s_Zahl2, out i_Zahl2);
} while (!b_Konv2);
} while (i_Zahl2 == 0);
i_ZahlD = i_Zahl1 / i_Zahl2;
Console.WriteLine(i_Zahl1 + "/" + i_Zahl2 + "=" + i_ZahlD);
}
else
{
do
{
Console.WriteLine("Zweite Zahl eingeben!");
s_Zahl3 = Console.ReadLine();
b_Konv3 = int.TryParse(s_Zahl3, out i_Zahl3);
} while (!b_Konv3);
switch (s_OP)
{
case "Addition":
Console.WriteLine("Addition");
i_ZahlE = i_Zahl1 + i_Zahl3;
Console.WriteLine(i_Zahl1 + "+" + i_Zahl3 + "=" + i_ZahlE);
break;
case "-":
Console.WriteLine("Subtraktion");
i_ZahlE = i_Zahl1 - i_Zahl3;
Console.WriteLine(i_Zahl1 + "-" + i_Zahl3 + "=" + i_ZahlE);
break;
case "*":
Console.WriteLine("Multiplikation");
i_ZahlE = i_Zahl1 * i_Zahl3;
Console.WriteLine(i_Zahl1 + "*" + i_Zahl3 + "=" + i_ZahlE);
break;
}
}
Console.WriteLine("ENDE eingeben um Rechner zu beenden!");
s_Ende = Console.ReadLine();
} while (s_Ende != "ENDE");
Console.ReadLine();
}
}
}
解决方案
很简单就做while (s_OP != "+" && s_OP != "-" && s_OP != "*" && s_OP != "/")
否则它将始终为真,因为您的 s_OP 不能同时是 +、-、* 和 /