gpt4 book ai didi

Java 素数

转载 作者:行者123 更新时间:2023-12-01 08:13:02 25 4
gpt4 key购买 nike

我正在尝试制作一个素数列表。我已经对它进行了编码,但它只告诉我 1 - 100 的质数是 1。我不确定为什么会发生这种情况。我还想为它制作一个JFrame。

import javax.swing.JOptionPane;

public class ProgrammingAssignment7 {
public static void main(String[] args) {
//Scanner Scan = new Scanner (System.in);
//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" );
System.exit(0);
}
}

最佳答案

您正在 for 循环中调用 system.exit(0)。这样它将在第一次迭代后终止程序。删除该行,然后尝试运行程序。它会给你正确的结果。

关于Java 素数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15871554/

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