我正在尝试为我的类创建一个抛出3个骰子的java程序。玩家掷骰子并根据获得的三个值获得美元利润。特别是,利润等于偶数值的数量乘以这些值的总和加上奇数值的数量乘以这些值的总和。
例如,如果玩家掷出4,5和2,我们计算:
2 x 6 (两个偶数值相加为6)
和
1 x 5 (一个奇数值汇总为5)。 因此利润为12 + 5 = 17美元。
这是我到目前为止所拥有的 -
import java.util.Scanner;
public class Maiorca_Hw2
{
public static void main (String [] args)
{
Scanner keyboard = new Scanner (System.in);
System.out.println("Enter Values of Dice 1");
d1 = keyboard.nextDouble();
System.out.println("Enter Values of Dice 2");
d2 = keyboard.nextDouble();
System.out.println("Enter Values of Dice 3");
d3 = keyboard.nextDouble();
double numbers = 1,2,3,4,5,6;
double profit, dice, d1, d2, d3, oddsum, evensum;
if (d1 == 1 || d1 == 3 || d1 == 5)
oddsum =
答案 0 :(得分:2)
我个人认为程序应该以不同的方式完成,你也应该使用clases。
我希望这个小代码可以帮到你!
首先,我们必须创建一个名为Dice的类,我们将在其中设置骰子的行为和允许的交互。 你说玩家会滚动,所以我认为必须有随机事件。
// CODE:
import java.util.Random;
public class Dice {
private final int SIDES;
//Constructor for a dice
public Dice(int sides){
//Establishes the number of sides of the dice we're using.
SIDES = sides;
}
//Methods:
public int roll(){
Random rnd = new Random();
return rnd.nextInt(SIDES) + 1;
}
}
之后,在主课程中,我们进行了测试。 所有内容的原因都在评论中
// CODE:
public class MainClass {
public static void main(String[] args) {
int moneyEarned = 0;
//We generate 3 dices with 6 sides each.
Dice d1 = new Dice(6);
Dice d2 = new Dice(6);
Dice d3 = new Dice(6);
// Then we start the simulation:
int res1, res2, res3;
res1 = d1.roll();
res2 = d2.roll();
res3 = d3.roll();
//Show results:
System.out.println("Roll results: dice 1 = " + res1 +
"; dice 2 = " + res2 + "; dice 3 = " + res3);
//We obtain the money the player must earn:
//An even number modulo 2 can only be either 1 or 0 so the same number + 1 will
//switch from 0 to 1 and the opposite thing.
//With this number we can choose which numbers we want to sum:
int oddNumbersPoints = ((res1 * (res1%2))+(res2 * (res2 % 2))+( res3 * (res3 % 2)));
int evenNumbersPoints = ((res1 * ((res1 + 1) %2)) + (res2 * ((res2+1) % 2))+( res3 * ((res3+1) % 2)));
moneyEarned = (evenNumbersPoints * 2) + oddNumbersPoints;
//Finally display the final result:
System.out.println("Even Numbers Points: " + evenNumbersPoints);
System.out.println("Odd Numbers Points: " + oddNumbersPoints);
System.out.println("Congratulations, you just earned: " + moneyEarned + "$");
}
}
///// 最后你可以改变主类来改变骰子的数量,得分......等等......
(添加'}'和主要班长)
答案 1 :(得分:0)
声明基本类型(在本例中为double
)可以做的是:
double profit = 0, dice = 2, d2 = 3;
声明一个双打数组:
double[] numbers = {1,2,3,4,5,6};