勾股数列表(Find Pythagorean Triples)

来源:互联网 发布:知乎口腔溃疡 冰硼散 编辑:程序博客网 时间:2024/06/08 04:44
//Find Pythagorean Triples within 500
//Java how to program, 5/e, Exercise 5.17
import javax.swing.*;
public class PythagoreanTriples {
    
public static void main (String arg[])
   
{
        
boolean guard;
        
int counter=1, number;
        String Number;
        Number
=JOptionPane.showInputDialog("Enter the limit number:");
        number
=Integer.parseInt(Number);
        JTextArea output
=new JTextArea(17,20);
        JScrollPane scroller
=new JScrollPane(output);
        output.setText(
"N1/t N2/tN3/t");
        
for (int i=1;i<=number;i++)
            
{for (int j=i+1;j<=number;j++)
                
{for (int k=j+1;k<=number;k++)
                    
{guard=i*i+j*j==k*k;
                    
if (guard)
                        
{output.append(i+"/t "+j+" /t"+k+" /t");
                    counter
++;}

                    }

                }

            }

        output.append(
" Total Pythagorean Triples number:"+counter);
        JOptionPane.showMessageDialog(
null, scroller,"Pythagorean Triples",
                JOptionPane.INFORMATION_MESSAGE);
    }

}

 
原创粉丝点击