本文目錄一覽:
- 1、java新手漢諾塔問題最終的輸出結果如下(問題規模為3),其中盤子的編號是漢諾塔問題中從上到下的順序。
- 2、求一個漢諾塔的JAVA代碼
- 3、java實現漢諾塔的代碼,求注釋,具體到每一行代碼,急求,,,
- 4、JAVA漢諾塔
- 5、Java漢諾塔的問題
- 6、怎樣用Java編寫漢諾塔程序
java新手漢諾塔問題最終的輸出結果如下(問題規模為3),其中盤子的編號是漢諾塔問題中從上到下的順序。
try..catch用於捕獲initProperties()和init()這兩個方法執行時候的異常,至於initProperties()和init()這兩個方法為何會出異常,就看你具體的業務邏輯了。
try中的代碼塊兒如果一旦出現異常則在catch中進行捕獲,showMessageDialog是JOptionPane封裝或者說提供的一個顯示一個帶有確定按鈕的模態對話框的方法。
幾個參數中
“Failed to init.\n” + ex 也就是第二個參數是消息框彈出消息的內容。
“BinaryRain 也就是第三個參數是消息框的標題。
JOptionPane.ERROR_MESSAGE 也就是第四個參數,是消息框的類型,表示錯誤提示,彈出來的消息框有個紅色的叉叉標識,如JOptionPane.WARNING_MESSAGE,表示警告提示,彈出來的消息框有個嘆號標識。
求一個漢諾塔的JAVA代碼
public class Hannoi {
public static void main(String[] args) {
int nDisks = 3;
moveDish(nDisks, ‘A’, ‘B’, ‘C’);
}
private static void moveDish(int level, char from, char inter, char to) {
if (level == 1) {
System.out.println(“從” + from + “移動盤子 ” + level + ” 號到” + to);
} else {
moveDish(level – 1, from, to, inter);
System.out.println(“從” + from + “移動盤子 ” + level + ” 號到” + to);
moveDish(level – 1, inter, from, to);
}
}
}
java實現漢諾塔的代碼,求注釋,具體到每一行代碼,急求,,,
這樣應該可以了
如果還有那個地方不懂的,建議你研究下漢諾塔演算法
import
java.io.BufferedReader;//引入IO包中的BufferedReader
import
java.io.IOException;//引入IO包中的IO異常處理
import
java.io.InputStreamReader;//引入IO包中的InputStreaReader
public
class
Hinoi
{
//主類
static
int
m=0;//定義移動的次數
//主程序入口–main方法
public
static
void
main(String[]
args)
{
//創建BufferedReader對象,InputStream輸入流
BufferedReader
bf
=
new
BufferedReader(new
InputStreamReader(System.in));
System.out.println(“請輸入盤子的個數:”);
try
{
int
sl
=
Integer.parseInt(bf.readLine().toString());//接收總盤子個數
toMove(sl,”A”,”B”,”C”);//調用移動方法
A–C
}
catch
(NumberFormatException
e)
{捕獲NumberFormatException異常
//
TODO
Auto-generated
catch
block
e.printStackTrace();//列印異常
}
catch
(IOException
e)
{//捕獲IOException異常
//
TODO
Auto-generated
catch
block
e.printStackTrace();//列印異常
}
System.out.println(“總共移動了:”+m+”
次數”);//列印移動次數
}
//移動方法
private
static
void
toMove(int
sl,
String
one,
String
two,String
three)
{
if(sl==1){//如果只有一個盤子,則直接移動到C柱
System.out.println(“盤子”+sl+”
從
“+one+”—-“+three);
}else{//如果總盤數大於1,則遞歸調用移動方法
//把所有的數量為sl-1的盤子全部從A移到到B(C作為一個過渡),好提供一個最下面的位置給最大盤子到C;
toMove(sl-1,one,three,two);
System.out.println(“盤子”+sl+”
從
“+one+”—-“+three);
//把所有的剩餘的盤子從B移動到C(A作為一個過渡)
toMove(sl-1,two,one,three);
}
m++;
}
}
JAVA漢諾塔
import java.awt.*;
public class TowerPoint //公共類TowerPoint
{
int x,y; //定義2個int類型的變數
boolean 有盤子; //定義一個boolean類型的變數
Disk 盤子=null; //初始化一個對象”盤子”並賦值為空
HannoiTower con=null; //初始化一個HannoiTower類的對象”con”並賦值為空
public TowerPoint(int x,int y,boolean boo) //構造函數,有3個參數,x,y,boo
{
this.x=x; //將參數賦給當前x
this.y=y; //將參數賦給當前y
有盤子=boo; //將boo賦給”有盤子”
}
public boolean 是否有盤子() //定義一個返回boolean類型的方法”是否有盤子”
{
return 有盤子; //返回boolean類型的”有盤子”
}
public void set有盤子(boolean boo) //set方法,並且參數為boolean
{
有盤子=boo; //將boo賦給有盤子
}
public int getX() //取得x方法
{
return x; //返回x
}
public int getY()//取得y方法
{
return y; //返回y
}
public void 放置盤子(Disk 盤子,HannoiTower con) //定義一個有2個參數的”放置盤子”方法。參數是Disk類和HannoiTower類
{
this.con=con; //當前con等於參數con
con.setLayout(null); //調用on對象的方法setLayout,並設置為空
this.盤子=盤子; //當前盤子等於參數盤子
con.add(盤子); //con對象的add方法,加入”盤子”對象
int w=盤子.getBounds().width; //定義並給一個int類型的w變數一個值,值為”盤子.getBounds().width”
int h=盤子.getBounds().height; //定義並給一個int類型的h變數一個值,值為”盤子.getBounds().height”
盤子.setBounds(x-w/2,y-h/2,w,h);//調用”盤子”對象的setBounds方法,並把傳遞值
有盤子=true;//boolean類型的對象”有盤子”等於true
con.validate(); //調用con對象的validate方法
}
public Disk 獲取盤子() //定義”獲取盤子”方法,方法返回Disk對象
{
return 盤子; //返回盤子
}
}
———————–另外說一下,樓主太摳門了!!!!!!!!只給5分———————–
Java漢諾塔的問題
import java.util.LinkedList;
public class Hanoi {
private int num;
private Peg[] pegs = new Peg[3];
public Hanoi(int num) {
this.num = num;
pegs[0] = new Peg(“A”);
pegs[1] = new Peg(“B”);
pegs[2] = new Peg(“C”);
for (int i = num; i 0; i–) {
pegs[0].pushDown(new Plate(i));
}
}
public void start() {
multiMove(pegs[0], pegs[2], pegs[1], num);
}
/**
* Move a given number plates from one peg to another
* @param from from this peg
* @param to to this peg
* @param temp the temp peg to use
* @param count how many plates to move
*/
public void multiMove(Peg from, Peg to, Peg temp, int count) {
if (count == 1) {
move(from, to);
return;
}
multiMove(from, temp, to, count – 1);
move(from, to);
multiMove(temp, to, from, count – 1);
}
public void move(Peg from, Peg to) {
Plate p = from.popUp();
to.pushDown(p);
System.out.print(“Moving ” + p.size + ” from ” + from.name + ” to ” + to.name + ” “);
printPegs();
}
public void printPegs() {
System.out.println(pegs[0].name + “: ” + pegs[0] +
” ” + pegs[1].name + “: ” + pegs[1] +
” ” + pegs[2].name + “: ” + pegs[2]);
}
public static class Plate {
int size;
Plate(int size) {
this.size = size;
}
@Override
public String toString() {
return String.valueOf(size);
}
}
public static class Peg {
LinkedListPlate plateList = new LinkedList();
String name;
public Peg(String name) {
this.name = name;
}
public void pushDown(Plate p) {
plateList.push(p);
}
public Plate popUp() {
return plateList.pop();
}
@Override
public String toString() {
return plateList.toString();
}
}
public static void main(String[] args) {
Hanoi game = new Hanoi(3);
game.printPegs();
game.start();
}
}
怎樣用Java編寫漢諾塔程序
package Hanoi;
import java.awt.*;
import java.io.*;
import java.awt.event.*;
public class Hanoi {
public static void main(String args[]) throws IOException {
Hanoi aa = new Hanoi();
aa.go();
}
public void go() throws IOException {
int n;
BufferedReader buf;
buf = new BufferedReader(new InputStreamReader(System.in));
System.out.print(“請輸入盤數:”);
n = Integer.parseInt(buf.readLine());
Hanoi hanoi = new Hanoi();
hanoi.move(n, ‘A’, ‘B’, ‘C’);
}
public void move(int n, char a, char b, char c) {
if (n == 1) {
System.out.println(“盤 ” + n + ” 由 ” + a + ” 移至 ” + c);
} else {
move(n – 1, a, c, b);
System.out.println(“盤 ” + n + ” 由 ” + a + ” 移至 ” + c);
move(n – 1, b, a, c);
}
}
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/312972.html