请,我需要帮助打印这些数组。在方法“IsPrime”中它测试输入是否为素数并将它们存储在数组“prime []”中我将其设置为数组的大数,因为我不可能知道输入将获得的确切素数和“palin”数组也是如此。在最后一个for循环中,我试图打印素数数组的结果,但我得到的输出不是素数。我得到“0000000000”谢谢!
import java.util.Scanner;
import java.util.Arrays;
public class p1c {
private static Scanner scan;
public static void main(String[] args){
scan = new Scanner(System.in);
int z;
for(z = 0; z<2; z++){
System.out.println("\n Please enter the first integer number:" );
int numx = scan.nextInt();
System.out.println("Please enter the second integer number");
int numy = scan.nextInt();
if ( numx < numy){
isPrime(numx,numy);
}
else{
System.out.print("This is invalid, try again, x must be less than y");
System.exit(0);
}
}
}
public static int reverse(int i){
int reverse = 0;
while (i!= 0){
reverse = reverse * 10;
reverse = reverse + i % 10;
i = i/10;
}
return reverse;
}
public static void isPrime(int numx, int numy){
int d = 0,c;
int prime[] = new int[50];
int palin[] = new int[50];
for (int i=numx; i <= numy; i++ ){
for (c=2; c<i; c++){
int n = i%c;
if (n==0){
break;
}
}
if(i == c && i != 0 && c != 0){
prime[i]=i;
}
if( reverse(i)== i){
int palindrome = i;
palin[palindrome]= palindrome;
}
}
for(int count = 0; count < prime.length;count ++){
d++;
System.out.print(prime[count]);
if(d == 10){
System.out.println();
d=0;
}
}
}
}
答案 0 :(得分:0)
我认为您的问题与您选择阵列有关。因此,当您打印出阵列的内容时,它会打印50个元素 - 大多数为0,因为这是您初始化它的原因。
请改用List<Integer>
。例如:
List <Integer> prime = new ArrayList<Integer> () ;
然后在if条件下,找到素数时,请prime.add(i);
打印出列表内容时,在打印内容时使用prime.size()
表示循环上限,prime.get(count)
。
答案 1 :(得分:0)
如果有人想要或曾经有过这个糟糕的项目,这是最终的工作代码!
import java.util.Scanner;
import java.util.Arrays;
public class p1c {
private static Scanner scan;
public static void main(String[] args) {
scan = new Scanner(System.in);
int z;
for (z = 0; z < 2; z++) {
System.out.println("\n Please enter the first integer number:");
int numx = scan.nextInt();
System.out.println("Please enter the second integer number");
int numy = scan.nextInt();
if (numx < numy) {
isPrime(numx, numy);
}
else {
System.out.print("This is invalid, try again, x must be less than y");
System.exit(0);
}
}
}
public static int reverse(int i) {
int reverse = 0;
while (i != 0) {
reverse = reverse * 10;
reverse = reverse + i % 10;
i = i / 10;
}
return reverse;
}
public static void isPrime(int numx, int numy) {
int d = 0, c;
int x = 0;
int prime[] = new int[1000000];
int palin[] = new int[500000];
for (int i = numx; i <= numy; i++) {
for (c = 2; c < i; c++) {
int n = i % c;
if (n == 0) {
break;
}
}
if (i == c && i != 0 && c != 0) {
prime[i] = i;
}
if (reverse(i) == i) {
int palindrome = i;
palin[palindrome] = palindrome;
}
}
System.out.println("Prime:");
for (int count = 0; count < prime.length; count++) {
if (prime[count] != 0) {
d++;
System.out.print("\t" + prime[count]);
}
if (d == 10) {
System.out.println();
d = 0;
}
}
System.out.println("\n palindromic Prime:");
for (int counter = 0; counter < palin.length; counter++) {
if (palin[counter] != 0) {
x++;
System.out.print("\t" + palin[counter]);
}
if (x == 10) {
System.out.println();
x = 0;
}
}
}
}