我似乎无法找到导致我的for循环错误的原因

时间:2016-03-15 01:19:10

标签: java graphics

我参加java的计算机科学入门课程。我正在尝试编写一个解决了......的问题的图形程序。

"设计一个代表具有指定半径和颜色的星的类。使用填充多边形绘制星形。设计并实现一个程序,在随机位置绘制10颗随机半径星。"

所以我完成了我认为相当简单的代码,但是当我去编译程序时,我得到48个错误。编译器似乎在抱怨程序的几乎每一行。

非常感谢任何解决这些问题的帮助。 (我希望这是一个影响大多数程序的简单错误。)

提前致谢。

以下是错误列表(删除了文件位置):

48 errors found:
File: C...Star.java  [line: 15]
Error: illegal start of type
File: C...Star.java  [line: 15]
Error: ')' expected
File: C...Star.java  [line: 15]
Error: illegal start of type
File: C...Star.java  [line: 15]
Error: <identifier> expected
File: C...Star.java  [line: 15]
Error: ';' expected
File: C...Star.java  [line: 15]
Error: illegal start of type
File: C...Star.java  [line: 15]
Error: <identifier> expected
File: C...Star.java  [line: 15]
Error: <identifier> expected
File: C...Star.java  [line: 15]
Error: illegal start of type
File: C...Star.java  [line: 15]
Error: <identifier> expected
File: C...Star.java  [line: 16]
Error: ';' expected
File: C...Star.java  [line: 17]
Error: ']' expected
File: C...Star.java  [line: 17]
Error: ';' expected
File: C...Star.java  [line: 17]
Error: <identifier> expected
File: C...Star.java  [line: 17]
Error: illegal start of type
File: C...Star.java  [line: 17]
Error: ';' expected
File: C...Star.java  [line: 18]
Error: ']' expected
File: C...Star.java  [line: 18]
Error: illegal start of type
File: C...Star.java  [line: 18]
Error: <identifier> expected
File: C...Star.java  [line: 18]
Error: ';' expected
File: C...Star.java  [line: 18]
Error: illegal start of type
File: C...Star.java  [line: 18]
Error: illegal start of type
File: C...Star.java  [line: 18]
Error: ';' expected
File: C...Star.java  [line: 19]
Error: ']' expected
File: C...Star.java  [line: 19]
Error: illegal start of type
File: C...Star.java  [line: 19]
Error: <identifier> expected
File: C...Star.java  [line: 19]
Error: ';' expected
File: C...Star.java  [line: 19]
Error: illegal start of type
File: C...Star.java  [line: 19]
Error: illegal start of type
File: C...Star.java  [line: 19]
Error: ';' expected
File: C...Star.java  [line: 22]
Error: class, interface, or enum expected
File: C...Star.java  [line: 25]
Error: class, interface, or enum expected
File: C...Star.java  [line: 28]
Error: class, interface, or enum expected
File: C...Star.java  [line: 29]
Error: class, interface, or enum expected
File: C...Star.java  [line: 31]
Error: class, interface, or enum expected
File: C...Star.java  [line: 31]
Error: class, interface, or enum expected
File: C...Star.java  [line: 31]
Error: class, interface, or enum expected
File: C...Star.java  [line: 34]
Error: class, interface, or enum expected
File: C...Star.java  [line: 35]
Error: class, interface, or enum expected
File: C...Star.java  [line: 36]
Error: class, interface, or enum expected
File: C...Star.java  [line: 37]
Error: class, interface, or enum expected
File: C...Star.java  [line: 47]
Error: class, interface, or enum expected
File: C...Star.java  [line: 50]
Error: class, interface, or enum expected
File: C...Star.java  [line: 52]
Error: class, interface, or enum expected
File: C...Star.java  [line: 52]
Error: class, interface, or enum expected
File: C...Star.java  [line: 52]
Error: class, interface, or enum expected
File: C...Star.java  [line: 55]
Error: class, interface, or enum expected
File: C...Star.java  [line: 56]
Error: class, interface, or enum expected

星级:

import java.util.Random;
import javax.swing.JPanel;
import java.awt.*;

public class Star extends JPanel
{
  Random random = new Random();

