Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java “印刷”;及;在最后一个整数之前_Java_Loops - Fatal编程技术网

Java “印刷”;及;在最后一个整数之前

Java “印刷”;及;在最后一个整数之前,java,loops,Java,Loops,我试图打印所有小于整数用户的素数,但在列表中最后一个素数之前打印“and”。有什么有效的方法可以做到这一点吗?例如,如果用户输入10,我希望它打印“2、3、5和7都是小于10的素数。” 公共类PrimeFinder{ 公共静态void main(字符串[]args){ 扫描仪kboard=新的扫描仪(System.in); int user=kboard.nextInt();//将用户设置为用户输入 对于(int k=1;k跟踪变量(例如previous)中的前一个素数。最初,将其设置为-1。(

我试图打印所有小于整数用户的素数,但在列表中最后一个素数之前打印“and”。有什么有效的方法可以做到这一点吗?例如,如果用户输入10,我希望它打印“2、3、5和7都是小于10的素数。”

公共类PrimeFinder{
公共静态void main(字符串[]args){
扫描仪kboard=新的扫描仪(System.in);
int user=kboard.nextInt();//将用户设置为用户输入

对于(int k=1;k跟踪变量(例如
previous
)中的前一个素数。最初,将其设置为-1。(您可能还需要使用额外的布尔变量跟踪是否需要在打印数字之前打印逗号)

如果找到素数,请打印上一个素数(如果不是-1),并将当前素数存储在
previous


最后,打印
和“+previous

您可以使用
StringBuilder
并在之后更新最后几个字符

// initialize size of buffer to a good estimate of what's required
StringBuilder output = new StringBuilder(k*18);
int last = 0;

for (int k=1; k<=user; k++) {
    // some other code here
    if (numFactors == 2) { // "k" is prime
        last = k;
        output.append(k).append(", ");
    }
    // some other code here
}

String lastText = Integer.toString(last);
int lastIndex = output.lastIndexOf(", " + lastText);
output.setLength(lastIndex);
output.append(" and " + lastText);

你可以使用一个数组列表来存储它们。找到所有素数后,你可以很容易地分辨出哪个是该范围内的最后一个

import java.util.ArrayList;
import java.util.Scanner;

public class PrimeFinder {
    public static void main(String[] args) {

        Scanner kboard = new Scanner(System.in);
        int user = kboard.nextInt(); // sets user to users input
        //Iltis: create array list
        ArrayList<Integer> prime = new ArrayList<Integer>();

        for (int k = 1; k <= user; k++) {

            int numFactors = 0;

            for (int a = 1; a <= k; a++) {

                if (k % a == 0)
                    numFactors++; // if a is divisible by k then add one to
                                    // numFactors

            }
            if (numFactors == 2) { // "k" is prime
                //Iltis: add all primes
                prime.add(k);
            }

        }
        for (int i = 0; i < prime.size()-2; i++) {
            //Iltis: printing all primes except the last two
            System.out.print(prime.get(i)+", ");
        }
        //Iltis: print the last two
        System.out.print(prime.get(prime.size()-2)+" and "+prime.get(prime.size()-1));
        System.out.print(" are all off the primes less than " + user + ".");
    }
}
import java.util.ArrayList;
导入java.util.Scanner;
公共类素数查找器{
公共静态void main(字符串[]args){
扫描仪kboard=新的扫描仪(System.in);
int user=kboard.nextInt();//将用户设置为用户输入
//Iltis:创建数组列表
ArrayList prime=新的ArrayList();
对于(int k=1;k
System.out.print(output.toString() + 
    " are all off the primes less than " + user + ".");
import java.util.ArrayList;
import java.util.Scanner;

public class PrimeFinder {
    public static void main(String[] args) {

        Scanner kboard = new Scanner(System.in);
        int user = kboard.nextInt(); // sets user to users input
        //Iltis: create array list
        ArrayList<Integer> prime = new ArrayList<Integer>();

        for (int k = 1; k <= user; k++) {

            int numFactors = 0;

            for (int a = 1; a <= k; a++) {

                if (k % a == 0)
                    numFactors++; // if a is divisible by k then add one to
                                    // numFactors

            }
            if (numFactors == 2) { // "k" is prime
                //Iltis: add all primes
                prime.add(k);
            }

        }
        for (int i = 0; i < prime.size()-2; i++) {
            //Iltis: printing all primes except the last two
            System.out.print(prime.get(i)+", ");
        }
        //Iltis: print the last two
        System.out.print(prime.get(prime.size()-2)+" and "+prime.get(prime.size()-1));
        System.out.print(" are all off the primes less than " + user + ".");
    }
}