扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
贪吃蛇游戏 望采纳
创新互联公司专注于企业成都全网营销、网站重做改版、西平网站定制设计、自适应品牌网站建设、H5网站设计、商城网站定制开发、集团公司官网建设、成都外贸网站制作、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为西平等各大城市提供网站开发制作服务。
import java.awt.Button;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.*;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class Snake extends JFrame implements KeyListener{
int Count=0;
Button[][] grid = new Button[20][20];
ArrayListPoint snake_list=new ArrayListPoint();
Point bean=new Point(-1,-1);//保存随机豆子【坐标】
int Direction = 1; //方向标志 1:上 2:下 3:左 4:右
//构造方法
public Snake()
{
//窗体初始化
this.setBounds(400,300,390,395);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridLayout f=new GridLayout(20,20);
this.getContentPane().setBackground(Color.gray);
this.setLayout(f);
//初始化20*20个按钮
for(int i=0;i20;i++)
for(int j=0;j20;j++)
{
grid[i][j]=new Button();
this.add(grid[i][j]);
grid[i][j].setVisible(false);
grid[i][j].addKeyListener(this);
grid[i][j].setBackground(Color.blue);
}
//蛇体初始化
grid[10][10].setVisible(true);
grid[11][10].setVisible(true);
grid[12][10].setVisible(true);
grid[13][10].setVisible(true);
grid[14][10].setVisible(true);
//在动态数组中保存蛇体按钮坐标【行列】信息
snake_list.add(new Point(10,10));
snake_list.add(new Point(11,10));
snake_list.add(new Point(12,10));
snake_list.add(new Point(13,10));
snake_list.add(new Point(14,10));
this.rand_bean();
this.setTitle("总分:0");
this.setVisible(true);
}
//该方法随机一个豆子,且不在蛇体上,并使豆子可见
public void rand_bean(){
Random rd=new Random();
do{
bean.x=rd.nextInt(20);//行
bean.y=rd.nextInt(20);//列
}while(snake_list.contains(bean));
grid[bean.x][bean.y].setVisible(true);
grid[bean.x][bean.y].setBackground(Color.red);
}
//判断拟增蛇头是否与自身有碰撞
public boolean is_cross(Point p){
boolean Flag=false;
for(int i=0;isnake_list.size();i++){
if(p.equals(snake_list.get(i) )){
Flag=true;break;
}
}
return Flag;
}
//判断蛇即将前进位置是否有豆子,有返回true,无返回false
public boolean isHaveBean(){
boolean Flag=false;
int x=snake_list.get(0).x;
int y=snake_list.get(0).y;
Point p=null;
if(Direction==1)p=new Point(x-1,y);
if(Direction==2)p=new Point(x+1,y);
if(Direction==3)p=new Point(x,y-1);
if(Direction==4)p=new Point(x,y+1);
if(bean.equals(p))Flag=true;
return Flag;
}
//前进一格
public void snake_move(){
if(isHaveBean()==true){//////////////有豆子吃
Point p=new Point(bean.x,bean.y);//【很重要,保证吃掉的是豆子的复制对象】
snake_list.add(0,p); //吃豆子
grid[p.x][p.y].setBackground(Color.blue);
this.Count++;
this.setTitle("总分:"+Count);
this.rand_bean(); //再产生一个豆子
}else{///////////////////无豆子吃
//取原蛇头坐标
int x=snake_list.get(0).x;
int y=snake_list.get(0).y;
//根据蛇头坐标推算出拟新增蛇头坐标
Point p=null;
if(Direction==1)p=new Point(x-1,y);//计算出向上的新坐标
if(Direction==2)p=new Point(x+1,y);//计算出向下的新坐标
if(Direction==3)p=new Point(x,y-1);//计算出向左的新坐标
if(Direction==4)p=new Point(x,y+1);//计算出向右的新坐标
//若拟新增蛇头碰壁,或缠绕则游戏结束
if(p.x0||p.x19|| p.y0||p.y19||is_cross(p)==true){
JOptionPane.showMessageDialog(null, "游戏结束!");
System.exit(0);
}
//向蛇体增加新的蛇头坐标,并使新蛇头可见
snake_list.add(0,p);
grid[p.x][p.y].setVisible(true);
//删除原蛇尾坐标,使蛇尾不可见
int x1=snake_list.get(snake_list.size()-1).x;
int y1=snake_list.get(snake_list.size()-1).y;
grid[x1][y1].setVisible(false);
snake_list.remove(snake_list.size()-1);
}
}
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_UP Direction!=2) Direction=1;
if(e.getKeyCode()==KeyEvent.VK_DOWN Direction!=1) Direction=2;
if(e.getKeyCode()==KeyEvent.VK_LEFT Direction!=4) Direction=3;
if(e.getKeyCode()==KeyEvent.VK_RIGHT Direction!=3) Direction=4;
}
@Override
public void keyReleased(KeyEvent e) { }
@Override
public void keyTyped(KeyEvent e) { }
public static void main(String[] args) throws InterruptedException {
Snake win=new Snake();
while(true){
win.snake_move();
Thread.sleep(300);
}
}
}
package soyea.base.util;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import soyea.base.bean.physics.ChargeSite;
/**
* 反射类,获取对象属性值,设置对象属性值等操作
*/
public class ClassReflect {
/**
* 用于获取指定对象不确定属性名的值 反射机制
* @param object 实体对象
* @param str 属性名
* @return 属性值
* @throws Exception
*/
public static Object get(Object object,String str) throws Exception {
Field field = hasEqualsIgnoreCase(object.getClass(),str);
if(field != null){
Method m = object.getClass().getMethod("get"+getMethodName(str));
Object invoke = m.invoke(object);
if(invoke == null){
return null;
}else{
return invoke.toString();
}
}else{
return null;
}
}
/**
* 设置对象属性
* @param object
* @param str
* @return
* @throws Exception
*/
public static void set(Object object,String property,Object value) throws Exception {
Field field = hasEqualsIgnoreCase(object.getClass(),property) ;
if(field != null){
Method m = object.getClass().getDeclaredMethod("set"+getMethodName(property),field.getType());
if(value!=null){
m.invoke(object,value);
}
}
}
/**
* @param clz 类文件
* @param str 类中的属性名
* @return 属性在liest中的下标
* @throws Exception
*/
public static Field hasEqualsIgnoreCase(Class? clz,String str) throws Exception{
ListField fields = Arrays.asList(clz.getDeclaredFields());
for(int i=0;ifields.size();i++){
Field x = fields.get(i);
if(str.equalsIgnoreCase(x.getName().toString())){
return x;
}
}
return null;
}
/**
* 大写第一个字母
* @param str 字段名
* @return
* @throws Exception
*/
public static String getMethodName(String str) throws Exception{
byte[] b = str.getBytes();
b[0]=(byte)((char)b[0]-'a'+'A');
return new String(b);
}
public static void resetObj(Object obj, String property)
throws Exception {
String gbk = Encoder.getByCode(obj, property);
if(gbk != null gbk.trim().length()0){
try {
ClassReflect.set(obj, property, gbk);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void resetAllObjPropertiesByCode(Object obj)
throws Exception {
Field[] fields = obj.getClass().getDeclaredFields();
for(int i=0;ifields.length;i++){
Field f = fields[i];
String name = f.getName();
if("id".equals(name)){
continue;
}
resetObj(obj, name);
}
}
}
优秀的JAVA程序员平常一天至少写150行代码,普通的JAVA程序员,平均一天的有效代码量大概是50~70行, 注意是有效代码。
延展回答:
JAVA程序员广义上是指一群以JAVA为谋生手段的软件开发人员。狭义的说,是指拥有SUN公司JAVA认证的程序员。Sun Java认证分为两个级别:Sun 认证Java程序员和Sun 认证Java开发员。通常要求程序员精通java基础,java高级编程,及常用java设计模式,并深入理解mvc编程模式,了解uml相关知识。
虽然JAVA人才的薪水很高,但是对该类人才需求旺盛的IT企业却很难招聘到合格的JAVA人员。其中,最根本的原因就是许多计算机专业的毕业生在读期间没有掌握实用的技能与经验,距离企业的实际用人需求有较大的差距。因此,计算机专业的大学生欲成为Java程序员,最便捷的一条路就是参加以实战项目为主要教学方法的JAVA职业技能培训,从而有效地缩短同企业具体用人需求之间的差距。
Java平台以其移动性、安全性和开放性受到追捧。据IDC预计,自2001年起的其后5年内,采用Java的IT产品的价值将翻番,在2006年将达到4.53亿美元,年增长率为14.9%。截止到2003年5月,Java注册开发商超过300万人,对JRE(Java运行环境)的下载达7200万次。詹姆斯·戈士林博士预计在3~5年内Java技术开发商将发展到1000万。无线Java也在迅速攀升。
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Painter extends JFrame{
/**
*
*/
private static final long serialVersionUID = 8160427604782702376L;
CanvasPanel canvas = new CanvasPanel();;
public Painter() {
super("Star");
this.add(canvas);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public static void main(String[] args) {
new Painter();
}
}
class CanvasPanel extends JPanel implements ActionListener{
/**
*
*/
private static final long serialVersionUID = -4642528854538741028L;
private JButton[] btn = new JButton[4];
private String[] btn_name = {"+", "-", "R", "L"};
private int center_x = 200, center_y = 200, radius = 100, degree = 0;
public CanvasPanel() {
this.setPreferredSize(new Dimension(400, 500));
this.setLayout(null);
for(int i = 0; i 4; i++) {
btn[i] = new JButton(btn_name[i]);
btn[i].setBounds(160 + i * 60, 425, 50, 50);
btn[i].addActionListener(this);
this.add(btn[i]);
}
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
for(int i = 0; i 5; i++) {
g.drawLine( (int) (center_x + radius * Math.sin(Math.toRadians(degree + 72 * i))),
(int) (center_y - radius * Math.cos(Math.toRadians(degree + 72 * i))),
(int) (center_x + radius * Math.sin(Math.toRadians(degree + 72 * i + 144))),
(int) (center_y - radius * Math.cos(Math.toRadians(degree + 72 * i + 144))));
}
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getActionCommand() == "+") {
if(radius 200)
radius += 2;
repaint();
} else if(e.getActionCommand() == "-") {
if(radius 0)
radius -= 2;
repaint();
} else if(e.getActionCommand() == "R") {
degree = (degree + 2) % 360;
repaint();
} else if(e.getActionCommand() == "L") {
degree = (degree - 2) % 360;
repaint();
}
}
}
视具体代码情况而定,视是否引用外部包而定,视个人编程书写习惯而定。
具体的来说,Python可以将任意长的代码写在一行上(其实好像java也可以这么干)。
所以行数说明不了什么问题。
平均来看,Java要打100行的代码,Python大约需要50行代码左右。
另外Python在某些问题上,处理比Java要更消耗资源,不过Python用了很多多线程优化,所以说起来,单机的运行速度不相上下,但在服务器上运行就能看出来Java是有明显优势的。
200多行,程序功能是输入两个数字的英文单词(20以内),输出结果,这个结果也是一个英文(就是将结果转换成英文输出)
import java.util.Scanner;
public class test{
private static String temp, tempNumA, tempNumB;
private static int geWei, shiWei, baiWei, judgeA, judgeB;
private static int number(String inputNum) {
if (inputNum.equalsIgnoreCase("zero")) {
return 0;
} else if (inputNum.equalsIgnoreCase("one")) {
return 1;
} else if (inputNum.equalsIgnoreCase("two")) {
return 2;
} else if (inputNum.equalsIgnoreCase("three")) {
return 3;
} else if (inputNum.equalsIgnoreCase("four")) {
return 4;
} else if (inputNum.equalsIgnoreCase("five")) {
return 5;
} else if (inputNum.equalsIgnoreCase("six")) {
return 6;
} else if (inputNum.equalsIgnoreCase("seven")) {
return 7;
} else if (inputNum.equalsIgnoreCase("eigth")) {
return 8;
} else if (inputNum.equalsIgnoreCase("nine")) {
return 9;
} else if (inputNum.equalsIgnoreCase("ten")) {
return 10;
} else if (inputNum.equalsIgnoreCase("eleven")) {
return 11;
} else if (inputNum.equalsIgnoreCase("twelve")) {
return 12;
} else if (inputNum.equalsIgnoreCase("thirteen")) {
return 13;
} else if (inputNum.equalsIgnoreCase("fourteen")) {
return 14;
} else if (inputNum.equalsIgnoreCase("fifteen")) {
return 15;
} else if (inputNum.equalsIgnoreCase("sixteen")) {
return 16;
} else if (inputNum.equalsIgnoreCase("seventeen")) {
return 17;
} else if (inputNum.equalsIgnoreCase("eighteen")) {
return 18;
} else if (inputNum.equalsIgnoreCase("nineteen")) {
return 19;
} else if (inputNum.equalsIgnoreCase("twenty")) {
return 20;
} else {
return -1;
}
}
private static String lessTen(int num) {
switch (num) {
case 0:
temp = "zero";
break;
case 1:
temp = "one";
break;
case 2:
temp = "two";
break;
case 3:
temp = "three";
break;
case 4:
temp = "four";
break;
case 5:
temp = "five";
break;
case 6:
temp = "six";
break;
case 7:
temp = "seven";
break;
case 8:
temp = "eight";
break;
case 9:
temp = "nine";
break;
default:
temp = "erro";
break;
}
return temp;
}
private static String moreThanTen(int num) {
if (num 20) {
switch (num) {
case 10:
temp = "ten";
break;
case 11:
temp = "eleven";
break;
case 12:
temp = "twelve";
break;
case 13:
temp = "thirteen";
break;
case 14:
temp = "fourteen";
break;
case 15:
temp = "fifteen";
break;
case 16:
temp = "sixteen";
break;
case 17:
temp = "seventeen";
break;
case 18:
temp = "eighteen";
break;
case 19:
temp = "nineteen";
break;
}
} else if (num = 20) {
switch (num) {
case 20:
temp = "twenty";
break;
case 30:
temp = "thirty";
break;
case 40:
temp = "forty";
break;
case 50:
temp = "fifty";
break;
case 60:
temp = "sixty";
break;
case 70:
temp = "seventy";
break;
case 80:
temp = "eighty";
break;
case 90:
temp = "ninety";
break;
}
}
return temp;
}
private static void outputReult(int num) {
if (num = 0 num = 9) {
System.out.println(lessTen(num));
} else if (num = 10 num = 19) {
System.out.println(moreThanTen(num));
} else if (num = 20 num = 99) {
shiWei = num / 10;
geWei = num % 10;
if (geWei == 0) {
System.out.println(moreThanTen(shiWei * 10));
} else {
System.out.println(moreThanTen(shiWei * 10) + "-"
+ lessTen(geWei));
}
} else if (num = 100 num = 400) {
baiWei = num / 100;
shiWei = num % 100 / 10;
geWei = num % 100 % 10;
if (shiWei == 0 geWei == 0) {
System.out.println(lessTen(baiWei) + " " + "hundred");
} else if (shiWei == 0 geWei != 0) {
System.out.println(lessTen(baiWei) + " " + "hundred" + " "
+ "and" + " " + lessTen(geWei));
} else if (shiWei != 0 geWei == 0) {
System.out.println(lessTen(baiWei) + " " + "hundred" + " "
+ "and" + " " + moreThanTen(shiWei * 10 + geWei));
} else {
if (shiWei == 1 geWei != 0) {
System.out.println(lessTen(baiWei) + " " + "hundred" + " "
+ "and" + " " + moreThanTen(shiWei * 10 + geWei));
} else {
System.out.println(lessTen(baiWei) + " " + "hundred" + " "
+ "and" + " " + moreThanTen(shiWei * 10) + "-"
+ lessTen(geWei));
}
}
}
}
public static void inputNumber() {
System.out.print("请输入第一个数字(数字必须小于等于20)的单词:");
tempNumA = new Scanner(System.in).next();
judgeA = number(tempNumA);
System.out.print("请输入第二个数字(数字必须小于等于20)的单词:");
tempNumB = new Scanner(System.in).next();
judgeB = number(tempNumB);
if (judgeA == -1 || judgeB == -1) {
System.out.println("你输入的数字单词不正确,请重新输入!");
System.out.println(" ");
inputNumber();
}
}
public static void main(String[] args) {
inputNumber();
System.out.print(tempNumA + " multiply " + tempNumB + " equals ");
outputReult(number(tempNumA) * number(tempNumB));
}
}
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流