【问题标题】:Using standard input in java, how to input an entire array of integers? [duplicate]在java中使用标准输入,如何输入整个整数数组? [复制]
【发布时间】:2015-02-14 05:50:27
【问题描述】:

所以到目前为止我的代码看起来像这样:

public class PancakeSort {
   public static int flip(int n) {
      int temp = 0;
      for (int i = 0; i < (n+1) / 2; ++i) {
         int[] pancakes = new int[n];
         temp = pancakes[i];
         pancakes[i] = pancakes[n-i];
         pancakes[n-i] = temp;
      }
      return temp;
   }

   public static void sort (int[] pancakes) {
      for (int i=0; i<pancakes.length; i++){
         if (pancakes[i] > pancakes[i+1]){
            flip(i+1);
         }
      }
      System.out.println(pancakes);
   }
   public static void main(String[] args) {    

   }
}

但是我如何使用标准输入 (StdIn.readLine()) 输入整个整数数组?我知道代码可能不正确,我正在努力解决这个问题,而且我也知道这个问题之前已经在这个网站上提出过,但没有专门使用标准库,这就是我所在的地方卡住了。

【问题讨论】:

  • 是的,我都检查过了。但是他们都没有使用标准输入(这是不同的方式)。还有没有。数组中的元素都取决于输入。
  • StdIn 不是核心 Java 提供的任何东西。 Java 提供System.in 用于从标准输入读取,您将看到在上面引用的问题中使用了它。如果您不想阅读 System.in 的内容,请解释您认为“使用标准库”的含义。

标签: java


【解决方案1】:

您可以发送整数数组作为输入

PancakeSort pancakeSort = new PancakeSort();
pancakeSort.sort(new int[] { 100, 50, 89, 2, 5, 150 });

或将扫描仪类用作

int arr[] = new int[10];
Scanner sc = new Scanner(System.in);
int i = 0;
while (sc.hasNextInt()) {
    arr[i] = sc.nextInt();
    i = i + 1;
}

PancakeSort pancakeSort = new PancakeSort();
pancakeSort.sort(arr);

但在最后一种情况下,你不能增加数组的大小。否则它会给出arrayIndexOutOfBoundException

【讨论】:

    【解决方案2】:

    我相信您可能引用了 StdIn,例如像这样的类?

    http://introcs.cs.princeton.edu/java/stdlib/StdIn.java.html

    如果是这样,那么要从控制台获取 int,您只需调用 StdIn.readInt。您可以如何处理的一个示例是:

    public static void main(String[] args) 
    { 
            System.out.println("Enter number of pancakes, or enter 0 to quit");
    
            int[] pancakeArray = new int[0];
    
            while (true)
            {
                try
                {
                    int entry = StdIn.readInt();
                    if (entry == 0)
                    {
                        break;
                    }
    
                    int[] expandedArray = new int[pancakeArray.length + 1];
                    System.arraycopy(pancakeArray, 0, expandedArray, 0, pancakeArray.length);
    
                    expandedArray[pancakeArray.length] = entry;
                    pancakeArray = expandedArray;
                }
                catch (Exception e)
                {
                    System.out.println("Invalid entry detected, closing input");
                    break;
                }
            }
    
            System.out.println("Pancake array length: " + pancakeArray.length);
            sort(pancakeArray);
    
            System.out.println("Final pancake array in order:");
            for (int entry : pancakeArray)
            {
                System.out.println("Pancake value: " + entry);
            }
    }       
    

    这将在 int 之后读取 int ,直到输入 0 或无效值,然后它会从那里调用您的排序例程。你的排序例程中存在问题,但你说你想看看那个,所以我会让你弄清楚那部分。

    【讨论】:

      猜你喜欢
      • 2014-08-22
      • 2018-04-28
      • 2011-01-31
      • 2019-07-26
      • 2011-04-14
      • 1970-01-01
      • 1970-01-01
      • 2012-01-01
      相关资源
      最近更新 更多