如何将用户输入用于 while 循环中的字符串数组?

我正在尝试将来自学生姓名和分数问题的用户输入转换为数组。我还需要帮助来打印数组。while 循环使用 boolean loopNaming 作为其条件,并且每次循环发生时都会更新 i 。


 public static void main(String [] args)

    {

        Scanner keyboard = new Scanner(System.in);

        double score;


        boolean loopNaming=true;

        int i=0;

        String[] name = new String[i];




         while(loopNaming==true)

            {

                System.out.printf("Enter name of student or done to finish: ");

                name[i] = keyboard.next();


                if(name[i].equals("done"))

                {

                    loopNaming = false;

                }

                else

                {

                    System.out.println("Enter score: ");

                    score = keyboard.nextDouble();

                }

                i=i+1;


            }

            System.out.println(name[i]);


        }

    }


素胚勾勒不出你
浏览 176回答 3
3回答

饮歌长啸

您可以简化程序的逻辑并编写如下内容,public static void main(String[] args) {&nbsp; &nbsp; Scanner keyboard = new Scanner(System.in);&nbsp; &nbsp; List<String> nameList = new ArrayList<String>();&nbsp; &nbsp; List<Double> scoreList = new ArrayList<Double>();&nbsp; &nbsp; while (true) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.printf("Enter first name of student or done to finish: ");&nbsp; &nbsp; &nbsp; &nbsp; String fname = keyboard.next();&nbsp; &nbsp; &nbsp; &nbsp; if (fname.equals("done")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.printf("Enter last name of student: ");&nbsp; &nbsp; &nbsp; &nbsp; String lname = keyboard.next();&nbsp; &nbsp; &nbsp; &nbsp; nameList.add(fname + " " + lname);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Enter score: ");&nbsp; &nbsp; &nbsp; &nbsp; scoreList.add(keyboard.nextDouble());&nbsp; &nbsp; }&nbsp; &nbsp; keyboard.close();&nbsp; &nbsp; System.out.println("Names: " + nameList);&nbsp; &nbsp; System.out.println("scores: " + scoreList);}

繁华开满天机

public static void main(String [] args)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Scanner keyboard = new Scanner(System.in);&nbsp; &nbsp; &nbsp; &nbsp; double score;&nbsp; &nbsp; &nbsp; &nbsp; boolean loopNaming=true;&nbsp; &nbsp; &nbsp; &nbsp; int i=0;&nbsp; &nbsp; &nbsp; &nbsp; ArrayList<String> name = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; while(loopNaming==true)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.printf("Enter name of student or done to finish: ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String input = keyboard.next();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(input.equals("done"))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; loopNaming = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp;name.add(input);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Enter score: ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; score = keyboard.nextDouble();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i=i+1; //no need to use&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(name);&nbsp; &nbsp; }您应该使用 adynamic list因为您不能在 Java 中调整数组的大小。第二点当用户给出“完成”时,你不应该把它放在列表中,所以在插入之前检查它。

慕妹3242003

您声明了大小为 0 的 String 数组。这就是您不能向其中添加元素的原因。import java.util.Scanner;public class NameArray {&nbsp;public static void main(String [] args){&nbsp; &nbsp; &nbsp;Scanner keyboard = new Scanner(System.in);&nbsp; &nbsp; &nbsp;double score[] = new double[10];&nbsp; &nbsp; &nbsp;boolean loopNaming=true;&nbsp; &nbsp; &nbsp;int i=0;&nbsp; &nbsp; &nbsp;String namae;&nbsp; &nbsp; &nbsp;String[] name = new String[10];&nbsp; &nbsp; &nbsp;int count = 0;&nbsp; &nbsp; &nbsp;while(loopNaming==true){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.printf("Enter name of student or done to finish: ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;name[i] = keyboard.next();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(name[i].equals("done")){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;loopNaming = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Enter score: ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; score[i] = keyboard.nextDouble();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;i=i+1;&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp;for(int j = 0; j < count; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(name[j]+" "+score[j]);&nbsp; &nbsp; &nbsp;}&nbsp;}}试试这个代码,或者你可以选择任何其他数据结构。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java