简体   繁体   English

在 Java 中,如何打印除数?

[英]In Java, how to print divisor numbers?

I want to print the divisors of a user input value between 1 and 10000.我想打印用户输入值在 1 到 10000 之间的除数。

import java.util.Scanner;

public class Divisors {

    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);
        int Range;
        int Divisor;

        while(true) {

            System.out.print("Please insert a number between 1 and 10000: ");
            Range = scan.nextInt();

            if (Range < 1 || Range > 10000)
            System.out.println("Wrong choice");

            else
                break;
        }


        Divisor = 0;    // Start counting Divisor from Zero

        for (int loop = 1; loop <= Range; loop++) {
            if (Range % loop == 0)
                Divisor++;
                System.out.println("loop);      
            }

        System.out.println("Total number of divisors of " + Range + " is " + Divisor);

    }
}

I have problem here with command System.out.println("loop); . I want to print all the divisors, like if a user inserted 10, then the output should show something like:我在这里遇到命令System.out.println("loop);的问题。我想打印所有除数,比如如果用户插入 10,那么 output 应该显示如下内容:

!

Please insert a number between 1 and 10000: 10
1
2
5
10
Total number of divisors of 10 is 4

! !

But the current output is:但是现在的output是:

!

Please insert a number between 1 and 10000: 10
1
2
3
4
5
6
7
8
9
10
Total number of divisors of 10 is 4

! !

so how to print loop only when the (Range % loop == 0) is true??那么如何仅在(Range % loop == 0)为真时打印循环

You missed the {}您错过了 {}

for (int loop = 1; loop <= Range; loop++) {
    if (Range % loop == 0) {
       System.out.println(loop); 
       Divisor++;
    }     

}

just add brackets to if condition the code can be as follows只需将括号添加到 if 条件代码可以如下

  for (int loop = 1; loop <= Range; loop++) {
            if (Range % loop == 0){
                Divisor++;
                System.out.println("loop); 
               }     
            }

You're missed the {} in the loop:您错过了循环中的{}

for (int loop = 1; loop <= Range; loop++) {
    if (Range % loop == 0) {
       System.out.println("loop); 
       Divisor++;
    }     

}

When you write if condition and you have one line of code after it will be executed: (Witout {} )当您编写if条件并且您将执行它之后有一行代码时:(Witout {}

 if (Range % loop == 0) 
           System.out.println("loop); //WORK !
           Divisor++; /// NOT WORK !!

But with {} the two line executed:但是使用{}执行了两行:

if (Range % loop == 0) {
           System.out.println("loop); //WORK !
           Divisor++; /// WORK !!
        }

Any conditional statement will execute subsequent one line if you do not include curly braces.如果不包含大括号,任何条件语句都将执行后续的一行。 If you want to execute more than one statement inside if block, include those statements inside curly braces.如果要在 if 块中执行多个语句,请将这些语句包含在花括号中。 Example:例子:

    If(Condition)
{
Statement1;
Statement2;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM