簡體   English   中英

將用戶字符串輸入到循環數組中

[英]input user string into a looping array

當我嘗試將一個字符串放入控制台時,我的循環不會將我的字符串添加到我的數組中,而是直接進入錯誤。 我在這里缺少什么?

public static void main(String[]args)
  {
      System.out.println("Trucks running today (min 2):  ");
      Scanner trucks = new Scanner(System.in);

      int userTrucks = trucks.nextInt();
      int [] x = truckNumberArray(userTrucks);
        if(x == null) 
            {
                System.out.print("Error: must be at least two trucks.");
                System.exit(0);
            }
    Scanner size = new Scanner(System.in);
    String [] largeSmall = new String[x.length];
    for(int t = 1; t<x.length;t++)
         {

            System.out.println("Truck is large or small (large max = 100, small max = 10)?  ");
            largeSmall[t] = size.nextLine();
            if (largeSmall.equals("small")) 
                {  
                    System.out.print(" "); 
                }            
            else if (largeSmall.equals("large")) 
                {
                    System.out.print(" ");
                } 
            else 
                {
                        System.out.println("error");
                        System.exit(0);
                }

         }              
   }
}

請注意: if (largeSmall.equals(...

修改: if (largeSmall[t].equals(...

不能通過使用該數組的名稱直接訪問任何數組元素,位置也需要指定。 String [] largeSmall = new String[x.length]; 這是一個字符串數組,必須作為largeSmall[t].equals("small")

暫無
暫無

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

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