sequence.txt

来自「java的一经典教程」· 文本 代码 · 共 40 行

TXT
40
字号
// Program to generate the Syracuse sequence for a
// number entered by the user

import javax.swing.JOptionPane;

public class Sequence
{

public static void syracuse()
{
	int length = 0;	// Initially no numbers generated

	String str = JOptionPane.showInputDialog(
	   "Enter starting number?");

  	int num = Integer.parseInt(str);

  	while ((num != 1) && (length < 10))    // Stop if sequence finishes or
	{                                      // reaches ten terms
   		if (num % 2 == 0) // num is even
 	  		num = num / 2;
 		else // num is odd
 	  		num = 3 * num + 1;

		System.out.println("Next number is " + num);

		length++;
	}
}



   public static void main(String[] args)
   {
	    syracuse();

        System.exit(0);
   }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?