求个简单点的Java程序 100行左右。 需要解释。
发布网友
发布时间:2022-04-21 15:24
我来回答
共3个回答
热心网友
时间:2023-05-25 02:52
贪吃蛇游戏 望采纳
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];
ArrayList<Point> snake_list=new ArrayList<Point>();
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;i<20;i++)
for(int j=0;j<20;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;i<snake_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.x<0||p.x>19|| p.y<0||p.y>19||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);
}
}
}追问太深奥了 老师一看就是假的 ,,望弄一个简单点
追答并不深奥 你仔细看看 其实还是挺简单的 我这没有比这更简单的、100行以上的代码,将就着用吧,100行,不实现些什么功能,说不过去
热心网友
时间:2023-05-25 02:53
public class byyourself{
public static void main(String[] args){
for(int 1=0;i<100;i++){
System.out.println(i);
}
}
}
追问太少了
热心网友
时间:2023-05-25 02:53
XSSFWorkbook xwb = new XSSFWorkbook(excel);
XSSFSheet sheet = xwb.getSheetAt(0);
for (int i = sheet.getFirstRowNum() + 1; i <= sheet.getPhysicalNumberOfRows(); i++) {
XSSFRow row = sheet.getRow(i);
if (row != null) {
//metaID
XSSFCell cell0 = row.getCell(0);
if (null == cell0) {
throw new Exception("唯一标识MetaID不能为空!");
}
// 中文标题
XSSFCell cell1 = row.getCell(1);
if (null == cell1) {
throw new Exception("标题不能为空!");
}
// StatutesTitle
XSSFCell cell2 = row.getCell(2);
if (null == cell2) {
throw new Exception("StatutesTitle不能为空!");
}
//StatutesTitle
XSSFCell cell3 = row.getCell(3);
if (null == cell3) {
throw new Exception("法律部门不能为空!");
}
//LegalDepartment
XSSFCell cell4 = row.getCell(4);
if (null == cell4) {
throw new Exception("LegalDepartment不能为空!");
}
//发布部门英文
XSSFCell cell5 = row.getCell(5);
if (null == cell5) {
throw new Exception("发布部门不能为空!");
}
//IssuingDepartment
XSSFCell cell6 = row.getCell(6);
if (null == cell6) {
throw new Exception("IssuingDepartment不能为空!");
}
//发布机构类型
XSSFCell cell7 = row.getCell(7);
if (null == cell7) {
throw new Exception("发布机构类型不能为空!");
}
//IssuingOrganizationType
XSSFCell cell8 = row.getCell(8);
if (null == cell8) {
throw new Exception("IssuingOrganizationType不能为空!");
}
//批准部门
XSSFCell cell9 = row.getCell(9);
//ApprovalDepartment
XSSFCell cell10 = row.getCell(10);
//通过/批准日期
XSSFCell cell11 = row.getCell(11);
//ApprovalDate
XSSFCell cell12 = row.getCell(12);
//发布文号
//TODO 有则必填??
XSSFCell cell13 = row.getCell(13);
//DispatchNo
XSSFCell cell14 = row.getCell(14);
//公布/发布日期
XSSFCell cell15 = row.getCell(15);
if (null == cell15) {
throw new Exception("公布/发布日期不能为空!");
}
//ReleaseDate
XSSFCell cell16 = row.getCell(16);
if (null == cell16) {
throw new Exception("ReleaseDate不能为空!");
}
//施行日期
XSSFCell cell17 = row.getCell(17);
if (null == cell17) {
throw new Exception("施行日期不能为空!");
}
//ImplementationDate
XSSFCell cell18 = row.getCell(18);
if (null == cell18) {
throw new Exception("ImplementationDate不能为空!");
}
//废止日期
XSSFCell cell19 = row.getCell(19);
//AbolitionDate
XSSFCell cell20 = row.getCell(20);
//历史沿革
XSSFCell cell21 = row.getCell(21);
//HistoricalDevelopment
XSSFCell cell22 = row.getCell(22);
//时效性
XSSFCell cell23 = row.getCell(23);
if (null == cell23) {
throw new Exception("时效性不能为空!");
}
//Timeliness
XSSFCell cell24 = row.getCell(24);
if (null == cell24) {
throw new Exception("Timeliness不能为空!");
}
//效力级别
XSSFCell cell25 = row.getCell(25);
if (null == cell25) {
throw new Exception("效力级别不能为空!");
}
//EffectivenessGrade
XSSFCell cell26 = row.getCell(26);
if (null == cell26) {
throw new Exception("EffectivenessGrade不能为空!");
}
//来源
XSSFCell cell27 = row.getCell(27);
if (null == cell27) {
throw new Exception("来源不能为空!");
}
//SourceName
XSSFCell cell28 = row.getCell(28);
if (null == cell28) {
throw new Exception("SourceName不能为空!");
}
//地域
XSSFCell cell29 = row.getCell(29);
if (null == cell29) {
throw new Exception("地域不能为空!");
}
//Areas
XSSFCell cell30 = row.getCell(30);
if (null == cell30) {
throw new Exception("Areas不能为空!");
}
//年度
XSSFCell cell31 = row.getCell(31);
if (null == cell31) {
throw new Exception("年度不能为空!");
}
//Year
XSSFCell cell32 = row.getCell(32);
if (null == cell32) {
throw new Exception("Year不能为空!");
}
//主题分类
XSSFCell cell33 = row.getCell(33);
if (null == cell33) {
throw new Exception("主题分类不能为空!");
}
//InstryClassification
XSSFCell cell34 = row.getCell(34);
if (null == cell34) {
throw new Exception("InstryClassification不能为空!");
}
//学科分类
XSSFCell cell35 = row.getCell(35);
if (null == cell35) {
throw new Exception("学科分类不能为空!");
}
//SubjectCategory
XSSFCell cell36 = row.getCell(36);
if (null == cell36) {
throw new Exception("SubjectCategory不能为空!");
}
//中图法分类
XSSFCell cell37 = row.getCell(37);
if (null == cell37) {
throw new Exception("中图法分类不能为空!");
}
//CLC
XSSFCell cell38 = row.getCell(38);
if (null == cell38) {
throw new Exception("CLC不能为空!");
}
//案由
XSSFCell cell39 = row.getCell(39);
//CauseOfAction
XSSFCell cell40 = row.getCell(40);
//程序
XSSFCell cell41 = row.getCell(41);
if (null == cell41) {
throw new Exception("程序不能为空!");
}
//Procere
XSSFCell cell42 = row.getCell(42);
if (null == cell42) {
throw new Exception("Procere不能为空!");
}
//编辑提示
XSSFCell cell43 = row.getCell(43);
//EditPrompt
XSSFCell cell44 = row.getCell(44);
//字数
XSSFCell cell45 = row.getCell(45);
//Words
XSSFCell cell46 = row.getCell(46);
//试读比例(字数)
XSSFCell cell47 = row.getCell(47);
if (null == cell47) {
throw new Exception("试读比例(字数)不能为空!");
}
//Previews
XSSFCell cell48 = row.getCell(48);
if (null == cell48) {
throw new Exception("Previews不能为空!");
}
//文件路径
XSSFCell cell49 = row.getCell(49);
if (null == cell49) {
throw new Exception("文件路径不能为空!");
}
//PathFile
XSSFCell cell50 = row.getCell(50);
if (null == cell50) {
throw new Exception("PathFile不能为空!");
}
//资源类型
XSSFCell cell51 = row.getCell(51);
if (null == cell51) {
throw new Exception("资源类型不能为空!");
}
//ResourceType
XSSFCell cell52 = row.getCell(52);
if (null == cell52) {
throw new Exception("ResourceType不能为空!");
}
//价格
XSSFCell cell53 = row.getCell(53);
if (null == cell53) {
throw new Exception("价格不能为空!");
}
//Eprice
XSSFCell cell54 = row.getCell(54);
if (null == cell54) {
throw new Exception("Eprice不能为空!");
}
String metaID = cell0.toString();
String statutesTitle = cell1.toString();
String legalDepartment = cell3.toString();
String issuingDepartment = cell5.toString();
String issuingOrganizationType = cell7.toString();
String approvalDepartment = cell9.toString();
String approvalDepartmentEn = cell10.toString();
String approvalDate = cell11.toString();
String approvalDateEn = cell12.toString();
String dispatchNo = cell13.toString();
String dispatchNoEn = cell14.toString();
String releaseDate = cell15.toString();
String releaseDateEn = cell16.toString();
String implementationDate = cell17.toString();
String implementationDateEn = cell18.toString();
String abolitionDate = "";
String abolitionDateEn = "";
if(cell19 != null){
abolitionDate = cell19.toString();
}
if(cell20 != null){
abolitionDateEn = cell20.toString();
}
String historicalDevelopment = "";
String historicalDevelopmentEn = "";
if(cell21!= null){
historicalDevelopment = cell21.toString();
}
if(cell22 != null){
historicalDevelopmentEn = cell22.toString();
}
String timeliness = cell23.toString();
String effectivenessGrade = cell25.toString();
String sourceName = "";
String sourceNameEn = "";
if(cell25 != null){
sourceName = cell25.toString();
}
if(cell26 != null){
sourceNameEn = cell26.toString();
}
String areas = cell27.toString();
String year = cell29.toString();
String subjectClassify = cell31.toString();
String instryClassification = cell33.toString();
String subjectCategory = cell35.toString();
String clc = cell37.toString();
String causeOfAction = "";
String causeOfActionEn = "";
if(cell39 != null){
causeOfAction = cell39.toString();
}
if(cell40 != null){
causeOfActionEn = cell40.toString();
}
String procere = cell41.toString();
String editPrompt = "";
String editPromptEn = "";
if(cell43 != null){
editPrompt = cell43.toString();
}
if(cell44 != null){
editPromptEn = cell44.toString();
}
String words = "";
String wordsEn = "";
if(cell45 != null){
words = cell45.toString();
}
if(cell46 != null){
wordsEn = cell46.toString();
}
String preViews = cell47.toString();
String pathFile = cell49.toString();
String resourceType = cell51.toString();
String eprice = cell53.toString();
String epriceEn = cell54.toString();
//给入库赋值
storage.setProctMetaId(metaID);
storage.setProctTitle(statutesTitle);
storage.setProctTitleEn(statutesTitle);
storage.setProctIssuingDepart(issuingDepartment);
storage.setProctIssuingDepartEn(issuingDepartment);
storage.setProctLegalDepartment(legalDepartment);
storage.setProctLegalDepartmentEn(legalDepartment);
storage.setProctIssuingOType(issuingOrganizationType);
storage.setProctIssuingOTypeEn(issuingOrganizationType);
storage.setProctApprovalDepart(approvalDepartment);
storage.setProctApprovalDeparttEn(approvalDepartmentEn);
storage.setProctApprovalDate(approvalDate);
storage.setProctApprovalDateEn(approvalDateEn);
storage.setProctDispatchNo(dispatchNo);
storage.setProctDispatchNoEn(dispatchNoEn);
storage.setProctReleaseDate(releaseDate);
storage.setProctReleaseDateEn(releaseDateEn);
storage.setProctImplementDate(implementationDate);
storage.setProctImplementDateEn(implementationDateEn);
storage.setProctAbolitionDate(abolitionDate);
storage.setProctAbolitionDateEn(abolitionDateEn);
storage.setProctHistoricalDevelop(historicalDevelopment);
storage.setProctHistoricalDevelopEn(historicalDevelopmentEn);
storage.setProctTimeliness(timeliness);
storage.setProctTimelinessEn(timeliness);
storage.setProctEffectivenessGrade(effectivenessGrade);
storage.setProctEffectivenessGradeEn(effectivenessGrade);
storage.setProctSourceName(sourceName);
storage.setProctSourceNameEn(sourceNameEn);
storage.setProctAreas(areas);
storage.setProctAreasEn(areas);
storage.setProctYear(year);
storage.setProctYearEn(year);
storage.setProctSubjectClass(subjectClassify);
storage.setProctSubjectClassEn(subjectClassify);
storage.setProctInstryClassifica(instryClassification);
storage.setProctInstryClassificaEn(instryClassification);
storage.setProctSubjectCategory(subjectCategory);
storage.setProctSubjectCategoryEn(subjectCategory);
storage.setProctCLC(clc);
storage.setProctCLCEn(clc);
storage.setProctCauseOfAction(causeOfAction);
storage.setProctCauseOfActionEn(causeOfActionEn);
storage.setProctProcere(procere);
storage.setProctProcereEn(procere);
storage.setProctEditPrompt(editPrompt);
storage.setProctEditPromptEn(editPromptEn);
storage.setProctWords(words);
storage.setProctWordsEn(wordsEn);
storage.setProctPreviews(preViews);
storage.setProctPreviewsEn(preViews);
storage.setProctPathFile(pathFile);
storage.setProctPathFileEn(pathFile);
storage.setProctResourceType(resourceType);
storage.setProctResourceTypeEn(resourceType);
storage.setProctEprice(eprice);
storage.setProctEpriceEn(epriceEn);
this.save(storage);
}
}追问请问这是?做什么的