簡體   English   中英

從用戶那里獲取輸入並將其存儲在Java中的字符串數組中

[英]get input from user and store it in String Array in Java

我編寫了一個程序,該程序從用戶那里獲得“輸入學生人數:”的輸入,然后將學生姓名添加到其中並在控制台中打印出來。 我寫了一個運行良好的代碼,但問題是循環已經漫游了一段時間,代碼無法正常工作我也想知道如何使用不帶掃描儀的命令行參數獲取輸入並將其存儲在字符串數組中

電流輸出就是這樣 在此處輸入圖片說明

這是我的代碼,請幫助,我正在學習Java短語

import java.util.Scanner;

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

    //get the input for number of students:
    System.out.println("Enter The number of students:");
    int totalstudents = in.nextInt();

    //store into String array

    String studentname[] = new String[totalstudents];

    for(int i = 0; i < studentname.length;i++)
    {
        System.out.println(i);
        System.out.println("Enter Student Names: ");
        studentname[i] = in.nextLine();
    }
    for(String names:studentname)
    {
        System.out.println(names);
    }
}

}

next():查找並返回此掃描儀的下一個完整令牌。

nextLine():將此掃描程序前進到當前行之外,並返回被跳過的輸入。

嘗試放置一個scanner.nextLine(); 如果要忽略該行的其余部分,則在每個nextInt()之后。

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

    //get the input for number of students:
    System.out.println("Enter The number of students:");
    int totalstudents = in.nextInt();
     in.nextLine();// just to ignore the line
    //store into String array

    String studentname[] = new String[totalstudents];

    for(int i = 0; i < studentname.length;i++)
    {

        System.out.println("Enter Student Names: "+i);
        studentname[i] = in.nextLine();
    }
    for(String names:studentname)
    {
        System.out.println(names);
    }
 }
}

您可以使用數組args[]不必在此傳遞學生人數。

因此,您在java <className>之后在命令提示符處傳遞的任何名稱都將存儲在此數組中,並且可以對其進行迭代。

使用ArrayList而不是String Array

聲明頭文件

      import java.util.ArrayList;

更改您的代碼

      Scanner in = new Scanner(System.in);

    //get the input for number of students:
    System.out.println("Enter The number of students:");
    int totalstudents = in.nextInt();

    //store into arraylist
    ArrayList<String> al = new ArrayList<String>();

    for(int i = 0; i < totalstudents;i++)
    {
        System.out.println(i);
        System.out.println("Enter Student Names: ");
        al.add(in.next());
    }
    for(int i=0; i< al.size(); i++)
    {
        System.out.println(al.get(i));
    }

添加in.nextLine(); 在分配此int totalstudents = in.nextInt();

試試這個代碼:

Scanner in = new Scanner(System.in);

//get the input for number of students:
System.out.print("Enter The number of students:");
int totalstudents = in.nextInt();

//store into String array

String studentname[] = new String[totalstudents];

for(int i = 0; i < studentname.length;i++)
{
    System.out.print("Enter Student " + i + " Name:");
    studentname[i] = in.nextLine();
}
for(int i = 0; i < studentname.length;i++)
{
    System.out.println(studentname[i]);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM