2014-10-14 3 views
-3

내 응용 프로그램은 소수를 테스트하는 것입니다. 처음으로 소수를 입력하면 결과는 true이며 사용자에게 숫자가 표시됩니다. 그러나 두 번째로 prime number check은 예상대로 작동하지 않습니다. 여기 내 코드는 다음과 같습니다.Windows Form 앱에서 소수 검사 문제

private void button1_Click(object sender, EventArgs e) 
{ 
    label3.Text = textBox1.Text; 

    float a = (float)Convert.ToDouble(textBox1.Text); 

    if (check_Number(ref a) == true) 
    { 
     ResultCheck.Text = "Input Number is Prime"; 
    } 
    else if (check_Number(ref a) == false) 
    { 
     ResultCheck.Text = "Input Number is not Prime"; 
    } 
} 
+2

"응용 프로그램이 실행되지 않는"선언 – RadioSpace

+0

check_Number''또는 적어도 코드를 게시하시기 바랍니다? – Blorgbeard

답변

0

여기는 평가판 나누기를 사용하는 예제 프로그램입니다.

enter image description here

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace PrimeCheck 
{ 
public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     label1.Text = textBox1.Text; 

     double testthis = 0; 
     if (double.TryParse(textBox1.Text, out testthis)) 
     { 
      if (CheckForPrime(testthis)) 
      { 
       MessageBox.Show("prime time!!"); 
      } 
     } 
    } 

    bool CheckForPrime(double number) 
    {//there are better ways but this is cheap and easy for an example 

     bool result = true;//assume we are prime until we can prove otherwise 

     for (double d = 2; d < number; d++) 
     { 
      if (number % d == 0) 
      { 
       result = false; 
       break; 
      } 
     } 

     return result; 
    } 
} 
} 
+0

코드에 감사드립니다. – phancuongviet

+0

@ user3555485 질문에 답변 한 경우 표시해주세요. – RadioSpace