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

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

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

问题

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

  1. package com.FactorsProgram;
  2. import java.util.Scanner;
  3. public class Main {
  4. public static void main(String[] args) {
  5. int N;
  6. Scanner scanner;
  7. scanner = new Scanner(System.in);
  8. System.out.println("请输入一个数字:");
  9. N = scanner.nextInt();
  10. while (N != 0) {
  11. printFactors(N);
  12. System.out.println("请输入一个数字:");
  13. N = scanner.nextInt();
  14. }
  15. }
  16. public static void printFactors(int N) {
  17. int count = 0;
  18. System.out.println("数字 " + N + " 的因数有:");
  19. for (int i = 2; i <= N / 2; i++) {
  20. if (N % i == 0) {
  21. System.out.print(i + " ");
  22. count++;
  23. }
  24. }
  25. System.out.println();
  26. if (count == 0) {
  27. System.out.println("数字 " + N + " 没有因数。");
  28. } else {
  29. System.out.println("数字 " + N + " 有 " + count + " 个因数。");
  30. }
  31. }
  32. }

注意:为了实现在输入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.

  1. Enter an Number
  2. 12
  3. The factors are
  4. 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.

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

Here is the code.

  1. package com.FactorsProgram;
  2. import jdk.swing.interop.SwingInterOpUtils;
  3. import java.sql.SQLOutput;
  4. import java.util.Scanner;
  5. //Java Program to print all factors of a number using function
  6. public class Main {
  7. public static void main(String[] args) {
  8. int N;
  9. Scanner scanner;
  10. scanner = new Scanner(System.in);
  11. System.out.println(&quot;Enter an Number&quot;);
  12. N = scanner.nextInt();
  13. // Calling printFactors method to print all
  14. // factors of N
  15. printFactors(N);
  16. }
  17. //This method prints all factors of N
  18. public static void printFactors(int N) {
  19. int i;
  20. //Check for every number between 1 to N, whether it divides N. If K
  21. //divides N, it means K is a factor of N
  22. System.out.println(&quot;factors for the number &quot; );
  23. for (i = 1; i &lt;= N; i++) {
  24. if (N % i == 0) {
  25. System.out.print(i + &quot; &quot;);
  26. }
  27. }
  28. }
  29. }

答案1

得分: 0

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

  1. System.out.println("输入一个数字");
  2. n = scanner.nextInt();
  3. while (n != 0) {
  4. // 调用 printFactors 方法打印 N 的所有因数
  5. printFactors(n);
  6. System.out.println("输入一个数字");
  7. n = scanner.nextInt();
  8. }
英文:

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

  1. System.out.println(&quot;Enter an Number&quot;);
  2. n = scanner.nextInt();
  3. while (n != 0) {
  4. // Calling printFactors method to print all
  5. // factors of N
  6. printFactors(n);
  7. System.out.println(&quot;Enter an Number&quot;);
  8. n = scanner.nextInt();
  9. }

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:

确定