gpt4 book ai didi

java - 从键盘读取数据并存储在字符串数组中

转载 作者:行者123 更新时间:2023-11-30 03:59:14 25 4
gpt4 key购买 nike

String s[];
System.out.println("Enter loop value");
int t = s.nextInt();
for(int i=0;i<t;i++)
{
str[i]=s.nextLine();
}

读取时给出空指针异常

最佳答案

你需要的是这样的:

    Scanner s = new Scanner(System.in);

System.out.println("Enter loop value");
int t = s.nextInt(); // read number of element
s.nextLine(); // consume new line
String str[] = new String[t];
for(int i=0;i<t;i++)
{
str[i]=s.nextLine();
}

System.out.println(Arrays.toString(str));

希望这有帮助。

关于java - 从键盘读取数据并存储在字符串数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22346306/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com