是否需将代码所有方法与实例变量设为static?NashEquilibrium代码咨询
静态变量/方法 vs 逻辑错误:纳什均衡Java代码问题
我编写了一段用于在给定矩阵中寻找纳什均衡的Java代码。此前因遇到“无法从static方法调用非static方法”的错误,将所有方法和实例变量都改为了static。目前代码存在大量逻辑错误,输出结果不正确,想咨询这是否是全部使用static导致的,还是仅为逻辑错误?代码如下:
import java.util.ArrayList; import java.util.Scanner; public class Nash { public static String nes; public static String str; public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("Please enter the amount of strategies for each player"); int stratA = scan.nextInt(); int stratB = scan.nextInt(); String[][] utilities = new String[stratA][stratB]; System.out.println("Please enter the utilities"); for(int row = 0; row<stratA; row++) for(int column = 0; column<stratB; column++) utilities[row][column] = scan.next(); // Creates a 2D array with given utilities if (nashExists(stratA, stratB, utilities) == true) System.out.println(nes); else System.out.println("No NE found"); // Prints the results } public static boolean nashExists(int strA, int strB, String[][] util) { int[][] movesA = new int[strA][strB]; for(int row = 0; row<strA; row++) for(int column = 0; column<strB; column++) movesA[row][column] = Integer.parseInt(util[row][column].substring(0,1)); int[][] movesB = new int[strA][strB]; for(int row = 0; row<strA; row++) for(int column = 0; column<strB; column++) movesA[row][column] = Integer.parseInt(util[row][column].substring(2,3)); // Creates a 2d integer array for utilites of every strategy of A and B ArrayList<String> aNE = new ArrayList<String>(); ArrayList<String> bNE = new ArrayList<String>(); for(int row = 0; row<strA; row++) for(int column = 0; column<strB; column++) if (nashExistsA(row, column, movesA) == true) aNE.add((row+1) + "," + (column+1)); for(int row = 0; row<strA; row++) for(int column = 0; column<strB; column++) if (nashExistsB(row, column, movesB) == true) bNE.add((row+1) + "," + (column+1)); // Checks if there are NE for one of players if (compareArrayLists(aNE, bNE) == true) return true; else return false; } // Checks if there are any matchs between both players NE's public static boolean nashExistsA(int r, int c, int[][] a) { int max = a[r][c]; for (int i = 0; i<a.length; i++) if (max < a[i][c]) max = a[i][c]; if (a[r][c] == max) return true; else return false; } public static boolean nashExistsB(int r, int c, int[][] b) { int max = b[r][c]; for (int i = 0; i<b[0].length; i++) if (max < b[r][i]) max = b[r][i]; if (b[r][c] == max) return true; else return false; } public static boolean compareArrayLists(ArrayList<String> aN, ArrayList<String> bN) { for (int i=0; i<aN.size(); i++) { String potNE = aN.get(i); if (bN.indexOf(potNE) >= 0) str += "(" + potNE + ") "; } nes = str; if (str.length()>0) return true; else return false; } }
问题分析
首先明确:全部使用static不是导致逻辑错误的直接原因,你的代码问题主要是独立的逻辑bug,和static无关。具体错误点如下:
- movesB数组赋值错误:在
nashExists方法中,给movesB赋值时错误地修改了movesA数组,导致movesB始终是默认的0值数组,完全无法正确获取玩家B的效用值。正确写法应为movesB[row][column] = Integer.parseInt(util[row][column].substring(2,3))。 - 静态变量未初始化:
str和nes作为static变量默认值为null,在compareArrayLists中直接拼接字符串会导致结果开头出现null(比如第一次拼接后变成null(1,1))。 - 静态变量的副作用:如果多次调用
compareArrayLists,str会累积之前的结果,导致输出混乱。应该用局部变量收集结果,而非依赖静态变量。 - 数组越界风险:
nashExistsB方法中直接使用b[0].length,如果传入的数组为空(比如没有策略),会抛出数组越界异常,需先做非空判断。
总结
代码的核心问题是逻辑错误,和全部使用static无直接关联。不过全用static会降低代码可维护性,还可能带来状态残留等副作用。建议重构为面向对象结构:将nes、str改为局部变量,方法改为实例方法,在main中创建Nash类实例调用方法,更符合Java的设计规范。
内容的提问来源于stack exchange,提问作者Arson1st
相关产品推荐
相关产品推荐

