当前位置: 首页 > 知识库问答 >
问题:

如何将输入设置为公共constanta

暨承平
2023-03-14

大家好,我有代码主和tictactoe类

这里是main类的示例。在这段代码中,我输入了类似字符串号的内容

public static void main(String[] args) {
         SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                 Scanner scanner = new Scanner(System.in);
                 System.out.println("Enter number of ROW Column Do you want to use");
                 String input = scanner.next();
                 Integer Input = Integer.parseInt(input);
                new TTTGraphics2P(Input); // Let the constructor do the job
             }
          });

这是为我的tictactoe程序班准备的。如果我运行这个代码,我只有3x3tictactoe程序,所以我想用我的输入修改其中一个代码,这样我的tictactoe将输入x输入

public class TTTGraphics2P extends JFrame {
   // Named-constants for the game board
   public static final int ROWS = 3;  // ROWS by COLS cells
   public static final int COLS = 3;
 
   // Named-constants of the various dimensions used for graphics drawing
   public static final int CELL_SIZE = 100; // cell width and height (square)
   public static final int CANVAS_WIDTH = CELL_SIZE * COLS;  // the drawing canvas
   public static final int CANVAS_HEIGHT = CELL_SIZE * ROWS;
   public static final int GRID_WIDTH = 8;                   // Grid-line's width
   public static final int GRID_WIDHT_HALF = GRID_WIDTH / 2; // Grid-line's half-width
   // Symbols (cross/nought) are displayed inside a cell, with padding from border
   public static final int CELL_PADDING = CELL_SIZE / 6;
   public static final int SYMBOL_SIZE = CELL_SIZE - CELL_PADDING * 2; // width/height
   public static final int SYMBOL_STROKE_WIDTH = 8; // pen's stroke width
 
   // Use an enumeration (inner class) to represent the various states of the game
   public enum GameState {
      PLAYING, DRAW, CROSS_WON, NOUGHT_WON
   }
   private GameState currentState;  // the current game state
 
   // Use an enumeration (inner class) to represent the seeds and cell contents
   public enum Seed {
      EMPTY, CROSS, NOUGHT
   }
   private Seed currentPlayer;  // the current player
 
   private Seed[][] board   ; // Game board of ROWS-by-COLS cells
   private JPanel canvas; // Drawing canvas (JPanel) for the game board
   private JLabel statusBar;  // Status Bar
 
   /** Constructor to setup the game and the GUI components 
 * @param input */
   public TTTGraphics2P(Integer input) {
      canvas = new DrawCanvas();  // Construct a drawing canvas (a JPanel)
      canvas.setPreferredSize(new Dimension(CANVAS_WIDTH, CANVAS_HEIGHT));
 
      // The canvas (JPanel) fires a MouseEvent upon mouse-click
      canvas.addMouseListener(new MouseAdapter() {
         @Override
         public void mouseClicked(MouseEvent e) {  // mouse-clicked handler
            int mouseX = e.getX();
            int mouseY = e.getY();
            // Get the row and column clicked
            int rowSelected = mouseY / CELL_SIZE;
            int colSelected = mouseX / CELL_SIZE;
 
            if (currentState == GameState.PLAYING) {
               if (rowSelected >= 0 && rowSelected < ROWS && colSelected >= 0
                     && colSelected < COLS && board[rowSelected][colSelected] == Seed.EMPTY) {
                  board[rowSelected][colSelected] = currentPlayer; // Make a move
                  updateGame(currentPlayer, rowSelected, colSelected); // update state
                  // Switch player
                  currentPlayer = (currentPlayer == Seed.CROSS) ? Seed.NOUGHT : Seed.CROSS;
               }
            } else {       // game over
               initGame(); // restart the game
            }
            // Refresh the drawing canvas
            repaint();  // Call-back paintComponent().
         }
      });

我的问题是我想改变这个领域

public static final int ROWS = 3;  // ROWS by COLS cells
       public static final int COLS = 3;

成为

公共静态最终int行=输入;//行按COLS单元格public static final int COLS=input;

怎么建?

谢谢大家

共有2个答案

艾宏远
2023-03-14

如果给final变量赋相同的值,就不能更改它。它应该是一个非静态的。如果我们静态一个最终变量,我们就不能在构造函数中赋值。并且不要忘记声明final variable不带值。声明没有值的最终变量。

public final int ROWS;
 public final int COLS;

请参阅此示例程序以找到问题的解决方案。

public class Solution {

    public final int ROWS;
    public final int COLS;

    public Solution(int input) {
        ROWS=input;
        COLS=input;
        System.out.println(ROWS);
    }

    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        int input=scanner.nextInt();
        Solution s=new Solution(input);
    }
} 
阮才俊
2023-03-14

只需使用构造函数设置cols:

public void run() {
    Scanner scanner = new Scanner(System.in);
    System.out.print("Total rows: ");
    int rows = scanner.nextInt();
    System.out.print("Total columns: ");
    int cols = scanner.nextInt();
    new TTTGraphics2P(rows, cols);
}

public class TTTGraphics2P extends JFrame {
    private final int rows;
    private final int cols;
    
    public TTTGraphics2P(int rows, int cols) {
        this.rows = rows;
        this.cols = cols;
    }
}
 类似资料:
  • 本文向大家介绍laravel框架如何设置公共头和公共尾,包括了laravel框架如何设置公共头和公共尾的使用技巧和注意事项,需要的朋友参考一下 可以新建一个布局模板,layout.blade.php 在layout中设置公共头、尾 再新建个内容模板,content.blade.php 继承布局模板 当然公共头部也可以再单独写一个模板header.blade.php 在layout中用@includ

  • 组件: 表单: 该函数更新一个JSON记录。例如,它用“fname”ng model更新“firstName:”条目。问题是,除非用户修改输入的值,否则ngModel被读取为null,即使已经存在< code > value = " { { current user . first name } } " 的值。 如何设置 ngModel 以从实际输入的值其值?

  • 我想通过Selenium设置日期值。 我正在处理这个页面

  • 我正在使用Azure WebJob,但现在收到以下错误消息: 未找到任何函数。尝试使作业类公开,使方法公开为静态。 我的代码很简单: 此外,我从我的调试文件夹创建一个zip文件并上传它,并且将作业配置为连续运行。

  • 我正在使用spring boot编写一个api,我希望将所有资源映射到一个公共基本路径(在本例中为/api)后面。但是,我不想为每个RestController类做注解(例如,通过使用@RequestMapping对其进行注解)。我想过以下解决方案,但它们都有我不想要的缺点: 创建一个基类(如ApiRestController)并让所有其他RestController继承这个类。这有一个缺点,即类

  • org.apache.axis2.AxisFault:连接已关闭:javax.net.ssl.sslhandShakeException:sun.security.validator.validatoreXception:PKIX路径验证失败:java.security.cert.certPathValidatoreXception:basic constraints检查失败:这不是CA证书