简体   繁体   English

For 循环没有将用户输入添加到数组中

[英]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".所以我在这里要做的就是让它运行一个简单的for循环,直到迭代变量i等于“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();"我试着做“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.我对 Java 相当陌生,因此我可能缺乏如何解决此类问题的知识。 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:)希望它有帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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