【问题标题】:Why is my code not running nor prompting me for input?为什么我的代码没有运行也没有提示我输入?
【发布时间】:2023-03-04 20:10:01
【问题描述】:

我想创建一个 Java 代码,当接收到任何大小的数组时,它会返回同一个数组的一个版本,其中删除了数组内的所有 7。其余元素应根据需要向数组开头左移,数组末尾的空格应为 0。 我正在尝试为数组的输入创建一种方法,如果需要,可以在其中扩展数组大小;我用于输入的代码是这样的:

static Scanner in = new Scanner(System.in);
public static int[] userInput() {

        int[] inputs = new int[5];
        int currentSize = 0;
        while (in.hasNextInt())
        { 
           if (currentSize > inputs.length)
           {
              inputs = Arrays.copyOf(inputs, 2 * inputs.length);
           }
          
           inputs[currentSize] = in.nextInt();
           currentSize++;
        }
        inputs = Arrays.copyOf(inputs, currentSize);
        return inputs;
    }

但是;当我运行代码时,什么也没有发生,也没有提示我输入。谁能帮帮我?

【问题讨论】:

  • 糟糕的标题。重写以总结您的具体技术问题。

标签: java arrays methods


【解决方案1】:

您的代码运行成功。您是否编写了执行该静态方法的代码?

这是您的代码稍作修改的版本。

我将 Scanner 移动到 try-with-resources 以自动关闭它。养成关闭您打开的此类资源的习惯。任何实现 AutoCloseable 的资源都可以在 try-with-resources 中使用。

为了清楚起见,我更改了一些名称。

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        System.out.println( "running" ) ;
        int[] inputs = Ideone.userInput() ;
        System.out.println( Arrays.toString( inputs ) ) ;
    }

public static int[] userInput() {
try(
   Scanner scanner = new Scanner( System.in ) ;
 ) {
        int[] inputs = new int[5];
        int currentSize = 0;
        while ( scanner.hasNextInt())
        { 
           if (currentSize > inputs.length)
           {
              inputs = Arrays.copyOf(inputs, 2 * inputs.length);
           }
           inputs[currentSize] = scanner.nextInt();
           currentSize++;
        }
        inputs = Arrays.copyOf(inputs, currentSize);
        return inputs;
    }
}
}

看到这个code run live at IdeOne.com

对于这些输入:

7
42
Exit

…我们得到这个输出:

running
[7, 42]

ArrayList

如果使用List(例如ArrayList)而不是数组,您的代码会简单得多。 ArrayList 在内部处理大小调整,我们无需付出任何努力。

如果您正在学习操作数组,那就太好了。或者,如果您有大量数据,请使用数组,因为它们在使用更少内存的同时执行速度更快。但对于日常编程,请使用 Collections 框架,例如 List


/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        System.out.println( "running" ) ;
        List< Integer > inputs = Ideone.userInput() ;
        System.out.println( inputs ) ;
    }


public static List< Integer > userInput() {
try(
   Scanner scanner = new Scanner( System.in ) ;
 ) {
        List< Integer > inputs = new ArrayList<>() ;
        while ( scanner.hasNextInt())
        { 
           inputs.add( scanner.nextInt() ) ;
        }
        return List.copyOf( inputs ) ;
    }
}

}

看到这个code run live at IdeOne.com

要删除任何值 7,请致电 removeAll

boolean oneOrMoreRemoved = inputs.removeAll( List.of( Integer.valueOf( 7 ) ) ) ;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-30
    • 2021-10-17
    • 2022-01-23
    • 2019-04-23
    • 2020-10-28
    • 2021-07-03
    • 1970-01-01
    相关资源
    最近更新 更多