簡體   English   中英

For 循環沒有將用戶輸入添加到數組中

[英]For loop is not adding user input into the array

所以我在這里要做的就是讓它運行一個簡單的for循環,直到迭代變量i等於“count”。 當它這樣做時,它會將用戶輸入添加到相應的索引中。 但是,當我嘗試打印出索引以驗證它是否有效時,它什么也沒打印。 沒有“空”,只有空虛。

我試着做“shapes[0] = s.next();” 它可以工作,打印出輸入。 只有當我嘗試循環輸入時,它才不會打印任何內容。

這是我的代碼:

import java.awt.*;
import java.util.*;
public class Animation
{
    static Scanner s = new Scanner(System.in);
  public static void getShapeInformation(int count, String[] shapes, int[] size, String[] color, int[] direction, int[] speed)
  {
      for(int i=0; i<count; i++)
      {
          shapes[i] = s.next();
      }
  }
  public static void main(String[] args)
  {
        System.out.println("UTSA - Fall 2021 - CS1083 - Section 001 - Projecet 3 - Written by Richard Pech");
        System.out.print("\n");
        System.out.print("Please input width, height of the panel, # of shapes, # of times to move followed by the shape, size, color, direction, and speed of every shape: ");
        
        int w = s.nextInt();
        int h = s.nextInt();
        int sAmount = s.nextInt();
        int times = s.nextInt();
        
        
        String[] shapes = new String[sAmount];
        int[] sizes = new int[sAmount];
        String[] colors = new String[sAmount];
        int[] directions = new int[sAmount];
        int[] speeds = new int[sAmount];
        
        
        
        getShapeInformation(sAmount,shapes,sizes,colors,directions,speeds);
        
        
        DrawingPanel panel = new DrawingPanel(w,h);
        System.out.println(shapes[0]);
    }
   
  }

我對 Java 相當陌生,因此我可能缺乏如何解決此類問題的知識。 感謝您對堆棧溢出的幫助!

我只是運行你的代碼,它工作正常:

在此處輸入圖像描述

輸入“圓圈”后,應按回車鍵。 希望它有幫助:)

暫無
暫無

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

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