如何打印素数到用户输入的整数? [英] How to print prime numbers up to the user's entered integer?

查看:133
本文介绍了如何打印素数到用户输入的整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家下午好,

我正在尝试创建一个执行以下操作的程序:

I'm currently trying to create a program that does the following:

开发一个代码,打印所有素数,直到用户输入的
数。输出示例:

Develop a code that will print all prime numbers up to the user's entered number. An example of the output:

 Enter an integer (2 or above): 19

 The prime numbers up to you integer are:

 2

 3

 5

 7

 11

 13

 17

 19

不幸的是,还有一个要求清单也需要满足:

Unfortunately, there is also a list of "requirements" that need to be met as well:


  • 如果用户输入的数字低于2,你的程序应该打印一个数字无效的消息,然后停止。

  • 如果数字不能被任何数字整除,则数字是素数除了1和它本身。

  • 对于这个程序,为了测试一个数字以查看它是否为素数,你应该尝试将数字除以2中的每个值到数字-1,看它是否均匀分开。例如:

  • If the user enters a number below 2, your program should print a message that the number is not valid, and then stop.
  • A number is a prime number if it is not divisible by any number except 1 and itself.
  • For this program, in order to test a number to see if it is prime, you should try to divide the number by every value from 2 up to the number-1, to see if it divides evenly or not. For example:

- 要查看5是否是素数:
5不均匀地划分2
5不均匀划分3
5不能均分4
因此5是素数

--To see if 5 is a prime number: 5 does not divide evenly by 2 5 does not divide evenly by 3 5 does not divide evenly by 4 therefore 5 is a prime number

- 查看9是素数:
9未平均分为2
9平均分为3
因此9不是素数

--To see if 9 is a prime number: 9 does not divide evenly by 2 9 divides evenly by 3 therefore 9 is not a prime number

此程序要求你要编写嵌套循环(即循环内的循环)。将使用一个循环从2到用户的数字进行计数,以便您可以测试这些数字中的每一个以确定它是否为素数。对于这些数字中的每一个,x:

This program requires you to write nested loops (that is, a loop inside a loop). One loop will be used to count from 2 up to the user's number so that you can test each of these numbers to see it it is prime. For each of these numbers, x:

上面的问题是关于我的代码到目前为止:

The question above is in regards to my code so far below:

import java.util。*;

import java.util.*;

public class Something3 {

public class Something3 {

public static void main(String[] args) {

    Scanner kbd = new Scanner(System.in);

    //Variable declaration.
    int limit, number;

    //get input till which prime number to be printed
    System.out.print("Enter an integer (2 or above): ");
    limit = kbd.nextInt();
    kbd.close();

    //Will print prime numbers till the limit (user entered integer).
    number = 2;

    if (limit >=2) {
        System.out.println("The prim numbers up to your interger are: " + limit+"\n");
        for(int i = 0; i <= limit;){         
            //print prime numbers only
            if(isPrime(number)){
                System.out.println(number +"\n");
                i++;
            } 
            number = number + 1;
        }
    }
    else
        System.out.println("Number is not vaild");

}

//Prime number is not divisible by any number other than 1 and itself
//return true if number is prime. 
public static boolean isPrime(int number){
    for(int i=2; i==number; i++){
        if(number%i == 0){
            return false; //Number is divisible, thus not prime.
        }
    }
    return true;//The number is prime.
}

}

极限变量我正在使用这个问题吗?任何帮助将非常感激。

Is the limit variable I'm using the issue? Any help would be much appreciated.

推荐答案

您的实际问题是 number 变量。这是你的解决方案。不需要变量 number 。以下是您的解决方案

Your actual problem was number variable. Here's your solution. There's no need of variable number. Below is your solution

import java.util.*;

public class Something3 {

public static void main(String[] args) {

    Scanner kbd = new Scanner(System.in);

    // Variable declaration.
    int limit;

    // get input till which prime number to be printed
    System.out.print("Enter an integer (2 or above): ");
    limit = kbd.nextInt();
    kbd.close();

    if (limit >= 2) {
        System.out.println("The prim numbers up to your interger are: "
                + limit + "\n");
        for (int i = 1; i <= limit; i++) {
            // print prime numbers only
            if (isPrime(i)) {
                System.out.println(i);
            }
        }
    } else
        System.out.println("Number is not vaild");

}

// Prime number is not divisible by any number other than 1 and itself
// return true if number is prime.
public static boolean isPrime(int n) {
    if (n % 2 == 0)
        // The only even prime is 2.
        return (n == 2);
    for (int i = 3; i * i <= n; i += 2) {
        if (n % i == 0)
            return false;
    }
    return true;
}
}

这篇关于如何打印素数到用户输入的整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