我正在java中介紹計算機科學類。我試圖寫一個圖形程序來解決這個問題......我似乎無法找到什麼導致與我的for循環錯誤
「設計一個代表具有指定半徑和顏色的星形的類,使用填充多邊形繪製星形。在隨機位置繪製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);
}
}
}
星畫家(亞軍):
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);
}
}