我想创建一个算法,该算法可以接受我写的数字,然后进行运算,然后取答案,并重复新的数字,直到该数字等于1
import java.util.Scanner;
public class Nombres {
public static void main(String args[]) {
int amount;
Scanner keyboard = new Scanner(System.in);
System.out.print("Inscrivez un nombre");
amount = keyboard.nextInt();
while (amount != 1) {
if (amount % 2 > 0) {
System.out.println((amount * 3) + 1);
}
if (amount % 2 == 0) {
System.out.println(amount / 2);
}
if (amount == 1) {
System.out.println("FIN");
}
}
if (amount == 1) {
System.out.println("1 FIN");
}
}
}
答案 0 :(得分:1)
您不会在每次迭代中更改amount
。例如
if (amount % 2 == 0)
amount = amount / 2;
else
amount = amount * 3 + 1
System.out.println(amount);