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

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

问题描述

大家下午好,

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

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 到 number-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:

导入 java.util.*;

import java.util.*;

公共类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+"
");
        for(int i = 0; i <= limit;){         
            //print prime numbers only
            if(isPrime(number)){
                System.out.println(number +"
");
                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 + "
");
        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天全站免登陆