PasteSite is open to the public, but with limited features. Register to be able to modify access rights, track your pastes and more...
If you prefer reading light text on a dark background to dark text on a light background, then you might want to try the dark theme.
"Re: #18562" by Anonymous [Java]Actions: |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
package lab2; import java.util.Scanner; /** * * @author delger42 */ public class TowersOfHanoi { static void myHanoi(int disks, char fromPeg, char toPeg, char tempPeg) { if (disks >= 1) { myHanoi(disks - 1, fromPeg, tempPeg, toPeg); moveMyDisk(disks,fromPeg, toPeg); myHanoi(disks - 1, tempPeg, toPeg, fromPeg); } } static void moveMyDisk(int disks,char fromPeg, char toPeg) { int action = 0; action++; int diskNum = disks; System.out.print(disks+ " " + fromPeg + " to " + toPeg + ". "); if (action % 1 == 0) { System.out.println(); } } public class Disk { private int diskNumber; public Disk(int disk) { diskNumber = disk; } public Disk() { diskNumber = 0; } } public class Peg { private char pegChar; public Peg(char peg) { pegChar = peg; } public Peg() { pegChar = ' '; } } public static void main(String[] args) { int disks; char FromPeg = 'A'; char ToPeg='C' ; char TempPeg ='B'; System.out.println("How many disk you want to play with?"); Scanner MyInputDisks = new Scanner(System.in); disks = MyInputDisks.nextInt(); System.out.println("If " + disks + " is input then output should be:"); System.out.println("Move Peg Configuration"); System.out.println(" A B C"); System.out.println("unit " + disks); myHanoi(disks, FromPeg, ToPeg, TempPeg); } } |