0

我刚刚完成了我的河内塔计划。它完美无瑕,除了现在我需要以某种方式使它可以读取命令行参数(并产生与我的程序相同的结果)。我的程序执行与下面的输出完全相同的任务,我只是不知道如何使它们看起来像下面的示例,因为我从未使用过命令行参数。

无论如何,对我来说令人沮丧的是,一些示例输入应该如下所示:

java hanoi 3 6将解谜 4 次,分别为 3、4、5 和 6 个磁盘。
java hanoi 3 2将解决难题一次,为​​ 3 个磁盘。
java hanoi dog 6将解谜 4 次,分别为 3、4、5 和 6 个磁盘。

如何将我的程序转换为使用这样的命令行参数?

代码:

import java.util.Scanner;
import java.util.*;

public class hanoi {
    static int moves = 0;
    static boolean displayMoves = false;

    public static void main(String[] args) {
        System.out.print(" Enter the minimum number of Discs: ");
        Scanner minD = new Scanner(System.in);
      String height = minD.nextLine();
      System.out.println();
      char source = 'S', auxiliary = 'D', destination = 'A'; // 'Needles'

      System.out.print(" Enter the maximum number of Discs: ");
        Scanner maxD = new Scanner(System.in);
      int heightmx = maxD.nextInt();

//       if (heightmx.isEmpty()) {   //If not empty
//          // iMax = Integer.parseInt(heightmx);
//          int iMax = 3;
//          hanoi(iMax, source, destination, auxiliary);
//       }  
      System.out.println();


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

      if (iHeight > heightmx){
         hanoi(iHeight, source, destination, auxiliary);
      }

        System.out.print("Press 'v' or 'V' for a list of moves: ");
        Scanner show = new Scanner(System.in);
        String c = show.next();
        displayMoves = c.equalsIgnoreCase("v");   
      }

      for (int i = iHeight; i <= heightmx; i++) {     
           hanoi(i,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);
        } 
    }
}
4

4 回答 4

1

你有 String[] 参数

public static void main(String[] args) {

数组中的第一个字符串是您的第一行参数,第二个字符串是第二行参数,...检查大小,解析为您需要的。

问候寒意。

于 2013-11-06T07:09:35.803 回答
0
public static void main(String[] args)

That magic method signature will contain any String params you pass from the command line.

so, for example:

java myProg a b c

Will have a b and c as part of the args array when I run my program. It's just a matter of processing the args passed in from the command line. They'll be there if you pass them.

于 2013-11-06T07:07:55.967 回答
0

Command line arguments are passed to your main() method, so they are available in args as Strings. Just check how many there are (args.length) and if sufficient arguments were given, convert them to numbers the same way you do now. Otherwise, print an error or usage isntructions and exit.

于 2013-11-06T07:08:29.017 回答
0

读取命令行参数,将String[] args其作为参数传递给您的 main 方法。谷歌会更快地发现这一点。

于 2013-11-06T07:09:52.177 回答