简体   繁体   中英

For loop is not adding user input into the array

So what I am trying to do here is make it to where a simple for loop will run through until the iterating variable, i, is equal to "count". As it does so, it will add the user input into the corresponding index. However, when I try to print out an index to verify that it works, it prints nothing. No "null", just emptiness.

I tried doing "shapes[0] = s.next();" and it works, printing out the input. It's only when I try to loop input in that it doesn't print anything.

Here is my code:

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]);
    }
   
  }

I am rather new to Java and as such my knowledge of how to solve issues like this may be lacking. Thank you for your help Stack Overflow!

I just run your code, it worked fine:

在此处输入图像描述

After input "circle", you should press enter button. Hope it's helpful:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM