gpt4 book ai didi

java - 了解我的素数计算和素数 JFrame 发生了什么

转载 作者:行者123 更新时间:2023-12-02 13:00:22 26 4
gpt4 key购买 nike

我想找出我的代码出了什么问题,并帮助理解我的素数计算和素数 JFrame 发生了什么情况。

  1. 由于某种原因,计算结果只是说 1-100 的素数是
    1. 我拿出了system.exit(0);现在我需要在退出之前点击所有数字。我希望实现的是得到一个列出 1 到 100 之间所有素数的列表。
  2. 然后通过编码素数计算,我需要制作一个素数 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.");

*/


}
}
}

最佳答案

  • setvisible(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); 缺少 +并且有太多的"。我认为应该是 JOptionPane.showMessageDialog(null, "You Pressed the "+ sActionCommand + "按钮", this.getTitle(), JOptionPane.INFORMATION_MESSAGE);
  • ProgrammingAssignment8 = new ProgrammingAssignment8("Instantiates with a Method"); 不是有效的赋值,您实际上并未将值分配给任何内容...类似于 ProgrammingAssignment8 assignment = newProgrammingAssignment8("实例化方法"); 将修复它。

以下内容不是有效评论...

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

这是...

/***********************************************************************************
PRIME NUMBERS CALCULATIONS
***********************************************************************************/
  • 我没有 CalculateButtonHandlerClearButtonHandlerExitButtonHandlerjbtnCalculatejbtnClear 的定义code>、jbtnExitfontChosenFont 所以我无法发表评论,不用说,在它们写完之前它不会起作用......

关于java - 了解我的素数计算和素数 JFrame 发生了什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15872609/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com