我有这个脚本来模拟java中的2个掷骰子。这是两次完成,一次由用户完成,一次由计算机完成(两次都是自动)。程序输出卷并总结它们。然而,我无法获得if / else语句来比较滚动并确定赢家/或领带。到目前为止,我有:
import java.util.Scanner;
import java.util.Random;
import java.text.DecimalFormat;
/*
Program to simulate die roll
*/
public class Dice
{
Scanner scan = new Scanner (System.in);
Random generator= new Random();
int roll1;
int roll2;
int roll3;
int roll4;
int addroll1;
int addroll2;
public void userdieroll() // Simulates users role
{
roll1 = (generator.nextInt(7) +1); // Generate number from 1-6
System.out.println("Your first roll is "+roll1+"");// Says users first role
roll2 = (generator.nextInt(7) +1); // Generate number from 1-6
System.out.println("Your second roll is "+roll2+"");// Says users second roll
addroll1= roll1 +roll2;// Sums users roles
System.out.println("The sum of your two roles is "+addroll1+" \n");
}
public void compdieroll()// Simulates computers role
{
roll3 = (generator.nextInt(7) +1); // Generate number from 1-6
System.out.println("The computers first role is "+roll3+""); // Says computers first role
roll4 = (generator.nextInt(7) +1); // Generate number from 1-6
System.out.println("The computers second role is "+roll4+""); // Says computers second role
addroll2= roll3 +roll4;// Sums computers roles
System.out.println("The sum of the computers roles is "+addroll2+"");
}
public void findwinner()
{
if (addroll1 == addroll2)
{
System.out.println("Its a tie!");
}
else
{
if (addroll1 > addroll2)
{
System.out.println("You Won!");
}
else
{
System.out.println("You lost!");
}
}
}
public static void main(String[] args)
{
Dice userroll = new Dice();
userroll.userdieroll();
Dice comproll = new Dice();
comproll.compdieroll();
Dice looper = new Dice();
looper.findwinner();
}
}
答案 0 :(得分:2)
每次调用方法时,都会创建一个新的Dice
对象。执行此操作时,您不会将addroll1
和addroll2
存储在同一个对象中,因此.findwinner()
在您的第三个对象中无法正常工作是很自然的未在addroll1
和addroll2
中存储任何值。要解决此问题,请对所有三种方法使用相同的Dice
对象,如下所示:
Dice tester = new Dice();
tester.userdieroll();
tester.compdieroll();
tester.findwinner();
答案 1 :(得分:1)
findwinner()
是此类属性的默认值,addroll1 = 0
正在处理addroll2 = 0
和0
。
如果要处理来自不同对象的相同数据,则需要创建属性static
。
无论如何,正如其他建议的那样,在单个实例上完成工作。
答案 2 :(得分:1)
只是为了让您了解我将如何实现它(具有约20年的Java编程经验):
package nl.owlstead.stackoverflow;
import java.util.Random;
/*
* Program to simulate a double die roll.
*/
public class DiceGame {
private Random generator = new Random();
enum Result {
LOSER,
TIE,
WINNER;
}
public DiceGame() {
// nothing to do in constructor
}
public int rollDice() {
int zeroToFive = generator.nextInt(6);
int oneToSix = zeroToFive + 1;
return oneToSix;
}
public Result findWinner(int totalRollComp, int totalRollUser) {
if (totalRollComp > totalRollUser) {
return Result.LOSER;
}
if (totalRollComp < totalRollUser) {
return Result.WINNER;
}
return Result.TIE;
}
public static void main(String[] args) {
DiceGame game = new DiceGame();
int firstRollComp = game.rollDice();
int secondRollComp = game.rollDice();
int totalRollComp = firstRollComp + secondRollComp;
System.out.printf("Comp rolled: %d and %d, totalling %d%n", firstRollComp, secondRollComp,
totalRollComp);
int firstRollUser = game.rollDice();
int secondRollUser = game.rollDice();
int totalRollUser = firstRollUser + secondRollUser;
System.out.printf("User rolled: %d and %d, totalling %d%n", firstRollUser, secondRollUser,
totalRollUser);
Result userResult = game.findWinner(totalRollComp, totalRollUser);
switch (userResult) {
case LOSER:
System.out.println("You lost!");
break;
case TIE:
System.out.println("Its a tie!");
break;
case WINNER:
System.out.println("You Won!");
break;
}
}
}
特别有用的是使用尽可能少的字段。正如您已经发现使用字段(代表程序中的 state )非常危险。如果你可以避免国家,你应该这样做。所以在这个程序中只有一个字段:随机数生成器(当然我会改用new SecureRandom()
)。
此外,它还显示了为变量和分割输出(System.out
)和游戏本身选择好名称的强度。我试图不介绍太多的技术,但我无法拒绝使用枚举结果和printf
来简化各种println
语句。
答案 3 :(得分:0)
只使用一个对象,您创建了3个骰子,并且这些值在实例之间不是静态共享的......
这将有效:
example.txt
example.jpg
example.pdf
答案 4 :(得分:-1)
在你的课堂上有用户骰子而不是电脑骰子变量。 在你的主要你应该只创建一个类:
Dice d = new Dice();
d.userdieroll();
d.compdieroll();
d.findwinner();
您的代码创建三个不同的Dice实例,其中包含三个不同的roll1
,roll2
,......当您编写
...
Dice looper = new Dice();
looper.findwinner();
它将varlable looper.addroll1
与looper.addroll2
(即looper类中的那些)进行比较,但这些变量仍为0(默认值),在填充userroll.addroll1
之前的行中comproll.addroll2
。
Vjncenzo