2

我想弄清楚我的代码出了什么问题,并帮助理解我的 Prime Numbers Calculation 和 Prime Numbers 发生了什么JFrame

  1. 出于某种原因,计算一个只是说 1-100 的素数是
    1. 我拿出了system.exit(0);,现在我需要在退出之前点击所有数字。我希望实现的是获得一个列出所有素数 1-100 的列表。
  2. 然后通过编码的素数计算,我需要制作一个 Prime Numbers JFrame。我有太多错误,这是我的第一个问题,其次,由于错误,我无法运行项目来查看它。

我已经将我的代码发布到素数计算和我的JFrame. 任何帮助弄清楚发生了什么/错误将不胜感激。

*********************************************************************************
//Prime Numbers Java Swing JFrame
**********************************************************************************

 import javax.swing.*;
 import java.awt.*;
 import java.awt.event.*;


 public class ProgrammingAssignment8 extends JFrame 
 implements ActionListener 
 {

//DECLARE BUT DO NOT INSTANTIATE
private JButton calculate;
private JButton clear;
private JButton exit;

//PANELS
private JPanel jpnlMain = new JPanel();

private JPanel jpnlTop = new JPanel();
private JPanel jpnlCenter = new JPanel();
private JPanel jpnlBotton = new JPanel();

private JScrollPane scrollingResult;


//DECLARE JFRAME COMPONENTS
private JLabel jlblName, jlblMaxTest;
private JTextField jtfName, jtfMaxTest;
private JTextArea jtaName, jtaMaxNumbers;
private JButton jbutCalculate, jbutClear, jbutExit;

private CalculateButtonHandler calculateHandler;
private ClearButtonHandler clearHandler;
private ExitButtonHandler exitHandler;





/**
 * @param args
 */

//CLASS CONSTRUCTOR 
public ProgrammingAssignment8 (String sTitle) 
{       

//PREPARE THE JFRAME/WINDOW
    super(sTitle);
//SET TITLE AND SIZE
setTitle(sTitle);
setSize(400,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//SET THE LAYOUT
jpnlMain.setLayout(new GridLayout(3,3,5,5));

 //INSTANTIATE THE JBUTTONS WITH THE PASSED CAPTION AND COLOR AND ADD TO JPANEL
//PUT INTO A METHOD FOR EFFICIENCY AND TO AVOID REPEATING ALL THE CODE
jbtnCalculate = instantiateJButton("Calculate", Color.red);
jbtnClear = instantiateJButton("Clear", Color.blue);
jbtnExit = instantiateJButton("Exit", Color.black);

//INSTANTIATE THE JLABEL COMPONENTS
jlblName = new JLabel ("");

//PREPARE THE CONTAINER
Container ca = getContentPane();
}

set visible(true);


//END CONSTRUCTOR


private JButton instantiateJButton(String, sInCaption, Color inColor)
{
JButton jbtnButton = new JButton(sInCaption);
jbtnButton.setActionCommand(sInCaption);
jbtnButton.addActionListener(this);
jbtnButton.setForeground(inColor);
jbtnButton.setFont(fontChosenFont);
jpnlMain.add(jbutnButton);
return jbtnButton;

//END INSTANTIATE JBUTTON

//CLASS EVENT HANDLER   
public void actionPerformed(java.awt.event.ActionEvent e)

{
    //FIND OUT WHICH BUTTON WAS PRESSED BY USING THE ACTION COMMAND VALUE
    String sActionCommand = e.getActionCommand();

    //FROM THE ACTIONEVENT OBJECT, GET AN INSTANCE OF THE JBUTTON THAT WAS PRESSED
    JButton jbtnSource = (JButton) e.getSource();

    jbtnSource.setEnabled(false);

    //LET THE USER KNOW WHAT BUTTON WAS CLICKED
    JOptionPane.showMessageDialog(null,  "You pressed the "" sActionCommand + " button", this.getTitle(), JOptionPane.INFORMATION_MESSAGE);
}

//END ACTIONPERFORMED (java.awt.event.ActionEvent e)

//EXECUTION STARTING POINT

public static void main(String[] args)
{
    ProgrammingAssignment8 = new ProgrammingAssignment8("Instantiates with a Method");

}
//END main(String[] args)


}
//END ProgrammingAssignment8 CLASS


***********************************************************************************
PRIME NUMBERS CALCULATIONS
***********************************************************************************

import javax.swing.JOptionPane;
public class ProgrammingAssignment7 
{

/**
 * @param args
 * @return 
 */
public static void main(String[] args) 
{
    //Scanner Scan = new Scanner (System.in);
    // TODO Auto-generated method stub

    //DECLARE VARIABLES
    int x = 1;
    int i = 1;
    int iNumber = 1;
    boolean bNotPrime = false;
    boolean bIsPrime = true;
    int iNumberToTest;
    int iPrimeCheck;
    int iCounter;
    int iResult = 1;
    int iFact = 1;
    int iLimit = 100;
    String OutputStr = null;





    System.out.println("Prime numbers between 1 and " + iLimit);

    //loop through the numbers one by one
    for(i=1; i < 100; i++)
    {

            bIsPrime = true;

            //check to see if the number is prime
            for(int j = 2; j < i ; j++){

                    if(i % j == 0){
                            bIsPrime = false;
                            break;
                    }
            }
            // print the number
            if(bIsPrime)

                OutputStr = "The Prime Numbers of 1 - 100 are: " + i + "\n";

                JOptionPane.showMessageDialog(null, OutputStr, "PRIME NUMBERS", JOptionPane.INFORMATION_MESSAGE);
                   //System.out.print(i + "\n" );






    //LISTS NUMBERS 1 THROUGH 100, INITIALIZE INTEGER NAMED I, IF IT IS LESS THAN OR EQUAL TO 100 THEN ADD ONE TO THE LOOP



    /* for (int i=1; i<=100; i++)
    {
        System.out.println(i);
    }



    /* iNumber = 7;

    for (iNumber = 1; iNumber <= 100; iNumber++)
        iResult = iNumber / 1;
    System.out.println(iResult);

    /* for ( iNumber = 2; iNumber <= 100; iNumber++ )
    {
      if ( iNumber % iNumber == 0 )
      {
        bIsPrime = false;
    break;

      }
    }

    return bIsPrime;
}

*/





    /* iNumber = 1; 
    while (iNumber < 100) iNumber++; 
    { 
        System.out.print(iNumber + " ");
        System.out.println();


        for (iNumber = iNumber - 1; iNumber > 2; iNumber++) 
        { //divides the number by each number less than the number itself and also greater than 1 (because 1 is a factor of every number, and every number is a factor of itself)
            if (iNumber % iNumber != 0) 
                System.out.println(iNumber + " is a prime number.");




            { //else if the number is evenly divisible by a number other than itself and 1 (we weeded out those cases in the above for statement), then it sets the boolean to be true, and breaks the for loop
                bNotPrime = false;
                iNumber = 1;
            }

        }

        if (bIsPrime = true) 
        { //if the number is not not a prime, then it prints the number and the for loop moves on 

        }

        else if (bNotPrime = false)
        { //otherwise the for loop moves on
            System.out.println(iNumber + " is not a prime number.");

            */              


}
}
}
4

2 回答 2

5
  • set visible(true);在构造函数之外并且不是有效的方法名称,它应该是setVisible(true);,向上移动,以便它出现在}它当前上方的之前......
  • private JButton instantiateJButton(String, sInCaption, Color inColor)不是有效的方法签名,您在不需要的地方添加了一个,String它应该是private JButton instantiateJButton(String sInCaption, Color inColor)
  • JOptionPane.showMessageDialog(null, "You pressed the "" sActionCommand + " button", this.getTitle(), JOptionPane.INFORMATION_MESSAGE);缺少 a+并且有太多". 我认为应该是JOptionPane.showMessageDialog(null, "You pressed the " + sActionCommand + " button", this.getTitle(), JOptionPane.INFORMATION_MESSAGE);
  • ProgrammingAssignment8 = new ProgrammingAssignment8("Instantiates with a Method");不是一个有效的分配,你实际上并没有将值分配给任何东西......类似的东西ProgrammingAssignment8 assignment = new ProgrammingAssignment8("Instantiates with a Method");会解决它。

以下不是有效的评论...

***********************************************************************************
PRIME NUMBERS CALCULATIONS
***********************************************************************************

这是...

/***********************************************************************************
PRIME NUMBERS CALCULATIONS
***********************************************************************************/
  • CalculateButtonHandler我对, ClearButtonHandler, ExitButtonHandler, jbtnCalculate,没有定义jbtnClear,所以我无法发表评论jbtnExitfontChosenFont不用说,在写出来之前它不会起作用......
于 2013-04-08T06:39:27.403 回答
3

您可以做的是将质数存储在集合中。看看例如 ArrayList。它就像一个数组,但会动态增长,因此您不必提前指定大小。

您有一个列表,您可以将该列表显示给用户。

于 2013-04-08T06:38:51.123 回答