如何在一个类中更改另一个类的变量值? Java的

时间:2015-10-25 21:13:19

标签: java variables command-line-arguments instance-variables

我遇到问题,我已经在一个类中声明了要从另一个类更新的变量。这在Java中甚至可能吗?

以下是我希望更改值的类的部分。

public class DrawMaze {
   // Width of border in pixels
   public static int borderwidth = 40;

   // Size of maze cell in pixels
   public static int cellsize = 30;

   // Time to sleep after a move (in milliseconds)
   // Change to default to 50. 1000 gives time to observe the program.
   public static int sleep time = 1000;
}

这是我的第二课的一部分,其中cellsizeborderwidthsleeptime的值将使用命令行参数进行更改。

public class MazeSolver {
    // Get the cell size, border width, and/or the sleep time if available 
   try{
       if (args.length >= 2){
           DrawMaze.cellsize = Integer.parseInt(args[1]);
           if (!(DrawMaze.cellsize >= 10)){
              System.out.println("Cellsize must be an integer greater than or equal to 10."); 
           }
       }
       if (args.length >= 3){
           DrawMaze.borderwidth = Integer.parseInt(args[2]);
           if (!(DrawMaze.borderwidth >= 5)){
                  System.out.println("Borderwidth must be an integer greater than or equal to 5."); 
           }
       }
       if (args.length == 4){
           DrawMaze.sleeptime = Integer.parseInt(args[3]);
           if (!(DrawMaze.sleeptime >= 0) && !(DrawMaze.sleeptime <= 10000)){
                  System.out.println("sleeptime must be an integer in the range of 0 to 10,000."); 
           }
       }
}

0 个答案:

没有答案