请解释这个循环-Java

时间:2017-04-15 02:52:54

标签: java

所以这里的代码是取一个整数并将其每个数字相乘,例如,如果我放入4321它将4*3*2*1并且出现为24。代码可以工作,一切,我的问题是,有人可以解释这个循环如何对我有用。因为我基本上使用了一个骨架来制作这段代码,但有人可以告诉我这个模数如何与*=/=一起工作吗?

import java.util.Scanner;

public class Multiplier {
    public static void main(String[] args) {
        int num, product;
        Scanner scan = new Scanner(System.in);

        System.out.println("Enter an Integer to be multiplied: ");
        num = scan.nextInt();
        product = 1;

        while (num > 0)
        {
         product *= (num%10);
         num/=10;
        }
         System.out.println("The Product of the Digits is: " +product);
    }

}

6 个答案:

答案 0 :(得分:1)

在我们进入循环之前,您应该了解一些事项:

  1. 运算符*=将值设置为值乘以指定的数字。 *=

    的简写符号
    number = number * number;
    

    例如,

    int value = 1;
    value *= 3;
    System.out.println(value);
    

    将输出3,因为速记符号与

    相同
    int value = 1;
    value = value * 3;
    System.out.println(value);
    
  2. 运算符/=将值设置为值除以指定的数值。 /=

    的简写符号
    number = number / number;
    

    重申一下,上面的行与

    相同
    number /= number;
    
  3. 模数运算符%将值除以指定数值后的余数。

    例如,

    System.out.println(4 % 3);
    

    输出1,因为4 % 3 == 1 R 1(第一个意味着我们可以将数字分成一次,R代表余数,最后一个是实际结果为4%3

  4. 考虑以下情况...

    如果你愿意,你想看看你剩下多少钱:

    • 支付十个不同的人100美元
    • 只需1,004美元

    在这种情况下,支付所有10个人后,剩余部分将是4美元。此方案与

    的概念相同
    int remainder = 1004 % 100;
    System.out.println(remainder);
    

    我们知道100次将进入1000次,而余数的最终值将是4,这将打印到控制台。

    好。既然您已经了解了这些操作数的作用,请查看while循环。

    // while our input number is greater than 0
    
    while( num > 0 ) { 
    
    
    // multiply the product by the remainder of number divided by ten
    // (this cuts off the right-most digit of the input number
    // and multiplies it to the product)
    
    product *= (num%10); 
    
    
    // what actually sets our input number to number / 10.
    
    num /= 10;
    

    此循环一直持续到所有数字都被“切断”并乘以我们的产品

答案 1 :(得分:0)

你基本上想知道%运算符的含义以及while循环的作用。

当整数除以该符号后面的数字时,

%(模数运算符)将给出余数。在这种情况下它是10.

以整数为例。让我们说我拿6543.这段代码会做什么?

当循环开始时,6543除以10时的余数为3.所以乘积=乘积* 3。因此,product = 3.现在,6543除以10.由于num是整数,6543/10 = 654。

现在循环将再次开始此号码。剩余时间为4,num将变为65.产品变为12。

它将以同样的方式前进。当num变为0时,循环将停止。

答案 2 :(得分:0)

  • %“从隐含的部门产生其余操作数”(Remainder Operator
  • *=会将变量更新为两个操作数的产品
  • /=会将值更新为两个操作数的商

*=/=都称为Compound Assignment Operators。这是Java关于它们如何工作的定义

  

首先,评估左侧操作数以生成变量。该   保存左侧操作数的值,然后保存右侧   操作数被评估。保存的左手变量值和   右侧操作数的值用于执行二进制   复合赋值运算符表示的运算。的结果   二进制操作转换为左手的类型   变量

以下是更清楚地分解的步骤

class Operators 
{
    static int product = 1;
    static int num = 25;

    public static void main(String[] args) 
    {
        System.out.println("------ [BEFORE MANIPULATION] ------");
        System.out.println("product: " + product);
        System.out.println("num: " + num);
        System.out.println("-----------------------------------");

        int remainder = num % 10;   // remainder is equal to 5 (25 / 10 = 2.5)
        product *= remainder;       // product is equal to 5 (5 * 1)
        num /= 10;                  // num is equal to 2 (25 / 10 = 2.5)

        System.out.println("------ [AFTER MANIPULATION] ------");
        System.out.println("remainder: " + remainder);
        System.out.println("product: " + product);
        System.out.println("num: " + num);
        System.out.println("-----------------------------------");

    }
}

答案 3 :(得分:0)

模数通过将num除以%之后的任何值来工作,然后结果是余数。例如,%(mod)b是a / b的余数。

所以在你的情况下: product = product *(num%10)。

产品是1次,1234%10,或1234/10的剩余部分是4。

num / = 10是num = num / 10的简写,* =,+ =, - = ETC ...

那么它将1234除以10,它是一个整数,它将123.4截断为123并循环

答案 4 :(得分:0)

import java.util.Scanner;

public class Multiply {
  public static void main(String[] args) {
      int num, product;
      Scanner scan = new Scanner(System.in);

      System.out.println("Enter an Integer to be multiplied: ");
      num = scan.nextInt();
      product = 1;

      while (num > 0) {
        // The notation variable += 2 is a common short hand for operations where it says that variable = variable + 2;

        // The below line could also be written product = product * (num % 10);
        product *= (num % 10);
        // The way modulus (the % operator) works is it returns the remainder of division
        // For example, on the first run of this program the code above would go like so:
        // product = 1 * (4321 % 10)
        // product = 1 * (1)
        // We get a one as the remainder because we are working with integers so when we divide 4321 by
        // 10 we get 432 with a remainder of 1. The next loop will result in 432 % 10 which will give you a remainder of 2. So on and so fourth. 

        // The below line could also be written num = num / 10;
        num /= 10;
      }
      System.out.println("The Product of the Digits is: " +product);
  }
}

答案 5 :(得分:0)

product *= (num % 10); 

相当于:

product = product * (num % 10);

num /= 10;num = num/10;

所以在这里, num%10 有助于从右到左依次获取数字,并且 num / 10 有助于将数字设置为现值的十分之一,直到数字变为0。

相关问题