在Java中,如何打印除数数字?

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

In Java, how to print divisor numbers?

问题

我想打印出用户输入的介于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("请插入一个介于1到10000之间的数字:");
            Range = scan.nextInt();

            if (Range < 1 || Range > 10000)
                System.out.println("选择错误");
            else
                break;
        }

        Divisor = 0;    // 从零开始计算约数

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

        System.out.println(Range + "的总约数数量为:" + Divisor);

    }
}

> 我在这里遇到了问题,System.out.println("loop); 这个命令。我想要打印出所有的约数,就像用户插入了10,输出应该显示类似于:

请插入一个介于1到10000之间的数字:10
1
2
5
10
10的总约数数量为:4

> 但是当前的输出是:

请插入一个介于1到10000之间的数字:10
1
2
3
4
5
6
7
8
9
10
10的总约数数量为:4

> 那么如何只在**(Range % loop == 0)为真时打印loop**呢?

英文:

I want to print the divisors of a user input value between 1 and 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(&quot;Please insert a number between 1 and 10000: &quot;);
			Range = scan.nextInt();
			
			if (Range &lt; 1 || Range &gt; 10000)
			System.out.println(&quot;Wrong choice&quot;);
			
			else
				break;
		}
		
		
		Divisor = 0;	// Start counting Divisor from Zero
		
		for (int loop = 1; loop &lt;= Range; loop++) {
			if (Range % loop == 0)
				Divisor++;
				System.out.println(&quot;loop);		
			}
			
		System.out.println(&quot;Total number of divisors of &quot; + Range + &quot; is &quot; + 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:

!

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:

!

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??

答案1

得分: 2

你错过了 {}

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

}

英文:

You missed the {}

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

答案2

得分: 1

以下是翻译好的内容:

只需在 if 条件中添加括号,代码可以如下所示:

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

just add brackets to if condition the code can be as follows

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

答案3

得分: 1

你在循环中漏掉了{}

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

当你在写if条件时,如果之后只有一行代码,它将被执行:(没有{}

if (Range % loop == 0) 
    System.out.println("loop"); //可以运行!
    Divisor++; /// 不能运行 !!

但是加上{}时,这两行都会被执行:

if (Range % loop == 0) {
    System.out.println("loop"); //可以运行!
    Divisor++; /// 可以运行 !!
}
英文:

You're missed the {} in the loop :

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

}

When you write if condition and you have one line of code after it will be executed : (Witout {} )

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

But with {} the two line executed :

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

答案4

得分: 1

任何条件语句在不包含花括号时,将执行接下来的一行代码。如果您想在 if 代码块内执行多于一条语句,请将这些语句包含在花括号内。

示例:

if (条件)
{
    语句1;
    语句2;
}
英文:

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.
Example:

    If(Condition)
{
Statement1;
Statement2;
}

huangapple
  • 本文由 发表于 2020年5月30日 21:58:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/62103485.html
匿名

发表评论

匿名网友

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

确定