Collat​​z序列:哪个数字最大?

时间:2017-02-20 19:02:02

标签: java

所以我为Collat​​z序列编写了代码,但我希望能够识别并打印出序列中出现的最大数字。这是我的代码:

import java.util.Scanner;

public class CollatzSequence {
    public static void main(String[]args) {
        Scanner keyboard = new Scanner(System.in);
        int n,ts = 0;

        System.out.print("This is the Lothar Collatz Sequnce. Please enter the starting number.\n>");
        n = keyboard.nextInt();

        do {
            if (n % 2 == 0) {
                n = n / 2;
                System.out.println(n);
            }
            else {
                n = n*3 + 1;
                System.out.println(n);
            }
        ts++;
        }
        while (n != 1);       
        System.out.println("Terminated after "+ts+" steps.");

        }
    }

1 个答案:

答案 0 :(得分:2)

创建一个名为max的变量。最初将其设置为n。在每个步骤中,检查n > max是否为max,如果是,请将n设置为public function report(Exception $exception) { parent::report($exception); Mail::send('emails.errorreport', ['e' => $exception], function($message) { $message->to('email@email.com')->subject('Report error'); }); }