在C#中测试Windows窗体应用程序

时间:2016-03-27 12:29:27

标签: c# visual-studio windows-forms-designer

我创建了一个Window Form Application来测试2个输入数字。输出必须根据此表: this table

我当前的程序创建了除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();
    }
}


}

1 个答案:

答案 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