【问题标题】:How can I make this code print how many prime digits there are in a number?如何使此代码打印一个数字中有多少个质数?
【发布时间】:2020-01-31 04:45:43
【问题描述】:

到目前为止,我遇到的问题与我得到的每个响应之间的区别在于,我正在尝试让代码打印有多少位是素数,而不是原始数字中有多少个素数。例如,如果用户输入 567,我需要测试 5、6 和 7 并判断有多少是素数。

无论如何-我正在尝试使此代码打印用户输入的数字中有多少质数。当我运行它时,它会打印出 The number has (a number) prime numbers。但每次我运行它时它通常都会打印错误的数字。我想如果我只是将一些变量切换为另一个变量,那会很好,但我无法弄清楚我需要更改哪些变量。 +edit:我很确定我每次都需要更改theNum 的值,但我不确定我该怎么做。我尝试将x++ 更改为theNum%10,但它说必须增加x。顺便说一句,我正在做theNum%10,因为我需要分别测试theNum 的每个数字。

int choice = 3, theNum, copy, x, y, counter, even, odd, zero;

System.out.print("Please enter a positive number ");
theNum = Integer.parseInt(kb.nextLine());

case 3:
    // using nested for loops print the prime numbers 
    counter = 0;
    for (x = 1; x <= theNum; x++) {
        for (x = 2; x <= theNum; x++) {
            if (theNum % 10 % x == 0) counter++;
        }
    }
    System.out.print("The number has " + counter + " prime numbers.");
    break;

【问题讨论】:

  • 你认为theNum%10%x == 0 是一个质数吗?例如是 10 还是 20 素数?
  • 提示 唯一的一位素数是:2、3、5和7。
  • 同样theNum的值也没有变化
  • 我怎样才能改变theNum?
  • 这个链接可以帮助你beginnersbook.com/2014/01/java-program-to-display-prime-numbers>。仅在 if(counter==2) 语句中添加您的 count_prime_number 变量并根据您的要求更改循环值。

标签: java logic primes


【解决方案1】:

class TestClass {
    public static void main(String args[] ) throws Exception {

        Scanner kb = new Scanner(System.in);

        int theNum,counter=0,remainder;      

        System.out.print("Please enter a positive number ");
        theNum = Integer.parseInt(kb.nextLine());  

        while(theNum>0) {
            remainder = theNum%10;

            if(isPrime(remainder))
                counter++;

            theNum = theNum/10;
        }      
        System.out.print("The number has " + counter + " prime numbers.");
    }

    static boolean isPrime(int n) 
    { 
        if (n <= 1) return false; 

        for (int i = 2; i < n; i++) 
            if (n % i == 0) 
                return false; 

        return true; 
    }
}

【讨论】:

    【解决方案2】:

    你能做一些简单的事情吗,比如预先定义已知的素数(因为很少):

    import java.util.*;
    
    class Example {
    
        static Set<Integer> primeDigits = new HashSet<>(Arrays.asList(2, 3, 5, 7));
    
        public static void main(String args[] ) throws Exception
        {
            Scanner kb = new Scanner(System.in);
    
            System.out.print("Please enter a positive number: ");
    
            int theNum = Integer.parseInt(kb.nextLine());
    
            int counter = 0;
    
            while (theNum > 0) {
                if (primeDigits.contains(theNum % 10))
                {
                    counter++;
                }
    
                theNum /= 10;
            }
    
            System.out.println("The number has " + counter + " prime digits.");
        }
    }
    

    用法

    % java Example
    Please enter a positive number: 2147483647
    The number has 4 prime digits.
    % 
    

    【讨论】:

      猜你喜欢
      • 2014-07-06
      • 1970-01-01
      • 1970-01-01
      • 2016-02-20
      • 2019-02-16
      • 2017-12-01
      • 2020-04-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多