  int radius = random.nextInt(10)+1;

  int[] xPos = new int[10];
  int[] yPos = new int[10];
  int[] rad = new int[10];

  for(int i=0; i!=10; i++)
  {
    int xPos[i] = random.nextInt(200)+1;
    int yPos[i] = random.nextInt(200)+1;
    int rad[i] = random.nextInt(10)+1;
  }

  public Star()
   {
     setPreferredSize(new Dimension(200, 200));
   }

  int[] xStar = new int[10];
  int[] yStar = new int[10];
  int[] rad = new int[10];

  for(int ii=0; ii!=10; ii++)
  {
    private int[] (xStar[ii]) = {1*rad[ii]+ xPos[ii], 3*rad[ii]+ xPos[ii], 3*rad[ii]+ xPos[ii], 5*rad[ii]+ xPos[ii], 7*rad[ii]+ xPos[ii], 5*rad[ii]+ xPos[ii] };
    private int[] (yStar[ii]) = {5*rad[ii]+ yPos[ii], 4*rad[ii]+ yPos[ii], 1*rad[ii]+ yPos[ii], 3*rad[ii]+ yPos[ii], 3*rad[ii]+ yPos[ii], 5*rad[ii]+ yPos[ii] };
    private int[] (xStar2[ii]) = {1*rad[ii]+ xPos[ii], 3*rad[ii]+ xPos[ii], 3*rad[ii]+ xPos[ii], 5*rad[ii]+ xPos[ii], 7*rad[ii]+ xPos[ii], 5*rad[ii]+ xPos[ii] };
    private int[] (yStar2[ii]) = {5*rad[ii]+ yPos[ii], 6*rad[ii]+ yPos[ii], 9*rad[ii]+ yPos[ii], 7*rad[ii]+ yPos[ii], 7*rad[ii]+ yPos[ii], 5*rad[ii]+ yPos[ii] };
  }

   //-----------------------------------------------------------------
   //  Constructor: Sets up the basic characteristics of this panel.
   //-----------------------------------------------------------------

   //-----------------------------------------------------------------
   //  Draws a Star using polygons and polylines.
   //-----------------------------------------------------------------

   public void paintComponent(Graphics page)
   {
      super.paintComponent(page);
      page.setColor(Color.red);

      for(int iii=0; iii!=10; iii++)
      {
        page.fillPolygon(yStar[iii], xStar[iii], xStar[iii].length);
        page.fillPolygon(yStar2[iii], xStar2[iii], xStar2[iii].length);
      }

   }
}

Star Painter(亚军):

import javax.swing.JFrame;

public class StarClassCrushingMyDreams
{
   //-----------------------------------------------------------------
   //  Creates the main frame of the program.
   //-----------------------------------------------------------------
   public static void main(String[] args)
   {

      JFrame frame = new JFrame("Rocket");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setSize(300,300);


      Star panel = new Star();



      frame.getContentPane().add(panel);
      frame.setVisible(true);

      frame.setVisible(true);
   }
}

2 个答案:

答案 0 :(得分:0)

除了for循环不在初始化程序块或构造函数中(如在@ John3136的答案中),循环的内容无效:

int xPos[i] = random.nextInt(200)+1;

应该是

xPos[i] = random.nextInt(200)+1;

您没有声明变量,只是为现有数组赋值。

类似地:

private int[] (xStar[ii]) = {1*rad[ii]+ xPos[ii], 3*rad[ii]+ xPos[ii], 3*rad[ii]+ xPos[ii], 5*rad[ii]+ xPos[ii], 7*rad[ii]+ xPos[ii], 5*rad[ii]+ xPos[ii] };

我不确定你在这里写的是什么。当然private int[]不应该在那里,但xStar的类型为int[],而不是int[][]

此外,您尚未在任何地方声明xStar2yStar2

答案 1 :(得分:-1)

你的for循环位于类的范围内,而不是在方法中。这不合法。

并且你在for循环的范围内声明xPos等,所以你实际上不会设置类变量,只是for循环中的卷影副本。

因为我在这里:让每个明星成为一个JPanel似乎“奇怪”,你不想在同一个面板中画出10颗星吗?