
正文
java期末作业代码 java作业题目
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
用java web小游戏源代码。期末结课老师让做,急用,谢了
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JFrame;
@SuppressWarnings("serial")
public class MainClass extends JFrame {
ControlSnake control;
Toolkit kit;
Dimension dimen;
public static void main(String[] args) {
new MainClass("my snake");
}
public MainClass(String s) {
super(s);
control = new ControlSnake();
control.setFocusable(true);
kit = Toolkit.getDefaultToolkit();
dimen = kit.getScreenSize();
add(control);
setLayout(new BorderLayout());
setLocation(dimen.width / 3, dimen.height / 3);// dimen.width/3,dimen.height/3
setSize(FWIDTH, FHEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setVisible(true);
}
public static final int FWIDTH = 315;
public static final int FHEIGHT = 380;
}
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.Timer;
@SuppressWarnings("serial")
public class ControlSnake extends JPanel implements ActionListener {
Random rand;
ArrayListPoint list, listBody;
String str, str1;
static boolean key;
int x, y, dx, dy, fx, fy, flag;
int snakeBody;
int speed;
public ControlSnake() {
snakeBody = 1;
str = "上下左右方向键控制 P键暂停...";
str1 = "现在的长度为:" + snakeBody;
key = true;
flag = 1;
speed = 700;
rand = new Random();
list = new ArrayListPoint();
listBody = new ArrayListPoint();
x = 5;
y = 5;
list.add(new Point(x, y));
listBody.add(list.get(0));
dx = 10;
dy = 0;
fx = rand.nextInt(30) * 10 + 5;// 2
fy = rand.nextInt(30) * 10 + 5;// 2
setBackground(Color.BLACK);
setSize(new Dimension(318, 380));
final Timer time = new Timer(speed, this);
time.start();
addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == 37) {
dx = -10;
dy = 0;
} else if (e.getKeyCode() == 38) {
dx = 0;
dy = -10;
} else if (e.getKeyCode() == 39) {
dx = 10;
dy = 0;
} else if (e.getKeyCode() == 40) {
dx = 0;
dy = 10;
} else if (e.getKeyCode() == 80) {
if (flag % 2 == 1) {
time.stop();
}
if (flag % 2 == 0) {
time.start();
}
flag++;
}
}
});
}
public void paint(Graphics g) {
g.setColor(Color.WHITE);
g.fillRect(0, 0, 400, 400);
g.setColor(Color.DARK_GRAY);
g.drawLine(3, 3, 305, 3);
g.drawLine(3, 3, 3, 305);
g.drawLine(305, 3, 305, 305);
g.drawLine(3, 305, 305, 305);
g.setColor(Color.PINK);
for (int i = 0; i listBody.size(); i++) {
g.fillRect(listBody.get(i).x, listBody.get(i).y, 9, 9);
}
g.fillRect(x, y, 9, 9);
g.setColor(Color.ORANGE);
g.fillRect(fx, fy, 9, 9);
g.setColor(Color.DARK_GRAY);
str1 = "现在的长度为:" + snakeBody;
g.drawString(str, 10, 320);
g.drawString(str1, 10, 335);
}
public void actionPerformed(ActionEvent e) {
x += dx;
y += dy;
if (makeOut() == false) {
JOptionPane.showMessageDialog(null, "重新开始......");
speed = 700;
snakeBody = 1;
x = 5;
y = 5;
list.clear();
list.add(new Point(x, y));
listBody.clear();
listBody.add(list.get(0));
dx = 10;
dy = 0;
}
addPoint(x, y);
if (x == fx y == fy) {
speed = (int) (speed * 0.8);//速度增加参数
if (speed 200) {
speed = 100;
}
fx = rand.nextInt(30) * 10 + 5;// 2
fy = rand.nextInt(30) * 10 + 5;// 2
snakeBody++;// 2
} // 2
repaint();
}
public void addPoint(int xx, int yy) {
// 动态的记录最新发生的50步以内的移动过的坐标
// 并画出最新的snakeBody
if (list.size() 100) {//蛇身长度最长为100
list.add(new Point(xx, yy));
} else {
list.remove(0);
list.add(new Point(xx, yy));
}
if (snakeBody == 1) {
listBody.remove(0);
listBody.add(0, list.get(list.size() - 1));
} else {
listBody.clear();
if (list.size() snakeBody) {
for (int i = list.size() - 1; i 0; i--) {
listBody.add(list.get(i));
}
} else {
for (int i = list.size() - 1; listBody.size() snakeBody; i--) {
listBody.add(list.get(i));
}
}
}
}
public boolean makeOut() {
if ((x 3 || y 3) || (x 305 || y 305)) {
return false;
}
for (int i = 0; i listBody.size() - 1; i++) {
for (int j = i + 1; j listBody.size(); j++) {
if (listBody.get(i).equals(listBody.get(j))) {
return false;
}
}
}
return true;
}
}
/*贪吃蛇代码*/
相关问答
Q1: JAVA上课作业:求一份代码
Java程序代码:
public class test {
public static void main(String[] args) {
if(args.length != 2) {
System.out.println("参数数量错误!必须是2个参数,以空格隔开...");
return;
}
Rectangle rect = new Rectangle(Double.parseDouble(args[0]), Double.parseDouble(args[1]));
System.out.println(rect.getArea() + " " + rect.getPerimeter());
}
}
class Rectangle {
protected double height;
protected double width;
public Rectangle(double height, double width) {
this.height = height;
this.width = width;
}
public double getArea() {
return height * width;
}
public double getPerimeter() {
return 2 * (height + width);
}
}
运行测试:
Q2: java期末作业做一个任意的程序。比如小型计算器。但是没学好,不会啊。求教高手。
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class JISUANQI
{
public static void main(String[] str)
{
jisuanqi SS1=new jisuanqi();
SS1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
SS1.setVisible(true);SS1.setSize(300,300);
}
}
class jisuanqi extends JFrame implements ActionListener
{
double a=0,b=0,c=0,fuhao=5;
Boolean has=false;
Container A;
JTextField tf1;
JButton bt1,bt2,bt3,bt4,bt5,bt6,bt7,bt8,bt9,bt0,bt_dian;
JButton bt_jia,bt_jian,bt_cheng,bt_chu,bt_fuhao;
JButton bt_clear,bt_jisuan;
public jisuanqi()
{
setTitle("简单计算器");
A=getContentPane();
GridBagLayout f=new GridBagLayout();
A.setLayout(f);
GridBagConstraints g=new GridBagConstraints();
g.fill=GridBagConstraints.BOTH;
g.gridwidth=1;
g.gridheight=1;
//面板的实例化
//显示框的实例化
tf1=new JTextField(10);tf1.setEditable(false);
//数字键的实例化
bt1=new JButton("1");bt2=new JButton("2");bt3=new JButton("3");
bt4=new JButton("4");bt5=new JButton("5");bt6=new JButton("6");
bt7=new JButton("7");bt8=new JButton("8");bt9=new JButton("9");
bt0=new JButton("0");bt_dian=new JButton(".");bt_fuhao=new JButton("+/-");
bt_jia=new JButton("+");bt_jian=new JButton("-");
bt_cheng=new JButton("*");bt_chu=new JButton("/");
bt_clear=new JButton("clear");bt_jisuan=new JButton("=");
g.gridx=1;g.gridy=0;A.add(tf1,g);
g.gridx=0;g.gridy=1;A.add(bt_clear,g);
g.gridx=3;g.gridy=1;A.add(bt_jisuan,g);
g.gridx=0;g.gridy=2;A.add(bt7,g);
g.gridx=1;g.gridy=2;A.add(bt8,g);
g.gridx=2;g.gridy=2;A.add(bt9,g);
g.gridx=3;g.gridy=2;A.add(bt_jia,g);
g.gridx=0;g.gridy=3;A.add(bt4,g);
g.gridx=1;g.gridy=3;A.add(bt5,g);
g.gridx=2;g.gridy=3;A.add(bt6,g);
g.gridx=3;g.gridy=3;A.add(bt_jian,g);
g.gridx=0;g.gridy=4;A.add(bt1,g);
g.gridx=1;g.gridy=4;A.add(bt2,g);
g.gridx=2;g.gridy=4;A.add(bt3,g);
g.gridx=3;g.gridy=4;A.add(bt_cheng,g);
g.gridx=0;g.gridy=5;A.add(bt0,g);
g.gridx=1;g.gridy=5;A.add(bt_fuhao,g);
g.gridx=2;g.gridy=5;A.add(bt_dian,g);
g.gridx=3;g.gridy=5;A.add(bt_chu,g);
bt1.addActionListener(this);
bt2.addActionListener(this);
bt3.addActionListener(this);
bt4.addActionListener(this);
bt5.addActionListener(this);
bt6.addActionListener(this);
bt7.addActionListener(this);
bt8.addActionListener(this);
bt9.addActionListener(this);
bt0.addActionListener(this);
bt_clear.addActionListener(this);
bt_dian.addActionListener(this);
bt_fuhao.addActionListener(this);
bt_jia.addActionListener(this);
bt_jian.addActionListener(this);
bt_cheng.addActionListener(this);
bt_chu.addActionListener(this);
bt_jisuan.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==bt1)
{tf1.setText(tf1.getText()+"1");}
else if(e.getSource()==bt2)
{
if(!tf1.getText().equals("0"))
tf1.setText(tf1.getText()+"2");
}
else if(e.getSource()==bt3)
{
if(!tf1.getText().equals("0"))
tf1.setText(tf1.getText()+"3");
}
else if(e.getSource()==bt4)
{
if(!tf1.getText().equals("0"))
tf1.setText(tf1.getText()+"4");
}
else if(e.getSource()==bt5)
{
if(!tf1.getText().equals("0"))
tf1.setText(tf1.getText()+"5");
}
else if(e.getSource()==bt6)
{
if(!tf1.getText().equals("0"))
tf1.setText(tf1.getText()+"6");
}
else if(e.getSource()==bt7)
{
if(!tf1.getText().equals("0"))
tf1.setText(tf1.getText()+"7");
}
else if(e.getSource()==bt8)
{
if(!tf1.getText().equals("0"))
tf1.setText(tf1.getText()+"8");
}
else if(e.getSource()==bt9)
{
if(!tf1.getText().equals("0"))
tf1.setText(tf1.getText()+"9");
}
else if(e.getSource()==bt0)
{
if(!tf1.getText().equals("0"))
{
tf1.setText(tf1.getText()+"0");
}
}
else if(e.getSource()==bt_dian)
{
if(tf1.getText().indexOf(".")==-1tf1.getText().length()0)
{
tf1.setText(tf1.getText()+".");
}
}
else if(e.getSource()==bt_jia)
{
if(tf1.getText().trim().length()0)
{ a=Double.valueOf(tf1.getText());
tf1.setText("");fuhao=0;
has=true;
}
}
else if(e.getSource()==bt_jian)
{
if(tf1.getText().trim().length()0)
{
a=Double.valueOf(tf1.getText());
tf1.setText("");fuhao=1;
has=true;
}
}
else if(e.getSource()==bt_cheng)
{
if(tf1.getText().trim().length()0)
{
a=Double.valueOf(tf1.getText());
tf1.setText("");fuhao=2;
has=true;
}
}
else if(e.getSource()==bt_chu)
{
if(tf1.getText().trim().length()0)
{
a=Double.valueOf(tf1.getText());
tf1.setText("");fuhao=3;
has=true;
}
}
else if(e.getSource()==bt_jisuan)
{
try
{
if(tf1.getText().length()0has)
{
b=Double.valueOf(tf1.getText());
if(fuhao==0)
{
c=a+b;
tf1.setText(String.valueOf(c));
has=false;
}
else if(fuhao==1)
{
c=a-b;
tf1.setText(String.valueOf(c));
has=false;
}
else if(fuhao==2)
{
c=a*b;
tf1.setText(String.valueOf(c));
has=false;
}
else if(fuhao==3)
{
c=a/b;
tf1.setText(String.valueOf(c));
has=false;
}
}
}
catch(Exception ex)
{tf1.setText(ex.getMessage().toString());}
}
else if(e.getSource()==bt_fuhao)
{
if(tf1.getText().indexOf("-")==-1tf1.getText().length()0)
{
String s=tf1.getText();
tf1.setText("-"+s);
}
else
{
if(tf1.getText().length()0)
{
String s=tf1.getText().substring(1);
tf1.setText(s);
}
}
}
else if(e.getSource()==bt_clear)
{
tf1.setText("");
}
}
}
好了,这个可能有点小问题,你自己找人帮你看看就是了,我也不知道了,写个程序,烦死姐了
Q3: 这两道题代码怎么写java?
创建一个名字为“ReportCard”的类,然后用下边的内容全部替换掉,你会成为全班最亮的仔。
import java.util.HashMap;
/**
* 学生成绩单
*/
public class ReportCard {
public static void main(String[] args) {
ReportCard reportCard = new ReportCard("张三", "070602213");
reportCard.set("语文", 80.0);
reportCard.set("数学", 59.5);
reportCard.set("英语", 66.0);
reportCard.set("java", 80, 99.0);
reportCard.set("数据库", 80, 66.0);
reportCard.set("毛概", null);
System.out.println(reportCard.getStudentName() + "语文分数:" + reportCard.get("语文"));
System.out.println(reportCard.getStudentName() + "数学考核结果:" + (reportCard.isPassed("数学") ? "合格" : "不合格"));
System.out.println(reportCard.getStudentName() + "期末是否挂科:" + (reportCard.isAllPassed() ? "否" : "是"));
}
// 学生姓名
private String studentName;
// 学生学号
private String studentNumber;
// 成绩单
private HashMapString, CourseResult cards = new HashMap();
public ReportCard() {
}
public ReportCard(String studentName, String studentNumber) {
this.studentName = studentName;
this.studentNumber = studentNumber;
}
public Double get(String courseName){
CourseResult courseResult = cards.get(courseName);
return courseResult == null ? Double.NaN : courseResult.getStudentScore();
}
public void set(String courseName, Double studentScore){
CourseResult courseResult = new CourseResult(courseName, studentScore);
cards.put(courseName, courseResult);
}
public void set(String courseName, double passMark, Double studentScore){
CourseResult courseResult = new CourseResult(courseName, passMark, studentScore);
cards.put(courseName, courseResult);
}
public boolean isPassed(String courseName){
return cards.get(courseName).isPassed();
}
public boolean isAllPassed(){
for(CourseResult cr : cards.values()){
if ( ! cr.isPassed()) {
return false;
}
}
return true;
}
public String getStudentName() {
return studentName;
}
public String getStudentNumber() {
return studentNumber;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public void setStudentNumber(String studentNumber) {
this.studentNumber = studentNumber;
}
/**
* 课程
*/
class Course{
// 课程名称
protected String courseName;
// 及格分
protected double passMark = 60;
public Course(String courseName, Double passMark) {
this.courseName = courseName;
if ( passMark != null) {
this.passMark = passMark;
}
}
}
/**
* 课程成绩
*/
class CourseResult extends Course{
// 学生成绩
private Double studentScore;
public CourseResult(String courseName, Double studentScore) {
this(courseName, null, studentScore);
}
public CourseResult(String courseName, Double passMark, Double studentScore) {
super(courseName, passMark);
this.studentScore = studentScore == null ? Double.NaN : studentScore;
}
public boolean isPassed(){
return studentScore = passMark;
}
public String getCourseName() {
return courseName;
}
public double getPassMark() {
return passMark;
}
public Double getStudentScore() {
return studentScore;
}
}
Q4: 一道java题 Java期末作业包括纸质文档和程序代码两部分 急........求大神
超市管理系统java期末作业代码的java期末作业代码,谢谢java期末作业代码!
关于java期末作业代码和java作业题目的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。






