如何让Java方法根据两个参数返回布尔值?代码调用异常排查
DiceRoll代码问题修复
问题现象
调用Parity方法时,布尔变量Parity未随方法返回值更新,无法确认方法返回结果。推测和传入的参数Val1、Val2相关,但不确定。尝试打印Val1、Val2仅能显示数字,无法定位问题。
原代码
import java.util.Scanner; import java.util.Random; public class DiceRoll { public static void main (String[] args) { Scanner scan = new Scanner (System.in); Random rand = new Random (); int gameNumber = 0; int compPoint = 0; int playerPoint = 0; int currentGame = 0; int compRoll = 0; int playerRoll = 0; int compRoll2 = 0; int playerRoll2 = 0; int sumComp = 0; int sumPlayer = 0; int Val1 = 0; int Val2 =0; boolean Parity = false; System.out.println (" How many games? "); gameNumber = scan.nextInt (); System.out.println ("Number of games: "+gameNumber); System.out.println ("Computer Wins: "+compPoint); System.out.println ("Player Wins: "+playerPoint); System.out.println (""); currentGame = currentGame+1; System.out.println ("Current Game:" +currentGame); compRoll = DiceRoll(10); playerRoll = DiceRoll(10); compRoll2 = DiceRoll (10); playerRoll2 = DiceRoll (10); sumComp = (compRoll + compRoll2); sumPlayer = (playerRoll + playerRoll2); System.out.printf ("Computer: %d %d Total: %d \nPlayer: %d %d Total: %d" , compRoll, compRoll2, sumComp, playerRoll, playerRoll2, sumPlayer); System.out.println (""); //parityCheck = Val1; if (Parity(Val1, Val2) == true) { System.out.println ("Parity: True"); } else { System.out.println ("Parity: False"); } } public static int DiceRoll (int sides) { int rollTotal = 0 ; int minRand = 1; int maxRand = sides; int range = (maxRand - minRand)+ 1; rollTotal = (int)(Math.random()* range) +minRand; return rollTotal; } public static boolean Parity (int Val1, int Val2) { int evenSum = 0; int oddSum = 0; int sumComp2 = 0; int sumPlayer2 = 0; sumComp2= Val1; sumPlayer2= Val2; if (sumComp2 %2 == 0 && sumPlayer2 == 0 || sumComp2 %2 == 1 && sumPlayer2 == 1) { return true; } else { return false; } //return (); }
问题分析
代码存在两处核心错误:
- 参数传递错误:调用
Parity方法时传入的是初始值为0的Val1和Val2,而非计算好的sumComp和sumPlayer,导致方法处理无效数据 - 逻辑判断错误:
Parity方法中错误检查sumPlayer2 == 0或sumPlayer2 == 1,实际应判断sumPlayer2的奇偶性是否和sumComp2一致
修复后的代码
import java.util.Scanner; import java.util.Random; public class DiceRoll { public static void main (String[] args) { Scanner scan = new Scanner (System.in); Random rand = new Random (); int gameNumber = 0; int compPoint = 0; int playerPoint = 0; int currentGame = 0; int compRoll = 0; int playerRoll = 0; int compRoll2 = 0; int playerRoll2 = 0; int sumComp = 0; int sumPlayer = 0; int Val1 = 0; int Val2 =0; boolean Parity = false; System.out.println (" How many games? "); gameNumber = scan.nextInt (); System.out.println ("Number of games: "+gameNumber); System.out.println ("Computer Wins: "+compPoint); System.out.println ("Player Wins: "+playerPoint); System.out.println (""); currentGame = currentGame+1; System.out.println ("Current Game:" +currentGame); compRoll = DiceRoll(10); playerRoll = DiceRoll(10); compRoll2 = DiceRoll (10); playerRoll2 = DiceRoll (10); sumComp = (compRoll + compRoll2); sumPlayer = (playerRoll + playerRoll2); System.out.printf ("Computer: %d %d Total: %d \nPlayer: %d %d Total: %d" , compRoll, compRoll2, sumComp, playerRoll, playerRoll2, sumPlayer); System.out.println (""); // 修复:传入实际计算的sumComp和sumPlayer if (Parity(sumComp, sumPlayer) == true) { System.out.println ("Parity: True"); } else { System.out.println ("Parity: False"); } } public static int DiceRoll (int sides) { int rollTotal = 0 ; int minRand = 1; int maxRand = sides; int range = (maxRand - minRand)+ 1; rollTotal = (int)(Math.random()* range) +minRand; return rollTotal; } public static boolean Parity (int Val1, int Val2) { int sumComp2 = Val1; int sumPlayer2 = Val2; // 修复:判断两者奇偶性是否一致 return (sumComp2 % 2) == (sumPlayer2 % 2); }
内容的提问来源于stack exchange,提问作者Tayfrax
相关产品推荐
相关产品推荐

