我正在处理一项任务中的问题。它被称为3n + 1问题。任务是从用户(不是1或负数)获得一个整数,并根据输入数(n),如果是偶数 - 除以2,如果是负数 - 多个n * 3 + 1。
我必须使用的方法如下:
public static ArrayList<Integer> getHailstoneSequence(int n) {
^这部分对我的作业是强制性的,所以我必须使用整数的ArrayList。
我正努力让我的计划有效。我无法弄清楚是否应该将输入存储在main方法或我的定义类中。我也不确定如何在main方法中为偶数执行循环执行,而没有在定义类(我的getHailstoneSequence()方法所在的位置已经说明的冗余)。
以下是我的代码:(DEFINITION CLASS)
package question7;
import java.util.ArrayList;
public class HailstoneSequence {
// Method for computing the Hailstone Sequence:
public static ArrayList<Integer> getHailstoneSequence(int n) {
ArrayList<Integer> hailstoneSequence = new ArrayList<Integer>();
if (n % 2 == 0) {
n = n / 2;
hailstoneSequence.add(n);
}
else {
n = (n * 3) + 1;
hailstoneSequence.add(n);
}
return hailstoneSequence;
}
}
我不确定如何将上面创建的方法包含在主打印方法中。我希望输出看起来像这样(例子):
5是奇数,所以我们做3N + 1:16 16是偶数,所以我们采取一半:8 8是偶数,所以我们采取一半:4 4是偶数,所以我们采取一半:2 2是偶数,所以我们采取一半:1
让程序停止时n = 1
以下是我在主要方法中所拥有的内容:
package question7;
import java.util.ArrayList;
import java.util.Scanner;
public class HailstoneSequenceTest {
public static void main(String[] args) {
Scanner hailstone = new Scanner(System.in);
System.out.println("To begin, please enter a positive integer that is not 1:");
int n = hailstone.nextInt();
ArrayList<Integer> list = HailstoneSequence.getHailstoneSequence(n);
for (int sequence : list) {
try {
if (n > 1) {
System.out.println("Great choice! Let's begin!");
System.out.println();
while (n % 2 == 0) {
System.out.println(n +
" is even, so we take half: " +
HailstoneSequence.getHailstoneSequence(n));
list.add(n);
if (n == 1) break;
while (n % 2 != 0) {
System.out.println(n +
" is odd, so I make 3n+1: " +
HailstoneSequence.getHailstoneSequence(n));
list.add(n);
if (n == 1) break;
}
// while (n == 1) {
// System.out.println(sequence);
// break;
}
}
}
catch (Exception error) {
while (n <= 1) {
System.out
.println("You did not enter a valid positive, greater than 1 integer. Please try again: ");
System.out.println();
n = hailstone.nextInt();
}
// End of HailstoneSequenceTest class
hailstone.close();
}
}
}
}
// }
有谁知道我哪里出错了?我知道如果有多种方式我的代码可能是错的,我只是不确定从哪里开始。
我是否需要一个for循环来保存特征并增加等等?
当我尝试按照我所知的方式执行此操作时,它说我必须返回一个ArrayList而不是Int。
请指教。
答案 0 :(得分:2)
可以在这里找到一个可能的答案:
您可以简单地调整此代码以使用arraylist而不是仅仅将其分配给变量
这样的事情:
public static ArrayList<Integer> getHailstoneSequence(int n) {
ArrayList<Integer> result = new ArrayList<Integer>();
while(n !=1)
{
result.add(number);
if(n % 2 == 0) //even
{
n = n/2;
}
else //odd
{
n= n*3 + 1;
}
}
return result;
}
答案 1 :(得分:1)
此方法应返回整个序列。
序列以用户输入的数字开头。所以这应该是返回列表中的第一个元素:
list.add(n);
当数字变为1时结束。因此,当n
变为1时,你应该有一个循环结束:
while (n != 1) {
...
}
应通过计算前一个序列值并将其添加到列表中来获取内部元素:
n = computeNextSequenceValue(n);
list.add(n);
我让你组装拼图的各个部分。毕竟这是一项任务。
答案 2 :(得分:1)
我去了,在这里你在main方法中检查输入的前提条件。然后递归调用您的规则,直到达到结束条件。
public class MakeItRain {
public static void main(String[] args) {
System.out.println("To begin, please enter a positive integer that is not 1:");
Scanner userInput = new Scanner(System.in);
int n = userInput.nextInt();
if (n <= 0) {
throw new IllegalArgumentException("I told you to enter a positive number! Wtf is " + n);
}
if (n == 1) {
throw new IllegalArgumentException("I told you to not enter 1! Come on man!");
}
List<Integer> hailstones = HailstoneSequence.getHailstoneSequence(n);
System.out.println(hailstones);
}
private static class HailstoneSequence {
public static List<Integer> getHailstoneSequence(int n) {
List<Integer> sequence = new ArrayList<>();
if (recurse(sequence, n)) {
return sequence;
}
return sequence;
}
private static boolean recurse(List<Integer> sequence, int input) {
int currentHailstone = getNewHailstone(input);
sequence.add(currentHailstone);
if (sequenceComplete(currentHailstone)) {
return true;
}
return recurse(sequence, currentHailstone);
}
private static int getNewHailstone(int hailstone) {
if (isEven(hailstone)) {
hailstone /= 2;
} else {
hailstone = (hailstone * 3) + 1;
}
return hailstone;
}
private static boolean isEven(int n) {
return n % 2 == 0;
}
private static boolean sequenceComplete(int rollingResult) {
return rollingResult == 1;
}
}
}
66结果:
错误结果:
答案 3 :(得分:0)
我认为你想要的......
import java.util.ArrayList;
import java.util.List;
public class HailstoneSequence
{
public static ArrayList<Integer> getHailstoneSequence( ArrayList<Integer> hailstoneSequence,int n)
{
if(n==1)
return hailstoneSequence;
if (n % 2 == 0)
{
n = n / 2;
hailstoneSequence.add(n);
getHailstoneSequence(hailstoneSequence,n);
}
else
{
n = (n * 3) + 1;
hailstoneSequence.add(n);
getHailstoneSequence(hailstoneSequence, n);
}
return hailstoneSequence;
}
public static void main(String ar[])
{
ArrayList<Integer> hailstoneSequence = new ArrayList<Integer>();
int n=5;
List<Integer> list=getHailstoneSequence(hailstoneSequence,n);
for(Integer i:list)
{
System.out.println(i);
}
}
}
答案 4 :(得分:0)
这里是我修改过的代码及其工作原理
public static ArrayList<Integer> getHailstoneSequence(int n) {
ArrayList<Integer> hailstoneSequence = new ArrayList<Integer>();
while(true)
{
if(n==1)break;
if (n % 2 == 0) { //if the remainder of n/2 is 0, then the number is even
hailstoneSequence.add(n);
n = n / 2;
}
else {
hailstoneSequence.add(n);
n = (n * 3) + 1;
}
}
return hailstoneSequence;
}
public static void main(String[] args) {
Scanner hailstone = new Scanner(System.in);//ask user for initial number input
System.out.println("To begin, please enter a positive integer that is not 1:");
int n = hailstone.nextInt();
ArrayList<Integer> list = AS.getHailstoneSequence(n);
int i=0;
// loop through all the numbers
for (int sequence : list) {
try
{
if(sequence==1)break;
if(sequence%2==0)
{
System.out.println(sequence + " is even, so I take half: " + (sequence/2));
}
else
System.out.println(sequence+ " is odd, so I make 3n+1: " + ((3*sequence)+1));
i++;
}
catch (Exception error) {
while (n <= 1) {
System.out
.println("You did not enter a valid positive, greater than 1 integer. Please try again: ");
System.out.println();
n = hailstone.nextInt();
}
}
和它的输出是,
To begin, please enter a positive integer that is not 1:
12
12 is even, so I take half: 6
6 is even, so I take half: 3
3 is odd, so I make 3n+1: 10
10 is even, so I take half: 5
5 is odd, so I make 3n+1: 16
16 is even, so I take half: 8
8 is even, so I take half: 4
4 is even, so I take half: 2
2 is even, so I take half: 1
The process took 9 to reach 1.