我是Java的新手,我有一个程序玩家猜测一个项目的价格(首先输入确切的价格)。我有一个名为winnerCount
的数组,它跟踪每个玩家赢得的回合。我想使用" JOptionPane.showMessageDialog"输出它。部分代码涉及根据赢得的回合给每位玩家一个奖励。因此,对于我的伪代码,我有IF轮次赢得= 1那么奖金赢得:15,125美元。当我在Java中尝试相同的想法时,它给了我错误"无与伦比的类型"因为它比较int
和int[]
。由于Java无法比较不同的数据类型,我可以通过哪种方式输出所需的结果(" winnerCount"以及每位玩家赢得的奖品)?
这是我与
合作的内容import javax.swing.JOptionPane;
import java.lang.Math;
import java.util.*;
public class priceIsRight {
public static void main(String[] args) {
double [] numPlayers = new double [4];
double exactPrice = getExactPrices();
double [] guessPrice = getGuessPrices(exactPrice);
int roundsWon = getRoundsWon(guessPrice, exactPrice);
int numRounds = 0;
int [] winnersCount = new int [4];
int roundWinner = getRoundsWon(guessPrice, exactPrice);
int prizesWon = calculatePrizesWon(winnersCount, roundWinner);
}
public static int getRoundsWon(double[] guessPrice, double exactPrice) {
int roundWinner = getRoundsWon(guessPrice, exactPrice);
int [] winnersCount = new int [4];
int numRounds = 0;
do {
double minValue = Math.abs(exactPrice-guessPrice[0]);
roundWinner = 0;
for (int i=1;i < guessPrice.length; i++) {
double diff = Math.abs(exactPrice-guessPrice[i]);
if (diff<minValue) {
minValue=diff;
roundWinner=i;
}
winnersCount[roundWinner]++;
}
}
while (numRounds <=3);
return roundWinner;
}
//Outputs result
public static int calculatePrizesWon(int [] winnersCount, int roundWinner) {
int prizesWon = 0;
if (winnersCount = 0) {
JOptionPane.ShowMessageDialog(null, "Prize won: $106 consolidation prize!");
}
else if (winnersCount = 1) {
JOptionPane.ShowMessageDialog(null, "Prize won: $15,125!");
}
else if (winnersCount = 2) {
JOptionPane.ShowMessageDialog(null, "Prize won: $30,110!");
}
else if (winnersCount = 3) {
JOptionPane.ShowMessageDialog(null, "Prize won: $15,120,000!");
}
}
}
答案 0 :(得分:0)
您需要指定要比较的数组中的哪个位置。试试这个:
public static int calculatePrizesWon(int[] winnersCount, int roundWinner) {
int prizesWon = 0;
if (winnersCount[roundWinner] == 0) {
JOptionPane.showMessageDialog(null, "Prize won: $106 consolidation prize!");
} else if (winnersCount[roundWinner] == 1) {
JOptionPane.showMessageDialog(null, "Prize won: $15,125!");
} else if (winnersCount[roundWinner] == 2) {
JOptionPane.showMessageDialog(null, "Prize won: $30,110!");
} else if (winnersCount[roundWinner] == 3) {
JOptionPane.showMessageDialog(null, "Prize won: $15,120,000!");
}
return prizesWon; // Replace this with the real prize won number.
}
您可能会使用switch语句做得更好。如果您想查看或接收有关其他事项的评论,您可以更好地发表评论。
答案 1 :(得分:0)
有些事情对这段代码没有意义。例如,为什么你让numPlayers成为double []?
是不是更有意义呢?int numPlayers = 4;
对于你的winnersCount,是存储每个玩家赢了多少轮?如果是这样,那么我假设您的calculatePrizesWon方法应该迭代每个玩家,并输出他们的奖品。
像
这样的东西public static int calculatePrizesWon(int [] winnersCount, int roundWinner)
{
int prizesWon = 0;
for (int i = 0; i < winnersCount.length; i++)
{
if (winnersCount[i] == 0)
{
JOptionPane.ShowMessageDialog(null, "Player "+i+" - Prize won: $106 consolidation prize!");
}
else if (winnersCount[i] == 1)
{
JOptionPane.ShowMessageDialog(null, "Player "+i+" - Prize won: $15,125!");
}
else if (winnersCount[i] == 2)
{
JOptionPane.ShowMessageDialog(null, "Player "+i+" - Prize won: $30,110!");
}
else if (winnersCount[i] == 3)
{
JOptionPane.ShowMessageDialog(null, "Player "+i+" - Prize won: $15,120,000!");
}
}
}
此外,您传递给calculatePrizesWon的“圆形赢家”参数是什么? 什么是“prizesWon”变量?例如,如果一名球员赢了三轮,那么他们也不会赢得三个奖项吗?那么winnersCount和奖品之间有什么区别呢? 我会尝试提供帮助,但你需要更清楚地解释一下你正在做些什么。