我创建了一个Window Form Application来测试2个输入数字。输出必须根据此表:
我当前的程序创建了除7,5之外的所有数字的预期输出。它给了我1,2而不是6,7
我不确定是什么导致这种情况发生。
到目前为止,这是我的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WhiteBox
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnStart_Click(object sender, EventArgs e)
{
int one = int.Parse(tbInputOne.Text);
int two = int.Parse(tbInputTwo.Text);
clearOutput();
runCal(one, two);
}
private void clearOutput(){
tbOneA.Clear();
tbOneB.Clear();
tbTwoA.Clear();
tbTwoB.Clear();
tbThreeA.Clear();
tbThreeB.Clear();
tbFinalA.Clear();
tbFinalB.Clear();
}
private void runCal(int a, int b){
if (a>=b){
int temp = b;
b = a;
a = temp;
tbOneA.Text = a.ToString();
tbOneB.Text = b.ToString();
}
if ((Math.Cos(a)) == 0 || (Math.Cos(b) >= 0)){
//Changed the < to ==
a = 1;
b = 3;
tbTwoA.Text = a.ToString();
tbTwoB.Text = b.ToString();
}
int x = (a + b) / 2;
if (Math.Cos(x) > 0){
a = x;
tbThreeA.Text = a.ToString();
}
else {
b = x;
tbThreeB.Text = b.ToString();
}
tbFinalA.Text = a.ToString();
tbFinalB.Text = b.ToString();
}
}
}
答案 0 :(得分:1)
检查runCal的流程:
输入:a = 7,b = 5
if (a>=b) => (7>=5) => true
{
swap(a,b)
}
首先是:a = 5,b = 7
if((Math.Cos(a)) == 0 || (Math.Cos(b) >= 0)) => (0.283662185 == 0 || (0.753902254 >= 0)) => false || true => true
{
a=1, b=3
}
在第二个if之后:a = 1,b = 3
x = (a + b) / 2 = 2
if (Math.Cos(x) > 0) => (-0.416146837 > 0) => false
*here go to else
{
b = x = 2
}
结尾:a = 1,b = 2