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

时间:2018-10-11 05:35:13

标签: java arrays while-loop

我正在尝试将来自学生姓名和分数的问题的用户输入转换为数组。 我还需要帮助以打印出阵列。 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]);

        }
    }

5 个答案:

答案 0 :(得分:3)

您可以简化程序的逻辑并编写如下内容,

public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);

    List<String> nameList = new ArrayList<String>();
    List<Double> scoreList = new ArrayList<Double>();

    while (true) {
        System.out.printf("Enter first name of student or done to finish: ");
        String fname = keyboard.next();
        if (fname.equals("done")) {
            break;
        }
        System.out.printf("Enter last name of student: ");
        String lname = keyboard.next();
        nameList.add(fname + " " + lname);
        System.out.println("Enter score: ");
        scoreList.add(keyboard.nextDouble());
    }
    keyboard.close();
    System.out.println("Names: " + nameList);
    System.out.println("scores: " + scoreList);
}

答案 1 :(得分:0)

我将数组更改为arraylist并移动了i = i + 1;到其他段中。还更改了最终打印语句以打印列表。

public static void main(String [] args)
  {
      Scanner keyboard = new Scanner(System.in);
      double score;

      boolean loopNaming=true;
      int i=0;
      ArrayList<String> name = new ArrayList<String>();



       while(loopNaming==true)
          {
              System.out.printf("Enter name of student or done to finish: ");
              name.add(keyboard.next());

              if(name.get(i).equals("done"))
              {
                  loopNaming = false;
              }
              else
              {
                  System.out.println("Enter score: ");
                  score = keyboard.nextDouble();
                  i=i+1;
              }

          }
          System.out.println(name);
      }

答案 2 :(得分:0)

我首先建议使用列表数据结构:

    public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);
    double score;
    boolean loopNaming = true;
    List<String> name = new ArrayList<>();

    while (loopNaming) {
        System.out.printf("Enter name of student or done to finish: ");
        String input = keyboard.next();

        if (input.equals("done")) {
            loopNaming = false;
        } else {
            name.add(input);
            System.out.println("Enter score: ");
            score = keyboard.nextDouble();
        }
    }
    System.out.println(name.toString());
}

但是,如果您非常想使用数组,则可以使用自己的方法来在每次添加新名称时增加数组的大小:

public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);
    double score;
    int i = 0;
    boolean loopNaming = true;
    String[] name = {};

    while (loopNaming) {
        System.out.printf("Enter name of student or done to finish: ");
        String input = keyboard.next();

        if (input.equals("done")) {
            loopNaming = false;
        } else {
            name = increaseArray(name);
            name[i] = input;
            System.out.println("Enter score: ");
            score = keyboard.nextDouble();
            i++;
        }
    }
    System.out.println(Arrays.toString(name));
}

public static String[] increaseArray(String[] arr) {
    String[] temp = new String[arr.length + 1];
    for (int i = 0; i < arr.length; i++) {
        temp[i] = arr[i];
    }
    return temp;
}

我不确定您的计分变量是什么计划,但这是实现所需结果的两种方法。

我希望这会有所帮助。

答案 3 :(得分:0)

 public static void main(String [] args)
    {
        Scanner keyboard = new Scanner(System.in);
        double score;

        boolean loopNaming=true;
        int i=0;
        ArrayList<String> name = new ArrayList<>();



        while(loopNaming==true)
        {
            System.out.printf("Enter name of student or done to finish: ");

            String input = keyboard.next();
            if(input.equals("done"))
            {
                loopNaming = false;
            }
            else
            {   name.add(input);
                System.out.println("Enter score: ");
                score = keyboard.nextDouble();
            }
            i=i+1; //no need to use

        }
        System.out.println(name);

    }

您应该使用dynamic list,因为您无法在Java中调整数组的大小。第二点当用户输入“完成”时,请勿将其放在列表中,因此请在插入之前进行检查。

答案 4 :(得分:0)

您声明了大小为0的String数组。这就是为什么您不能在其中添加元素的原因。

import java.util.Scanner;

public class NameArray {
 public static void main(String [] args){
     Scanner keyboard = new Scanner(System.in);
     double score[] = new double[10];

     boolean loopNaming=true;
     int i=0;
     String namae;
     String[] name = new String[10];
     int count = 0;


     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[i] = keyboard.nextDouble();
            count++;
         }
         i=i+1;

     }
     for(int j = 0; j < count; j++) {
         System.out.println(name[j]+" "+score[j]);
     }
 }
}

尝试使用此代码,否则您可以使用任何其他数据结构。