如何将输入设置为公共常量。

huangapple go评论90阅读模式
英文:

how to set input as public constanta

问题

以下是你要翻译的内容:

  1. hi all i have code main and tictactoe class
  2. here my example
  3. of main class. in this code i input something like string number
  4. public static void main(String[] args) {
  5. SwingUtilities.invokeLater(new Runnable() {
  6. public void run() {
  7. Scanner scanner = new Scanner(System.in);
  8. System.out.println("Enter number of ROW Column Do you want to use");
  9. String input = scanner.next();
  10. Integer Input = Integer.parseInt(input);
  11. new TTTGraphics2P(Input); // Let the constructor do the job
  12. }
  13. });
  14. and this is for my class of tictactoe program. if i run this code i only have 3x3 tictactoe program, so i want to modify one of code using my input so my tictactoe will be Input x input
  15. public class TTTGraphics2P extends JFrame {
  16. // Named-constants for the game board
  17. public static final int ROWS = 3; // ROWS by COLS cells
  18. public static final int COLS = 3;
  19. // Named-constants of the various dimensions used for graphics drawing
  20. public static final int CELL_SIZE = 100; // cell width and height (square)
  21. public static final int CANVAS_WIDTH = CELL_SIZE * COLS; // the drawing canvas
  22. public static final int CANVAS_HEIGHT = CELL_SIZE * ROWS;
  23. public static final int GRID_WIDTH = 8; // Grid-line's width
  24. public static final int GRID_WIDHT_HALF = GRID_WIDTH / 2; // Grid-line's half-width
  25. // Symbols (cross/nought) are displayed inside a cell, with padding from border
  26. public static final int CELL_PADDING = CELL_SIZE / 6;
  27. public static final int SYMBOL_SIZE = CELL_SIZE - CELL_PADDING * 2; // width/height
  28. public static final int SYMBOL_STROKE_WIDTH = 8; // pen's stroke width
  29. // Use an enumeration (inner class) to represent the various states of the game
  30. public enum GameState {
  31. PLAYING, DRAW, CROSS_WON, NOUGHT_WON
  32. }
  33. private GameState currentState; // the current game state
  34. // Use an enumeration (inner class) to represent the seeds and cell contents
  35. public enum Seed {
  36. EMPTY, CROSS, NOUGHT
  37. }
  38. private Seed currentPlayer; // the current player
  39. private Seed[][] board ; // Game board of ROWS-by-COLS cells
  40. private JPanel canvas; // Drawing canvas (JPanel) for the game board
  41. private JLabel statusBar; // Status Bar
  42. /** Constructor to setup the game and the GUI components
  43. * @param input */
  44. public TTTGraphics2P(Integer input) {
  45. canvas = new DrawCanvas(); // Construct a drawing canvas (a JPanel)
  46. canvas.setPreferredSize(new Dimension(CANVAS_WIDTH, CANVAS_HEIGHT));
  47. // The canvas (JPanel) fires a MouseEvent upon mouse-click
  48. canvas.addMouseListener(new MouseAdapter() {
  49. @Override
  50. public void mouseClicked(MouseEvent e) { // mouse-clicked handler
  51. int mouseX = e.getX();
  52. int mouseY = e.getY();
  53. // Get the row and column clicked
  54. int rowSelected = mouseY / CELL_SIZE;
  55. int colSelected = mouseX / CELL_SIZE;
  56. if (currentState == GameState.PLAYING) {
  57. if (rowSelected >= 0 && rowSelected < ROWS && colSelected >= 0
  58. && colSelected < COLS && board[rowSelected][colSelected] == Seed.EMPTY) {
  59. board[rowSelected][colSelected] = currentPlayer; // Make a move
  60. updateGame(currentPlayer, rowSelected, colSelected); // update state
  61. // Switch player
  62. currentPlayer = (currentPlayer == Seed.CROSS) ? Seed.NOUGHT : Seed.CROSS;
  63. }
  64. } else { // game over
  65. initGame(); // restart the game
  66. }
  67. // Refresh the drawing canvas
  68. repaint(); // Call-back paintComponent().
  69. }
  70. });

你的问题是如何将以下字段更改为输入的值:

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

更改为:

  1. public static final int ROWS = Input; // ROWS by COLS cells
  2. public static final int COLS = Input;

解答:

要将这些字段更改为输入的值,你需要在构造函数 TTTGraphics2P 中传递一个参数 input,然后使用这个参数来初始化 ROWSCOLS 字段。在构造函数内部,将参数 input 分配给这两个字段,像这样:

  1. public TTTGraphics2P(Integer input) {
  2. // ... (之前的代码)
  3. // 在构造函数内部将输入的值赋给 ROWS 和 COLS 字段
  4. ROWS = input;
  5. COLS = input;
  6. // ... (继续后续的代码)
  7. }

然后,确保你的 ROWSCOLS 字段不再被声明为 final,因为你将在构造函数中为它们赋值。

英文:

hi all i have code main and tictactoe class

here my example
of main class. in this code i input something like string number

  1. public static void main(String[] args) {
  2. SwingUtilities.invokeLater(new Runnable() {
  3. public void run() {
  4. Scanner scanner = new Scanner(System.in);
  5. System.out.println(&quot;Enter number of ROW Column Do you want to use&quot;);
  6. String input = scanner.next();
  7. Integer Input = Integer.parseInt(input);
  8. new TTTGraphics2P(Input); // Let the constructor do the job
  9. }
  10. });

and this is for my class of tictactoe program. if i run this code i only have 3x3 tictactoe program, so i want to modify one of code using my input so my tictactoe will be Input x input

  1. public class TTTGraphics2P extends JFrame {
  2. // Named-constants for the game board
  3. public static final int ROWS = 3; // ROWS by COLS cells
  4. public static final int COLS = 3;
  5. // Named-constants of the various dimensions used for graphics drawing
  6. public static final int CELL_SIZE = 100; // cell width and height (square)
  7. public static final int CANVAS_WIDTH = CELL_SIZE * COLS; // the drawing canvas
  8. public static final int CANVAS_HEIGHT = CELL_SIZE * ROWS;
  9. public static final int GRID_WIDTH = 8; // Grid-line&#39;s width
  10. public static final int GRID_WIDHT_HALF = GRID_WIDTH / 2; // Grid-line&#39;s half-width
  11. // Symbols (cross/nought) are displayed inside a cell, with padding from border
  12. public static final int CELL_PADDING = CELL_SIZE / 6;
  13. public static final int SYMBOL_SIZE = CELL_SIZE - CELL_PADDING * 2; // width/height
  14. public static final int SYMBOL_STROKE_WIDTH = 8; // pen&#39;s stroke width
  15. // Use an enumeration (inner class) to represent the various states of the game
  16. public enum GameState {
  17. PLAYING, DRAW, CROSS_WON, NOUGHT_WON
  18. }
  19. private GameState currentState; // the current game state
  20. // Use an enumeration (inner class) to represent the seeds and cell contents
  21. public enum Seed {
  22. EMPTY, CROSS, NOUGHT
  23. }
  24. private Seed currentPlayer; // the current player
  25. private Seed[][] board ; // Game board of ROWS-by-COLS cells
  26. private JPanel canvas; // Drawing canvas (JPanel) for the game board
  27. private JLabel statusBar; // Status Bar
  28. /** Constructor to setup the game and the GUI components
  29. * @param input */
  30. public TTTGraphics2P(Integer input) {
  31. canvas = new DrawCanvas(); // Construct a drawing canvas (a JPanel)
  32. canvas.setPreferredSize(new Dimension(CANVAS_WIDTH, CANVAS_HEIGHT));
  33. // The canvas (JPanel) fires a MouseEvent upon mouse-click
  34. canvas.addMouseListener(new MouseAdapter() {
  35. @Override
  36. public void mouseClicked(MouseEvent e) { // mouse-clicked handler
  37. int mouseX = e.getX();
  38. int mouseY = e.getY();
  39. // Get the row and column clicked
  40. int rowSelected = mouseY / CELL_SIZE;
  41. int colSelected = mouseX / CELL_SIZE;
  42. if (currentState == GameState.PLAYING) {
  43. if (rowSelected &gt;= 0 &amp;&amp; rowSelected &lt; ROWS &amp;&amp; colSelected &gt;= 0
  44. &amp;&amp; colSelected &lt; COLS &amp;&amp; board[rowSelected][colSelected] == Seed.EMPTY) {
  45. board[rowSelected][colSelected] = currentPlayer; // Make a move
  46. updateGame(currentPlayer, rowSelected, colSelected); // update state
  47. // Switch player
  48. currentPlayer = (currentPlayer == Seed.CROSS) ? Seed.NOUGHT : Seed.CROSS;
  49. }
  50. } else { // game over
  51. initGame(); // restart the game
  52. }
  53. // Refresh the drawing canvas
  54. repaint(); // Call-back paintComponent().
  55. }
  56. });

my question is i want to change this field

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

become

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

how to build it ?

thank you all

答案1

得分: 1

只需使用构造函数设置rowscols

  1. public void run() {
  2. Scanner scanner = new Scanner(System.in);
  3. System.out.print("Total rows: ");
  4. int rows = scanner.nextInt();
  5. System.out.print("Total columns: ");
  6. int cols = scanner.nextInt();
  7. new TTTGraphics2P(rows, cols);
  8. }
  9. public class TTTGraphics2P extends JFrame {
  10. private final int rows;
  11. private final int cols;
  12. public TTTGraphics2P(int rows, int cols) {
  13. this.rows = rows;
  14. this.cols = cols;
  15. }
  16. }
英文:

Just use constructor to set rows and cols:

  1. public void run() {
  2. Scanner scanner = new Scanner(System.in);
  3. System.out.print(&quot;Total rows: &quot;);
  4. int rows = scanner.nextInt();
  5. System.out.print(&quot;Total columns: &quot;);
  6. int cols = scanner.nextInt();
  7. new TTTGraphics2P(rows, cols);
  8. }
  9. public class TTTGraphics2P extends JFrame {
  10. private final int rows;
  11. private final int cols;
  12. public TTTGraphics2P(int rows, int cols) {
  13. this.rows = rows;
  14. this.cols = cols;
  15. }
  16. }

答案2

得分: 1

如果将相同的值分配给最终变量,您将无法更改它。而且它应该是非静态的。如果我们将最终变量设为静态,就无法在构造函数中为其分配值。不要忘记声明没有值的最终变量。
声明没有值的最终变量。

  1. public final int ROWS;
  2. public final int COLS;

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

  1. public class Solution {
  2. public final int ROWS;
  3. public final int COLS;
  4. public Solution(int input) {
  5. ROWS = input;
  6. COLS = input;
  7. System.out.println(ROWS);
  8. }
  9. public static void main(String[] args) {
  10. Scanner scanner = new Scanner(System.in);
  11. int input = scanner.nextInt();
  12. Solution s = new Solution(input);
  13. }
  14. }
英文:

If you assign same value to final variable you can't change it. And it should be a non-static. If we static a final variable we can't assign a value in constructor. And don't forget to declare final variable without value.
Declare final variables without value.

  1. public final int ROWS;
  2. public final int COLS;

Refer this sample program to find a solution for your problem.

  1. public class Solution {
  2. public final int ROWS;
  3. public final int COLS;
  4. public Solution(int input) {
  5. ROWS=input;
  6. COLS=input;
  7. System.out.println(ROWS);
  8. }
  9. public static void main(String[] args) {
  10. Scanner scanner=new Scanner(System.in);
  11. int input=scanner.nextInt();
  12. Solution s=new Solution(input);
  13. }
  14. }

答案3

得分: 0

由于您已将ROWSCOLS变量声明为static,它们将在类加载时进行初始化。由于它们是final的,一旦设置了值就不能更改。

为了实现您的要求,您可以按照以下方式修改TTTGraphics2P类,以读取输入并在类加载时将其设置为变量。

  1. public class TTTGraphics2P extends JFrame {
  2. // 游戏棋盘的命名常量
  3. public static final int ROWS = getInput(); // 行乘以列的单元格数
  4. public static final int COLS = getInput();
  5. ...
  6. // 从用户读取标准输入
  7. public static int getInput() {
  8. Scanner scanner = new Scanner(System.in);
  9. System.out.println("请输入要使用的行列数:");
  10. String input = scanner.next();
  11. return Integer.parseInt(input);
  12. }
  13. ...
  14. }
英文:

As you have declared the ROWS and COLS variables as static they will be initialized when class is loaded. Since they are final, once set the value cannot be changed.

To achieve your requirement you can modify the TTTGraphics2P class as below to read the input and set to the variables at class loading.

  1. public class TTTGraphics2P extends JFrame {
  2. // Named-constants for the game board
  3. public static final int ROWS = getInput(); // ROWS by COLS cells
  4. public static final int COLS = getInput();
  5. ...
  6. // Read standard input from the user
  7. public static int getInput() {
  8. Scanner scanner = new Scanner(System.in);
  9. System.out.println(&quot;Enter number of ROW Column Do you want to use&quot;);
  10. String input = scanner.next();
  11. return Integer.parseInt(input);
  12. }
  13. ...
  14. }

huangapple
  • 本文由 发表于 2020年9月13日 15:33:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/63868307.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定