【问题标题】:If/else statement for certain case特定情况下的 if/else 语句
【发布时间】:2013-11-06 13:04:01
【问题描述】:

您好,这是一个河内之塔益智计划。现在,如果提示“输入光盘数量”为空白(即在未输入任何整数值的情况下按下回车键),我无法让程序解决 3 个光盘问题。

*我的 hanoi 方法中的 if/else 语句是我认为问题所在。我评论了我认为问题出在哪里。如果提示输入“输入光盘数量”时未输入任何内容,如何让程序仅求解 3 张光盘?*

代码:

import java.util.Scanner;

public class TowerOfHanoi4 {
    static int moves = 0;
    static boolean displayMoves = false;
   static int blank = 3;

    public static void main(String[] args) {

        System.out.println("Enter the Number of Discs : ");
        Scanner scanner = new Scanner(System.in);
        int iHeight = scanner.nextInt();
        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();
        displayMoves = c.equalsIgnoreCase("v");


        hanoi(iHeight, source, destination, auxiliary);
        System.out.println(" Total Moves : " + moves);
    }

    static void hanoi(int height, char source, char destination, char auxiliary) {
        if (height >= 1) {
            hanoi(height - 1, source, auxiliary, destination);
            if (displayMoves) {
                System.out.println(" Move disc from needle " + source + " to "
                        + destination);
            }
            moves++;
            hanoi(height - 1, auxiliary, destination, source);
        }
//       else (height == blank) {                                  //I think the problem
//          hanoi(height - 1, source, auxiliary, destination);//Lies with this
//          moves++;                                          //else
//          hanoi(height - 1, auxiliary, destination, source);//statement         
//       }   
    }
}

【问题讨论】:

  • 这个程序在哪里说“如果输入是空白的?”您将 scanner.nextInt(); 中的输入与值 3 进行比较。仅仅因为您将变量名称称为“空白”,您是否希望程序等待空白输入?
  • 是的,我确实希望它等待空白输入。输入“无”
  • 但是您在程序的哪个位置检查空白输入?您提到输入为空白的唯一地方是您选择将变量命名为“空白”。

标签: java recursion user-input towers-of-hanoi


【解决方案1】:

您可以在获取输入后立即进行检查,而不是在方法中进行检查。

String height = scanner.nextLine();
int iHeight = height.trim().isEmpty() ? 3 : Integer.parseInt(height);
// The above code snippet will read the input and set the iHeight value as 3 if no input is entered.

并从hanoi() 方法中删除else 部分。

编辑:如果不需要显示步骤的选项,您需要添加if 并在其中显示显示步骤的选项。

String height = scanner.nextLine();
int iHeight = 3; // Default is 3
if (!height.trim().isEmpty()) { // If not empty
    iHeight = Integer.parseInt(height); // Use that value

    // And display the option to show steps
    System.out.println("Press 'v' or 'V' for a list of moves");
    Scanner show = new Scanner(System.in);
    String c = show.next();
    displayMoves = c.equalsIgnoreCase("v");
}

【讨论】:

  • 太棒了,我没看到!还有一个问题,如果不输入任何内容,我如何让程序跳过“按'v'或'V'获取移动列表”步骤?
  • 在这种情况下,您需要在代码中有一个if-else。但是你为什么要这样做呢?如果没有输入,那么默认值为 3 对吗?为什么不让用户选择查看高度为 3 的步数?
  • 这正是说明所指定的。如果未输入任何内容,则求解 3 个棋子,仅显示“总步数”且不列出步数。我知道很奇怪
  • 检查答案中的编辑。我想这应该对你有用。
猜你喜欢
  • 1970-01-01
  • 2018-10-25
  • 1970-01-01
  • 1970-01-01
  • 2019-04-09
  • 1970-01-01
  • 2017-02-05
  • 2021-06-01
  • 1970-01-01
相关资源
最近更新 更多