2017-04-15 77 views
0

所以这里的代码是取一个整数并且将它的每个数字相乘,例如,如果我输入4321它会做4*3*2*1并且出来是24。代码工作和一切,我的问题是,有人可以解释这个循环是如何工作给我的。因为我基本上使用了一个骷髅来制作这段代码,但是有人能够引导我通过这种模数如何与*=/=一起工作吗?请解释这个循环请-Java

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); 
    } 

} 
+1

不模数,余数。 'product'乘以'num/10'的余数*,然后'num'设置为其当前值的十分之一*。 –

回答

0

您基本上想知道%运算符的含义以及while循环做什么。

%(模运算符)会在整数除以该符号后面的数字时给出余数。在这种情况下,它是10.

举一个整数的例子。可以说我拿6543.这段代码会做什么?

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

现在循环将再次启动此号码。剩余时间将为4,数字将变为65.产品变为12.

它将采用相同的方式。当num变为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

想想下面的情况......

你想看看你会多少钱已经离开,如果你:

  • 收费十个不同的人$ 100
  • 只有1004 $

在这种情况下,其余的将是$ 4支付全部10人后。这种情况是相同的概念

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; 

这个循环继续,直到所有的数字都被“切断”,再乘以我们的产品

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("-----------------------------------"); 

    } 
} 
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和循环

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); 
    } 
} 
1
product *= (num % 10); 

等同于:

product = product * (num % 10); 

num /= 10;num = num/10;

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