0

我有一个越来越接近完成的河内塔益智计划。我现在的问题是试图让用户的输入正常工作。

如果他们键入“v”或“V”,则将显示解决难题的步骤(因此输出将是“将光盘从 S 移动到 D”等等)。否则,如果用户没有键入“v”或“V”,则程序继续解谜,显示总步数但不显示步数。

我遇到的问题是选项没有像他们应该的那样工作。

现在唯一的问题是当用户输入'v'或'V'时,移动没有正确显示。输出:

Enter the min number of discs : 
2
Press 'v' or 'V' for a list of moves
v
 Move disc from needle S to A
 Total Moves : 3

如果用户键入“v”或“V”,我如何才能完成显示移动,如果用户键入除此之外的其他内容,则输出仅显示“总移动”?

这是我的代码:

 import java.util.*;

import java.util.Scanner;

public class TowerOfHanoi4 {
   static int moves=0;
   public static void main(String[] args) {

   System.out.println("Enter the min number of discs : ");
        Scanner scanner = new Scanner(System.in);
        int iHtMn = scanner.nextInt();       //iHeightMin         

        char source='S', auxiliary='D', destination='A';       //name poles or 'Needles'

   System.out.println("Press 'v' or 'V' for a list of moves");     
        Scanner show = new Scanner(System.in);
        String c = show.next();
        // char lstep='v', lsteps='V';       // grab option v or V

   if (c.equalsIgnoreCase("v")){        //if option is not v or V, execute code and only display total moves
    hanoi(iHtMn, source, destination, auxiliary); //else, user typed v or V and moves are displayed
    System.out.println(" Move disc from needle "+source+" to "+destination);
    System.out.println(" Total Moves : "+moves);
   } else {
      hanoi(iHtMn, source, destination, auxiliary);
      System.out.println(" Total Moves : "+moves);
     }  
   }


    static void hanoi(int htmn,char  source,char  destination,char  auxiliary)
      {
      if (htmn >=1)
          {
             hanoi(htmn-1, source, auxiliary, destination); // move n-1 disks from source to auxilary
              // System.out.println(" Move disc from needle "+source+" to "+destination); // move nth disk to destination
             moves++;     
             hanoi(htmn-1, auxiliary, destination, source);//move n-1 disks from auxiliary to Destination
          }
          // else (
      }
}
4

2 回答 2

3

检查此行

if (c != lstep || c != lsteps){////}

即使 c 是 v 或 V,这也将起作用。假设如果 c = 'v' then c != 'V',所以这个 'if test' 适用于 v 或 V。将其更改为

if (c != lstep && c != lsteps){ 
于 2013-11-06T03:01:17.913 回答
1

您的代码的问题在于else if条件,当您输入vorV时,应用程序会首先执行 if 并且条件为真,因此您else if永远不会执行。

一个if条件就够了,这样试试

//if option is not v or V, execute code and only display total moves
//else, user typed v or V and moves are displayed
if (c == lstep || c == lsteps){
    hanoi(iHeight, source, destination, auxiliary); 
    System.out.println(" Move disc from needle "+source+" to "+destination);
    System.out.println(" Total Moves : "+moves);
} else {
    hanoi(iHeight, source, destination, auxiliary);
    System.out.println(" Total Moves : "+moves);
}

顺便说一句,您的代码中不需要多个扫描仪,只需一个就足够了。

也不需要定义两个char来比较它们,你可以很容易地这样做:

String c = show.next();

//if option is not v or V, execute code and only display total moves
//else, user typed v or V and moves are displayed
if (c.equalsIgnoreCase("v")){
    hanoi(iHeight, source, destination, auxiliary);
    System.out.println(" Move disc from needle "+source+" to "+destination);
    System.out.println(" Total Moves : "+moves);
} else {
    hanoi(iHeight, source, destination, auxiliary);
    System.out.println(" Total Moves : "+moves);
}

根据您上次的编辑更新boolean,您可以定义一个静态字段并将您的 if 条件移动到您的hanoi方法

在这里检查 pastebin

于 2013-11-06T03:04:26.930 回答