嘿伙计们,我无法看到阻止我的代码正确编译的原因。我很新,所以我提前为我糟糕的格式道歉。
package javaApplication2;
/*There are two players playing a card game. Both players have seven cards each.
There are seven rounds in the game. Each round, the value of the players’ cards for
that particular round are compared to see who has the highest valued card. The
player with the highest value card wins the round.
At the end of the game (after seven rounds), the program should determine which
player has won the game overall (won the most rounds) or if the game has ended in a
tie.*/
public class JavaApplication2 {
public static void main(String[] args) {
int[] wresult = {0,1,2,3,4,5,6};
int[] lresult = {0,1,2,3,4,5,6};
int P1round = 0, P2round = 0;
int rounds = 0;
String victor=null;
String loser=null;
int[] winner = {0,1,2,3,4,5,6};
int[] p1Cards = {10, 6, 8, 9, 7, 12, 7};
int[] p2Cards = {7, 6, 9, 5, 2, 8, 11};
int[] players = {1,2};
boolean playing = true;
//////////////////////////////////////////////////////////////////
if(playing){
//game/loop begins here
do{
if (p1Cards[rounds]>p2Cards[rounds]){
winner[rounds]=players[0];
wresult[rounds]=p1Cards[rounds];
lresult[rounds]=p2Cards[rounds];
P1round+=1;
rounds++;
}
else if (p1Cards[rounds]<p2Cards[rounds]){
winner[rounds]=players[1];
wresult[rounds]=p2Cards[rounds];
lresult[rounds]=p1Cards[rounds];
P2round+=1;
rounds++;}
else wresult[rounds]=players[0];
lresult[rounds]=players[1];
rounds++;
}while (rounds!=6);
//end loop}
//game is over
else{playing=false;}
if(playing==false){
//if/else to determine the result
if (P1round>P2round){
victor = "Player One";
loser = "Player Two";
}
else if (P2round>P1round){
victor = "Player Two";
loser = "Player One";
}
else
System.out.println("It's a draw!");
System.exit(0);
System.out.println("Round No: 1- Player" + winner[0] + " wins the round:" + wresult[0] + " beats" + lresult[0]);
System.out.println("Round No: 2- Tie!" + wresult[1] + " ties with" + lresult[1]);
System.out.println("Round No: 3- Player" + winner[2] + "wins the round:" + wresult[2] + " beats" + lresult[2]);
System.out.println("Round No: 4- Player" + winner[3] + "wins the round:" + wresult[3] + " beats" + lresult[3]);
System.out.println("Round No: 5- Player" + winner[4] + "wins the round:" + wresult[4] + " beats" + lresult[4]);
System.out.println("Round No: 6- Player" + winner[5] + "wins the round:" + wresult[5] + " beats" + lresult[5]);
System.out.println("Round No: 7- Player" + winner[6] + "wins the round:" + wresult[6] + " beats" + lresult[6]);
System.out.println("Player " + victor + " wins!!! He won " + P1round + " rounds beating " + loser + " who won " + P2round + " rounds!");
System.out.println("Goodbye!");
}
}
}
答案 0 :(得分:0)
我注意到你在两个else
语句中缺少大括号。尝试更改此内容:
else wresult[rounds]=players[0];
lresult[rounds]=players[1];
rounds++;
对此:
else {
wresult[rounds]=players[0];
lresult[rounds]=players[1];
rounds++;
}
同样,您需要围绕最终else
语句使用括号,如下所示:
else {
System.out.println("It's a draw!");
System.exit(0);
}
如果你的代码仍然没有编译,请告诉我,我可以再看看。
答案 1 :(得分:0)
运行设置为true的if语句以运行do-while循环,但是打印输出所在的if语句遵循条件“if(playing == false){” - 在此场景中播放永远不能等于假的。
正如@Keara所提到的,你需要为这些花括号添加一些语法。 在某些情况下,编译器将允许您不添加大括号,例如使用if语句而不使用其他条件。但是在你的while循环结束时你有“else {playing = false;}”没有语法前缀,我想你会在这里得到一个错误。
修复语法并更正打印输出条件,它应该可以正常工作。 :)
答案 2 :(得分:0)
在您的评论//end loop}
和//game is over
之间进行编译所需的唯一缺失括号,因为您不会关闭if(playing){
。
但是,您应该注意playing
永远不会false
,因为您最初将其设置为true
并且不会更改它,因此代码永远不会进入{{ 1}}。
所以我建议完全取出你的if(playing==false){
语句,然后运行if...else
循环,然后让它转到结束块,以确定谁赢了并运行你的打印语句等。
此外,您应该删除do...while
,因为这会在您打印任何内容之前结束该程序。
我还会有一个计数器,这个计数器加起来每轮赢得的比例与你的显示方式略有不同;例如,在System.exit(0)
内只需将一个添加到胜利计数器。
以下是example在线版。