Java 如何返回ArrayList<;整数>;?

Java 如何返回ArrayList<;整数>;?,java,Java,我正在处理作业中的一个问题。这被称为3n+1问题。任务是从用户处获取一个整数(不是1或负数),并基于输入的数字(n),如果是偶数除以2,如果是负数,则是n*3+1的倍数 我必须使用的方法如下: public static ArrayList<Integer> getHailstoneSequence(int n) { publicstaticarraylistgethailstonesequence(intn){ ^这部分对于我的赋值是必需的,因此我有必要使用整数数组列表 我正在

我正在处理作业中的一个问题。这被称为3n+1问题。任务是从用户处获取一个整数(不是1或负数),并基于输入的数字(n),如果是偶数除以2,如果是负数,则是n*3+1的倍数

我必须使用的方法如下:

public static ArrayList<Integer> getHailstoneSequence(int n) {
publicstaticarraylistgethailstonesequence(intn){
^这部分对于我的赋值是必需的,因此我有必要使用整数数组列表

我正在努力使我的程序正常工作。我不知道我应该将输入存储在主方法还是定义类中。我也不知道如何让循环在主方法中对偶数执行,而不必在定义类(我的getHailstoneSequence()方法所在的位置)中声明它

下面是我的代码:(定义类)

package问题7;
导入java.util.ArrayList;
公共类冰雹序列{
//计算冰雹序列的方法:
公共静态数组列表getHailstoneSequence(int n){
ArrayList hailstoneSequence=新的ArrayList();
如果(n%2==0){
n=n/2;
冰雹序列。添加(n);
}
否则{
n=(n*3)+1;
冰雹序列。添加(n);
}
返回冰雹序列;
}
}
我不确定如何将上面创建的方法包含到用于打印的主方法中。我希望输出如下所示(示例):

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();

            }

        }
    }
}

// }
package问题7;
导入java.util.ArrayList;
导入java.util.Scanner;
公共类冰雹试验{
公共静态void main(字符串[]args){
扫描器冰雹=新扫描器(System.in);
System.out.println(“首先,请输入一个不是1的正整数:”;
int n=冰雹。nextInt();
ArrayList=HailstoneSequence.getHailstoneSequence(n);
for(int序列:列表){
试一试{
如果(n>1){
System.out.println(“很棒的选择!让我们开始吧!”);
System.out.println();
而(n%2==0){
系统输出打印项次(n)+
是偶数,所以我们取一半:+
冰雹序列(n);
列表。添加(n);
如果(n==1)中断;
而(n%2!=0){
系统输出打印项次(n+
“是奇数,所以我做3n+1:”+
冰雹序列(n);
列表。添加(n);
如果(n==1)中断;
}
//而(n==1){
//系统输出打印项次(顺序);
//中断;
}
}
}
捕获(异常错误){

而(n可以在这里找到一个可能的答案:

您可以简单地调整此代码以使用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;
}
publicstaticarraylistgethailstonesequence(intn){
ArrayList结果=新建ArrayList();
而(n!=1)
{
结果。添加(编号);
如果(n%2==0)//偶数
{
n=n/2;
} 
其他//奇怪
{
n=n*3+1;
}
}
返回结果;
}

可以在此处找到可能的答案:

您可以简单地调整此代码以使用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;
}
publicstaticarraylistgethailstonesequence(intn){
ArrayList结果=新建ArrayList();
而(n!=1)
{
结果。添加(编号);
如果(n%2==0)//偶数
{
n=n/2;
} 
其他//奇怪
{
n=n*3+1;
}
}
返回结果;
}

此方法应返回整个序列

序列以用户输入的数字开始。因此,这应该是返回列表中的第一个元素:

list.add(n);
当数字变为1时结束。因此,您应该有一个循环,当
n
变为1时结束:

 while (n != 1) {
     ...
 }
应通过计算上一个序列值的下一个序列值并将其添加到列表中来获得内部元素:

n = computeNextSequenceValue(n);
list.add(n);

我让你组装拼图。这毕竟是一个作业。

此方法应返回整个序列

序列以用户输入的数字开始。因此,这应该是返回列表中的第一个元素:

list.add(n);
当数字变为1时结束。因此,您应该有一个循环,当
n
变为1时结束:

 while (n != 1) {
     ...
 }
应通过计算上一个序列值的下一个序列值并将其添加到列表中来获得内部元素:

n = computeNextSequenceValue(n);
list.add(n);

我让你组装拼图的各个部分。这毕竟是一个作业。

我试了一下,在这里你检查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;
        }
    }
}
public类MakeItRain{
公共静态void main(字符串[]args){
System.out.println(“首先,请输入一个不是1的正整数:”;
扫描仪用户输入=新扫描仪(System.in);
int n=userInput.nextInt();

如果(n我试过了,在这里检查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;
        }
    }
}
public类MakeItRain{
公共静态void main(字符串[]args){
System.out.println(“