Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么字符串数组中的随机索引返回null?_Java_Arrays - Fatal编程技术网

Java 为什么字符串数组中的随机索引返回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

我想允许用户输入一个数字,它将是字符串的数量(选项)。然后,程序将生成一个介于0和数组长度之间的随机数,并显示与该随机数的idex匹配的元素。然而,每当我运行程序时,输出只会显示“null”。我已经附上了我的代码。任何建议都会有帮助,谢谢

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()只读取一行,不使用它
    
  • 您不使用
    java.util.array
    import
  • 您不应该创建新的扫描仪,因为您可以将第一个扫描仪用于所有输入目的
  • 请对变量使用有意义的名称,考虑.
  • 随机数
    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);