如何在因数程序中排除数字?以及如何在输入特定数字之前重新提示用户?

huangapple go评论63阅读模式
英文:

How to exclude numbers in factor program ? AND How to re prompt user until a certain number is entered?

问题

这是您提供的代码的翻译部分:

package com.FactorsProgram;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        int N;
        Scanner scanner;
        scanner = new Scanner(System.in);

        System.out.println("请输入一个数字:");
        N = scanner.nextInt();

        while (N != 0) {
            printFactors(N);
            System.out.println("请输入一个数字:");
            N = scanner.nextInt();
        }
    }

    public static void printFactors(int N) {
        int count = 0;

        System.out.println("数字 " + N + " 的因数有:");
        for (int i = 2; i <= N / 2; i++) {
            if (N % i == 0) {
                System.out.print(i + " ");
                count++;
            }
        }

        System.out.println();
        if (count == 0) {
            System.out.println("数字 " + N + " 没有因数。");
        } else {
            System.out.println("数字 " + N + " 有 " + count + " 个因数。");
        }
    }
}

注意:为了实现在输入0之前持续循环接受输入,我添加了一个 while 循环。同时,我也进行了必要的调整,以便程序能够输出您所期望的格式。

英文:

So I am doing this project. And I need to

  • Write a program that finds the factors and number of factors for an integer.
  • Factors will be listed on 1 line for smallest to largest with a space separating each.
  • Continuously accept input until a 0 is entered, which is the sentinel. Do not factor 0.

I got the factoring part. This is the output I am currently getting.

Enter an Number
12
The factors are 
1 2 3 4 6 12 

It stops immediately after giving the factors. I am not sure on how to implement it to re prompt. I have tried loops but its not working. Also,How can I exclude 1 and the entered number.

This is how my output should look. It should stop once 0 is entered.

Enter a number: 12
There are 4 factors for the number 12: 2 3 4 6
Enter a number: 25
There are 1 factors for the number 25: 5
Enter a number: 100
There are 7 factors for the number 100: 2 4 5 10 20 25 50
Enter a number: 13
There are 0 factors for the number 13:
Enter a number: 0

Here is the code.

package com.FactorsProgram;
import jdk.swing.interop.SwingInterOpUtils;
import java.sql.SQLOutput;
import java.util.Scanner;


 //Java Program to print all factors of a number using function

public class Main {
    public static void main(String[] args) {
        int N;
        Scanner scanner;
        scanner = new Scanner(System.in);

        System.out.println(&quot;Enter an Number&quot;);
        N = scanner.nextInt();

        // Calling printFactors method to print all
        // factors of N
        printFactors(N);
    }


     //This method prints all factors of N
    public static void printFactors(int N) {
        int i;

         //Check for every number between 1 to N, whether it divides N. If K
         //divides N, it means K is a factor of N
        
        System.out.println(&quot;factors for the number &quot; );
        for (i = 1; i &lt;= N; i++) {
            
            if (N % i == 0) {

                System.out.print(i + &quot; &quot;);
            }
        }
    }
}

答案1

得分: 0

你关于使用循环的想法是正确的 - 你需要循环并检查 n 是否不等于 0。例如:

System.out.println("输入一个数字");
n = scanner.nextInt();
while (n != 0) {
    // 调用 printFactors 方法打印 N 的所有因数
    printFactors(n);

    System.out.println("输入一个数字");
    n = scanner.nextInt();
}
英文:

You had the right idea with having a loop - you need to loop and check that n isn't 0. E.g.:

System.out.println(&quot;Enter an Number&quot;);
n = scanner.nextInt();
while (n != 0) {
    // Calling printFactors method to print all
    // factors of N
    printFactors(n);

    System.out.println(&quot;Enter an Number&quot;);
    n = scanner.nextInt();
}

huangapple
  • 本文由 发表于 2020年9月26日 01:03:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/64068465.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定