Java 为什么字符串数组中的随机索引返回null?
我想允许用户输入一个数字,它将是字符串的数量(选项)。然后,程序将生成一个介于0和数组长度之间的随机数,并显示与该随机数的idex匹配的元素。然而,每当我运行程序时,输出只会显示“null”。我已经附上了我的代码。任何建议都会有帮助,谢谢Java 为什么字符串数组中的随机索引返回null?,java,arrays,Java,Arrays,我想允许用户输入一个数字,它将是字符串的数量(选项)。然后,程序将生成一个介于0和数组长度之间的随机数,并显示与该随机数的idex匹配的元素。然而,每当我运行程序时,输出只会显示“null”。我已经附上了我的代码。任何建议都会有帮助,谢谢 import java.util.Scanner; import java.util.Arrays; public class Decision{ public static void main (String[] args){ Sc
import java.util.Scanner;
import java.util.Arrays;
public class Decision{
public static void main (String[] args){
Scanner I= new Scanner(System.in);
System.out.println("How many choices would you like to choose from?");
int i=I.nextInt();
System.out.println("Enter the " + i + " choices:");
Scanner C= new Scanner(System.in);
String c= C.nextLine();
String[] options=new String[i];
int random = (int)(Math.random() * i +1);
String choice = options[random];
System.out.println(choice);
}
}
您的代码存在多个问题:
- 您的控制台输出为
,因为创建数组后null
您从不为它分配任何字符串,因此所有元素都保持String[]选项=新字符串[i]
李>null
- 您需要一个循环来使用扫描仪检索多行,
C.nextLine()代码>只读取一行,不使用它李>
- 您不使用
importjava.util.array
- 您不应该创建新的扫描仪,因为您可以将第一个扫描仪用于所有输入目的
请对变量使用有意义的名称,考虑. - 您不使用
- 随机数
对于数组索引不正确Math.random()*i+1)
import java.util.Scanner;
import java.util.Random;
public class Decision {
public static void main (String[] args){
Scanner scanner = new Scanner(System.in);
System.out.println("How many choices would you like to choose from?");
// I use an ArrayList, because it may be hand
int count = scanner.nextInt();
String[] choices = new String[count];
// Necessary, because nextInt() does not consume
// the newline character.
scanner.nextLine();
System.out.println("Enter the " + count + " choices:");
for (int i = 0; i < count; i++) {
choices[i] = scanner.nextLine();
}
// The method nextInt(int bound) returns an integer
// in range [0, count - 1].
Random random = new Random();
int randomChoice = random.nextInt(count);
System.out.println(options[randomChoice]);
}
}
import java.util.Scanner;
导入java.util.Random;
公共阶级决策{
公共静态void main(字符串[]args){
扫描仪=新的扫描仪(System.in);
System.out.println(“您希望从中选择多少个选项?”);
//我使用ArrayList,因为它可能是手动的
int count=scanner.nextInt();
字符串[]选项=新字符串[计数];
//必要,因为nextInt()不使用
//换行符。
scanner.nextLine();
System.out.println(“输入“+count+”选项:”);
for(int i=0;i
为什么
Random
比Math更好。一般来说,Random()
已经讨论过了,可以找到扫描仪和nextInt()
的问题。您的代码有很多问题
首先,在nextInt
之后调用nextLine
可能会返回”
,因为nextInt
不会读取新行字符。我建议您将I.nextInt()
替换为Integer.parseInt(I.nextLine())
其次,一台扫描仪就足够了。不需要创建两个:I
和C
。您还应该适当地命名变量
第三,如果你想让用户输入一堆东西,你应该使用一个循环。我认为for循环适合这种情况
最后,我个人不喜欢Math.random()
。这使得很难看到您生成的随机数的范围。我建议您使用一个(参见随机数部分)对象
如果你懒惰(我希望你不是),下面是代码:
Scanner scan=新的扫描仪(System.in);
System.out.println(“您希望从中选择多少个选项?”);
int numberOfChoices=Integer.parseInt(scan.nextLine());
字符串[]选项=新字符串[numberOfChoices];
for(int i=0;i
无需在同一流上制作新的扫描仪。此外,您从未将任何值放入选项中。(注意c
从未读过)我不知道在学习基础知识的同时,谁建议使用扫描仪。我建议您首先将输入直接存储在变量中,而不是从输入中读取并尝试使其工作。使用调试器查看代码的工作方式。因此,让一个工作程序与实际输入一起工作是微不足道的。
Scanner scan = new Scanner(System.in);
System.out.println("How many choices would you like to choose from?");
int numberOfChoices = Integer.parseInt(scan.nextLine());
String[] options = new String[numberOfChoices];
for (int i = 0 ; i < numberOfChoices ; i++) {
System.out.println("Enter choice " + (i + 1) + ":");
options[i] = scan.nextLine();
}
Random rand = new Random();
int random = rand.nextInt(numberOfChoices);
String choice = options[random];
System.out.println(choice);