java基本編程思路:
成都創(chuàng)新互聯(lián)公司,專(zhuān)注為中小企業(yè)提供官網(wǎng)建設(shè)、營(yíng)銷(xiāo)型網(wǎng)站制作、成都響應(yīng)式網(wǎng)站建設(shè)、展示型成都網(wǎng)站制作、成都做網(wǎng)站等服務(wù),幫助中小企業(yè)通過(guò)網(wǎng)站體現(xiàn)價(jià)值、有效益。幫助企業(yè)快速建站、解決網(wǎng)站建設(shè)與網(wǎng)站營(yíng)銷(xiāo)推廣問(wèn)題。
第一步:寫(xiě)框架(內(nèi)層和外層):
public class?類(lèi)名{//外層
public static void main(String[ ] args){//內(nèi)層
}
}
第二步:看題目中是否有“接收”“輸入”“錄入”等字眼,則導(dǎo)入util包,創(chuàng)建Scanner掃描儀對(duì)象:
import??java.util.Scanner;//寫(xiě)在文件的第一行
Scanner??input = new Scanner(System.in);//寫(xiě)在main方法中的第一行
第三步:編寫(xiě)main方法中的主體代碼
A、接收、輸入、錄入:
System.out.println(“提示信息”);
數(shù)據(jù)類(lèi)型??變量名=__掃描數(shù)據(jù)___;
說(shuō)明:掃描不同類(lèi)型的數(shù)據(jù),使用不同的掃描方法。以下介紹了常用的類(lèi)型的掃描方法。
int型?????使用nextInt();
String型?????使用next ()或nextLine();
double型??????使用nextDouble();
B、計(jì)算:算術(shù)運(yùn)算等
C、輸出:
System.out.println(輸出的內(nèi)容);
或者
System.out.println(“提示內(nèi)容:”+輸出的內(nèi)容);
import java.awt.*;
import java.awt.event.*;
public class MoveExample
{
public static void main(String args[])
{
new Hua_Rong_Road();
}
}
class Person extends Button implements FocusListener
{
int number;
Color c = new Color(255,245,170);
Person(int number,String s)
{
super(s);
setBackground(c);
this.number = number;
c = getBackground();
addFocusListener(this);
}
public void focusGained(FocusEvent e)
{
setBackground(Color.red);
}
public void focusLost(FocusEvent e)
{
setBackground(c);
}
}
class Hua_Rong_Road extends Frame implements MouseListener,KeyListener,ActionListener
{
Person person[] = new Person[10];
Button left,right,above,below;
Button restart = new Button("重新開(kāi)始");
public Hua_Rong_Road()
{
init();
setBounds(100,100,320,360);
setVisible(true);
validate();
addWindowListener( new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
}
public void init()
{
setLayout(null);
add(restart);
restart.setBounds(100,320,120,25);
restart.addActionListener(this);
String name[] = {"曹操","關(guān)羽","張飛","劉備","趙云","黃忠","兵","兵","兵","兵"};
for(int k = 0;kname.length;k++)
{
person[k] = new Person(k,name[k]);
person[k].addMouseListener(this);
person[k].addKeyListener(this);
add(person[k]);
}
person[0].setBounds(104,54,100,100);
person[1].setBounds(104,154,100,50);
person[2].setBounds(54,154,50,100);
person[3].setBounds(204,154,50,100);
person[4].setBounds(54,54,50,100);
person[5].setBounds(204,54,50,100);
person[6].setBounds(54,254,50,50);
person[7].setBounds(204,254,50,50);
person[8].setBounds(104,204,50,50);
person[9].setBounds(154,204,50,50);
person[9].requestFocus();
left = new Button();
right = new Button();
above = new Button();
below = new Button();
add(left);
add(right);
add(above);
add(below);
left.setBounds(49,49,5,260);
right.setBounds(254,49,5,260);
above.setBounds(49,49,210,5);
below.setBounds(49,304,210,5);
validate();
}
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){}
public void keyPressed(KeyEvent e)
{
Person man = (Person)e.getSource();
if(e.getKeyCode()==KeyEvent.VK_DOWN)
{
go(man,below);
}
if(e.getKeyCode()==KeyEvent.VK_UP)
{
go(man,above);
}
if(e.getKeyCode()==KeyEvent.VK_LEFT)
{
go(man,left);
}
if(e.getKeyCode()==KeyEvent.VK_RIGHT)
{
go(man,right);
}
}
public void mousePressed(MouseEvent e)
{
Person man = (Person)e.getSource();
int x = -1,y = -1;
x = e.getX();
y = e.getY();
int w = man.getBounds().width;
int h = man.getBounds().height;
if(yh/2)
{
go(man,below);
}
if(yh/2)
{
go(man,above);
}
if(xw/2)
{
go(man,left);
}
if(xw/2)
{
go(man,right);
}
}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void go(Person man,Button direction)
{
boolean move = true;
Rectangle manRect = man.getBounds(); //什么意思??
int x = man.getBounds().x; //又不懂了
int y = man.getBounds().y;
if(direction==below)
{
y = y+50;
}
else if(direction==above)
{
y = y-50;
}
else if(direction==left)
{
x = x-50;
}
else if(direction==right)
{
x = x+50;
}
manRect.setLocation(x,y);
Rectangle directionRect = direction.getBounds();
for(int k = 0;k10;k++)
{
Rectangle personRect = person[k].getBounds();
if((manRect.intersects(personRect))(man.number!=k))
{
move = false;
}
}
if(manRect.intersects(directionRect))
{
move = false;
}
if(move==true)
{
man.setLocation(x,y);
}
}
public void actionPerformed(ActionEvent e)
{
dispose();
new Hua_Rong_Road();
}
}
這是我們課本上的,顏色不一樣,其他的都差不多,不過(guò)是用awt組件寫(xiě)的,你應(yīng)該是要用swing寫(xiě)的吧,照這個(gè)改改吧...
import java.awt.*;
import java.awt.event.*;
public class MoveExample //主類(lèi)
{
public static void main(String args[]) //定義主方法
{
new Hua_Rong_Road(); //創(chuàng)建對(duì)象
}
}
class Person extends Button implements FocusListener
{
int number;
Color c = new Color(128,128,128);
Person(int number,String s)//構(gòu)造方法
{
super(s);//調(diào)用父類(lèi)s的構(gòu)造方法
setBackground(c);//設(shè)置組件的背景色
this.number = number;//調(diào)用當(dāng)前的number
c = getBackground();
addFocusListener(this);//添加焦點(diǎn)事件監(jiān)聽(tīng)器
}
public void focusGained(FocusEvent e)//焦點(diǎn)事件觸發(fā)
{
setBackground(Color.blue);
}
public void focusLost(FocusEvent e)//焦點(diǎn)事件失去
{
setBackground(c);
}
}
class Hua_Rong_Road extends Frame implements MouseListener,KeyListener,ActionListener
{
Person person[] = new Person[10];//person類(lèi)的數(shù)組
Button left,right,above,below;
Button restart = new Button("重新開(kāi)始");
public Hua_Rong_Road() //華容道的構(gòu)造方法
{
init(); //初始化
setBounds(100,100,320,360);//設(shè)置窗口在屏幕上出現(xiàn)位置,和窗口大小
setVisible(true);//設(shè)置窗口可見(jiàn)
setResizable(true);//設(shè)置窗口可調(diào)節(jié)
validate();//刷新
addWindowListener( new WindowAdapter()//獲得窗口事件監(jiān)視器
{
public void windowClosing(WindowEvent e)//窗口正在被關(guān)閉時(shí),窗口監(jiān)視器調(diào)用該方法
{
System.exit(0);
}
}
);
}
public void init()
{
setLayout(null);//設(shè)置默認(rèn)布局
add(restart);//添加重新開(kāi)始
restart.setBounds(100,320,120,25);//重新開(kāi)始按鈕大小
restart.addActionListener(this);//事件源獲得監(jiān)視器
String name[] = {"曹操","關(guān)羽","張飛","劉備","趙云","黃忠","兵","兵","兵","兵"};
for(int k = 0;kname.length;k++)
{
person[k] = new Person(k,name[k]);//給按鈕添加名字
person[k].addMouseListener(this);//每個(gè)按鈕都注冊(cè)鼠標(biāo)事件
person[k].addKeyListener(this);//每個(gè)按鈕都注冊(cè)鍵盤(pán)事件
add(person[k]);//添加人物
}
person[0].setBounds(104,54,100,100);
person[1].setBounds(104,154,100,50);
person[2].setBounds(54,154,50,100);
person[3].setBounds(204,154,50,100);
person[4].setBounds(54,54,50,100);
person[5].setBounds(204,54,50,100);
person[6].setBounds(54,254,50,50);
person[7].setBounds(204,254,50,50);
person[8].setBounds(104,204,50,50);
person[9].setBounds(154,204,50,50);//為每個(gè)人物按鈕設(shè)置位置和大小
person[9].requestFocus();//把焦點(diǎn)先設(shè)置在這個(gè)按鈕上
left = new Button();//畫(huà)出游戲界面邊框,并用定義的left,right,above,below控制大小
right = new Button();
above = new Button();
below = new Button();
add(left);
add(right);
add(above);
add(below);
left.setBounds(49,49,5,260);
right.setBounds(254,49,5,260);
above.setBounds(49,49,210,5);
below.setBounds(49,304,210,5);
validate();//刷新
} //完成界面布局
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){}
public void keyPressed(KeyEvent e)//響應(yīng)鍵盤(pán)事件,按鍵,釋放鍵,按下和釋放組合
{
Person man = (Person)e.getSource();//獲得事件源
if(e.getKeyCode()==KeyEvent.VK_DOWN)//響應(yīng)用戶(hù)按下方向光標(biāo)的操作;用KeyEvent類(lèi)中的getkeycode()判斷哪個(gè)鍵被按下
{
go(man,below); //go方法控制移動(dòng)
}
if(e.getKeyCode()==KeyEvent.VK_UP)
{
go(man,above);
}
if(e.getKeyCode()==KeyEvent.VK_LEFT)
{
go(man,left);
}
if(e.getKeyCode()==KeyEvent.VK_RIGHT)
{
go(man,right);
}
}
public void mousePressed(MouseEvent e)
{
Person man = (Person)e.getSource();
int x = -1,y = -1;
x = e.getX();
y = e.getY();
int w = man.getBounds().width;
int h = man.getBounds().height;
if(yh/2)
{
go(man,below);
}
if(yh/2)
{
go(man,above);
}
if(xw/2)
{
go(man,left);
}
if(xw/2)
{
go(man,right);
}
}
public void mouseReleased(MouseEvent e){}//鼠標(biāo)事件
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void go(Person man,Button direction)
{
boolean move = true;
Rectangle manRect = man.getBounds();
int x = man.getBounds().x;
int y = man.getBounds().y;
if(direction==below)//向各個(gè)方向移動(dòng)
{
y = y+50;
}
else if(direction==above)
{
y = y-50;
}
else if(direction==left)
{
x = x-50;
}
else if(direction==right)
{
x = x+50;
}
manRect.setLocation(x,y);
Rectangle directionRect = direction.getBounds();
for(int k = 0;k10;k++)
{
Rectangle personRect = person[k].getBounds();
if((manRect.intersects(personRect))(man.number!=k))//如果覆蓋就不移動(dòng)
{
move = false;
}
}
if(manRect.intersects(directionRect))
{
move = false;
}
if(move==true)
{
man.setLocation(x,y);
}
}
public void actionPerformed(ActionEvent e)
{
dispose();
new Hua_Rong_Road();
}
}
當(dāng)前標(biāo)題:java華容道代碼的思路 數(shù)字華容道java代碼
分享路徑:http://jinyejixie.com/article14/dosdcge.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、軟件開(kāi)發(fā)、做網(wǎng)站、面包屑導(dǎo)航、網(wǎng)站改版、小程序開(kāi)發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)