prprprll 2020-06-09 16:21 采纳率: 100%
浏览 177
已采纳

想请大家看一下错在哪里呀,下面的是eclipse的错误提示

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.*;

public class MyGame extends JFrame {
public MyGame(){
this.setTitle("我的游戏");
this.setBounds(300,100, 800, 600);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setBackground(Color.black);

MyPanel mp=new MyPanel();
add(mp);
this.addKeyListener(mp) ;
mp.addKeyListener(mp);

Thread t=new Thread();
t.start();
}
public static void main(String args[]){
new MyGame();
}
}
class MyPanel extends JPanel implements Runnable,KeyListener{
int score;
int ballcount=8;

int x[] = new int[ballcount];
int y[] = new int[ballcount];
char ch[]=new char[ballcount];
Color ballColor[]=new Color[ballcount];
public void init(){
score=20;
for (int i = 0; i < ballcount; i++) {
x[i] = (int) (Math.random() * 800);
y[i] = (int) (Math.random() * 600);

ch[i]=(char)(Math.random()*26+65);

ballColor[i]=new Color((int)(Math.random()*255),(int)(Math.random()*255),(int)(Math.random()*255));
}
public MyPanel(){
init();
}

public void paint(Graphics g){
Image img;
super.paint(g);
try {
img = ImageIO.read(new File("19.jpg"));
g.drawImage(img, 0, 0, this);

} catch (IOException e) {// TODO Auto-generated catch block
e.printStackTrace();
}
g.setFont(new Font("", Font.BOLD, 28));
g.setColor(Color.red);
g.drawString("score:"+score, 10, 30);
for (int i = 0; i < ballcount; i++) {
g.setColor(ballColor[i]);
g.fillOval(x[i] - 20, y[i] - 30, 50, 50);
g.setFont(new Font("", Font.BOLD, 20));
g.setColor(Color.white);
g.drawString(""+ch[i], x[i], y[i]);
y[i]++;
if (y[i] > 600) {
y[i] = 0;
x[i] = (int) (Math.random() * 1000);
ch[i]=(char)(Math.random()*26+65);
ballColor[i]=new Color((int)(Math.random()*255),(int)(Math.random()*255),(int)(Math.random()*255));
score--;
}
}
}

public void run() {
while (true) {
repaint();
try {
Thread.sleep(10);
}catch (InterruptedException e) {
e.printStackTrace();
}
}

}

public void keyPressed(KeyEvent e) {
char c=e.getKeyChar();
System.out.println(c);
for(int i=0;i<ballcount;i++){
if(c==ch[i]){ x[i]=(int)(Math.random()*800);
y[i]=0;
ch[i]=(char)((Math.random()*26)+65);
ballColor[i]=new Color((int)(Math.random()*255),(int)(Math.random()*255),(int)(Math.random()*255));
score++;
}
}
}

public void keyReleased(KeyEvent e) { }

public void keyTyped(KeyEvent e) { }
}
错误是
MyGame.java:47: 错误: 非法的表达式开始
public MyPanel(){

eclipse的错误显示
Syntax error, insert "}" to complete Block

at game.MyPanel.init(MyGame.java:46)
at game.MyPanel.<init>(MyGame.java:49)
at game.MyGame.<init>(MyGame.java:21)
at game.MyGame.main(MyGame.java:29)
  • 写回答

1条回答 默认 最新

  • threenewbee 2020-06-09 16:26
    关注

    public void init(){
    score=20;
    for (int i = 0; i < ballcount; i++) {
    x[i] = (int) (Math.random() * 800);
    y[i] = (int) (Math.random() * 600);

    ch[i]=(char)(Math.random()*26+65);

    ballColor[i]=new Color((int)(Math.random()*255),(int)(Math.random()*255),(int)(Math.random()*255));
    }
    //少了一个后花括号
    }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于#flink#的问题:关于docker部署flink集成hadoop的yarn,请教个问题flink启动yarn-session.sh连不上hadoop
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题