分析Java中的阶乘循环

时间:2015-10-04 02:31:30

标签: java loops for-loop logic

请考虑以下代码:

int number;
double factorial = 1.0;

for(int i=2; i<=5; i++)
{
    factorial *=i;
    System.out.println(number + "! = " + factorial );
}

为什么输出

2.0
6.0
24.0
120.0

我知道它从2点到5点开始。

5 个答案:

答案 0 :(得分:1)

factorial *=i; 

表示factorial = i * factorial;

事实变量范围是循环外的中继,因此它不会在每次循环迭代时重置

所以当i = 2时,时间因子= 2.0

当i = 3时,时间因子值为2.0以上计算,因此因子= 2.0 * 3,即6

当i = 4时,时间因子= 6 * 4 = 24.0

当i = 5时,时间因子= 24 * 5 = 120.0

答案 1 :(得分:0)

                FB.api('/me', function (me) {
                    user.set("facebook_id", me.id);
                    user.set("facebook_link", me.link);
                    user.set("firstName", me.first_name);
                    user.set("lastName", me.last_name);
                    user.setEmail(me.email);
                    user.save().then(function () {
                        //go to new page
                    });
                });

相当于:

stop();
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.display.MovieClip;

var hitArrayClass:Array = new Array(answerTargetQ4, answerTargetQ41, answerTargetQ42, answerTargetQ43, answerTargetQ5, answerTargetQ51, answerTargetQ52, answerTargetQ53, answerTargetQ6, answerTargetQ61, answerTargetQ62, answerTargetQ63, answerTargetQ7, answerTargetQ71, answerTargetQ72,answerTargetQ73);
var dropArrayClass: Array = new Array(answerQ4, answerQ41, answerQ42, answerQ43, answerQ5, answerQ51, answerQ52, answerQ53, answerQ6, answerQ61, answerQ62, answerQ63, answerQ7, answerQ71, answerQ72, answerQ73);
var positionsArrayClass: Array = new Array();


for (var a:int = 0; a < dropArrayClass.length; a++) {
    dropArrayClass[a].buttonMode = true;
    dropArrayClass[a].addEventListener(MouseEvent.MOUSE_DOWN, moveDown);
    dropArrayClass[a].addEventListener(MouseEvent.MOUSE_UP, mUp);

    positionsArrayClass.push({xPos:dropArrayClass[a].x, yPos:dropArrayClass[a].y});

}


function moveDown (e:MouseEvent):void{
    e.currentTarget.startDrag();

    setChildIndex(MovieClip(e.currentTarget), numChildren - 1);

    if (e.currentTarget !=null){
        //e.currentTarget.stopDrag();
    }
}


function mUp(e:MouseEvent):void {
    var dropIndex:int = dropArrayClass.indexOf(e.currentTarget);
    var target:MovieClip = e.currentTarget as MovieClip;

    target.stopDrag();

    if (target.hitTestObject(hitArrayClass[dropIndex])){    
target.x = hitArrayClass[dropIndex].x;
target.y = hitArrayClass[dropIndex].y;

    target.removeEventListener(MouseEvent.MOUSE_DOWN, moveDown);
    target.removeEventListener(MouseEvent.MOUSE_UP, mUp);



} else {
       target.x = positionsArrayClass[dropIndex].xPos;
       target.y = positionsArrayClass[dropIndex].yPos;
   }


  }

我们说factorial *= i; factorial = factorial * i; factorial1.0

微积分将是:

i

等到循环结束。

答案 2 :(得分:0)

我认为您想要使用package project.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import project.MediaFileRepository; import project.MediaFile; import org.springframework.ui.Model; @Controller public class SearchController { @Autowired private MediaFileRepository repository; @RequestMapping(value = "/searchmedia", method = RequestMethod.GET) public String searchForm(Model model) { // This is what I want to get to the JSP file Iterable<MediaFile> mediaFiles = repository.findAll(); // I could also generate the html here and put that into the file for (MediaFile mediaFile: repository.findAll()) { model.addAttribute("mediaFile", mediaFile); } return "search"; } } (而不是<ul> <li>Info from Object 1 from array</li> <li>Info from Object 2 from array</li> . . . <li>Info from Object N from array</li> </ul> 和它,因为您使用long获得了浮点类型。因子通常是整体类型。像,

double

我得到了(预期的)

double

答案 3 :(得分:0)

int number;


for(int i=2; i<=5; i++)
{
  double factorial = 1.0;
  factorial *=i;
  System.out.println(number + "! = " + factorial );
}

上面的代码将给出输出2.0,3.0 ......因为循环中的每个迭代都有一个新变量&#39; factorial&#39;将被创建并初始化为1.0

但是

int number;
double factorial = 1.0;

for(int i=2; i<=5; i++)
{
factorial *=i;
System.out.println(number + "! = " + factorial );
}

表示i = 2

factorial = factorial * i // ie。 1.0 * 2 = 2.0;阶乘的新价值是2.0即。 factorial = 2.0

表示i = 3

factorial = factorial * 3 //即。 2.0 * 3 = 6.0;这是阶乘(当i = 1时)乘以3

表示i = 4

factorial = factorial(当i = 2时)* 4 //即。 6.0 * 4 = 24.0

答案 4 :(得分:-1)

  • 1 * 2 = 2
  • 2 * 3 = 6
  • 6 * 4 = 24
  • 24 * 5 = 120

我猜,输出是正确的

1,2,6,24是在先前迭代期间存储在factorial var中的结果

2,3,4,5用于循环i值