数字が素数で、その前に素数がある場合に出力するプログラムを作成していますが、問題が1つあります。私は持っている:
if(i != 2){ System.out.println(primes[i] + ", "); else System.out.println(primes[i] + ".");
数値が 2 の場合、「2」と表示されます。代わりに、3 で終わる任意の数字のピリオドを配置します。何が問題なのですか? :P これが私のコードです
package me.Jesse.PrimeCalc;
import java.util.Scanner;
public class PrimeArrayCalculator {
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
int primeCounter = 0;
int arrayCounter = 0;
int[] primes;
int input;
System.out.println("Please Enter A Number.");
input = scan.nextInt();
System.out.println("Calculating Results... (This may take a while!) ");
System.out.println("");
if(isPrimeMethod.isPrime(input) == true){
System.out.println(input + " Is Prime!");
System.out.println("Calculating The Prime Numbers Before " + input + "...");
System.out.println("");
}else{
System.out.println(input + " Is Not Prime.");
System.out.println("Calculating The Prime Numbers Before " + input + "...");
System.out.println("");
}
for (int i = input; i <= input; i--){
if(isPrimeMethod.isPrime(i) == true){
primeCounter++;
}
}
primes = new int[primeCounter++];
for (int i = input; i <= input; i--){
if(isPrimeMethod.isPrime(i) == true){
primes[arrayCounter] = i;
arrayCounter++;
}
}
System.out.println("Here Are All The Prime Numbers Before " + input + ":");
System.out.println("");
for (int i = 0; i <= primeCounter; i++){
try{
if (primeCounter != 2)
System.out.println(primes[i] + ",");
else
System.out.println(primes[i] + ".");
}catch(ArrayIndexOutOfBoundsException e){
// Yes I know the program trys to print from two invalid array index's. Try/Catch FTW.
}
}
}
}