简体   繁体   English

无法弄清楚为什么无法正确解决

[英]Can't figure out why it isn't resolving properly

So, I'm working on this assignment for a class. 所以,我正在为一个班级做这项作业。 It's a Java class, and I'm supposed to make a game where it rolls two dice, adds them up, and adds them to your turn score. 这是一门Java类,我应该做一个游戏,掷出两个骰子,将它们相加,然后将它们加到您的回合得分中。 It then asks if you want to keep playing. 然后询问是否要继续播放。 When your turn score hits 20, or when you decide to pass, it goes to a computer. 当转弯得分达到20或决定通过时,它将进入计算机。 It's supposed to print the scores each turn, and then when someone hits 100 points, it announces the winner. 它应该每回合打印分数,然后当有人达到100分时,宣布获胜者。 However, no matter what, the score at the end of each turn is 0, no matter how many times I run it. 但是,无论如何,无论我跑了多少次,每回合结束时的得分都是0。 When a player rolls a 1, their turn score is cancelled, and it moves on to the other player, and if they roll double 1's, they lose all of their points for the game so far. 当一个玩家掷出1时,他们的回合分数将被取消,并转移到另一位玩家上;如果他们掷出双1,则到目前为止,他们将失去所有积分。 Here is my code, can you figure out why the score variables don't update? 这是我的代码,您能找出为什么分数变量不更新吗? Thank you. 谢谢。

import java.util.Scanner;
import java.util.Random;
public class PlayPig {

    public static void main(String[] args) {
        // TODO Auto-generated method stub  
        Scanner scan = new Scanner(System.in);
        int player1 = 0;
        int player2 = 0;
        int a, b, c, player1turn, player2turn, input;
        int pig = 1;
        Random r = new Random();
        do{
            do {
                player1turn=0;
                a = r.nextInt(6)+1;
                b = r.nextInt(6)+1;
                    if( a==1 || b==1){
                        if (a == 1 && b == 1){
                            player1=0;
                            break;}
                        else if (a==1 || b==1){
                            player1turn=0;
                            break;}
                    else {
                        player1turn= a+b ;
                        }}
                    player1= player1+player1turn;
                System.out.println("Player1 score is " + player1 + " and player2 score is " + player2);
                System.out.print("Do you want to keep playing? Enter 1 to continue. Enter any other number to pass.");
                input = scan.nextInt();
                    if (input != 1)
                        break;
            }
            while       
                (player1turn <= 20);
            do{
                player2turn=0;
                a = r.nextInt(6)+1;
                b = r.nextInt(6)+1;
                if( a==1 || b==1){
                    if (a == 1 && b == 1){
                        player2=0;
                        break;}
                    else if (a==1 || b==1){
                        player2turn=0;
                        break;}
                else {
                    player1turn= a+b ;
                    player2= player2+player2turn;}}
            }
            while
                (player2turn<=20);
        }
        while 
            (player1 < 100 || player2 < 100);
        if (player1>player2)
            System.out.print("Player 1 wins");
        else
            System.out.print("Player 2 wins");

}}

The main problem was, that your else conditions in which you assigned the current score were in the wrong block. 主要问题是,您分配当前分数的else条件处于错误的区域。 (These ones): (这些):

else {
    player1turn = a+b ;
} 

Try this code: 试试这个代码:

public static void main(String[] args) {
    // TODO Auto-generated method stub  
    Scanner scan = new Scanner(System.in);
    int player1 = 0;
    int player2 = 0;
    int a, b, c, player1turn, player2turn, input;        
    int pig = 1;
    Random r = new Random();
    do{

        do {

            player1turn=0;
            a = r.nextInt(6)+1;
            b = r.nextInt(6)+1;
                if( a==1 || b==1){
                    if (a == 1 && b == 1){
                        player1 = 0;
                        break;
                    }
                    else if (a==1 || b==1){
                        player1turn=0;
                        break;
                    }
                }else {
                    player1turn = a+b ;
                }
            player1 += player1turn;
            System.out.println("Player1 score is " + player1 + " and player2 score is " + player2);
            System.out.print("Do you want to keep playing? Enter 1 to continue. Enter any other number to pass.");
            input = scan.nextInt();
                if (input != 1){
                    break;
                }
        } while (player1turn <= 20);

        do{
            player2turn=0;
            a = r.nextInt(6)+1;
            b = r.nextInt(6)+1;

            if( a==1 || b==1){
                if (a == 1 && b == 1){
                    player2=0;
                    break;
                } else if (a==1 || b==1){
                    player2turn=0;
                    break;
                }               
            }else {
                player2turn = a+b ;
                player2 += player2turn;
            }

        }while (player2turn<=20);

    } while (player1 < 100 || player2 < 100);
    if (player1>player2)
        System.out.print("Player 1 wins");
    else
        System.out.print("Player 2 wins");

}

I have modified the if loop. 我已经修改了if循环。 You can try this: 您可以尝试以下方法:

import java.util.Scanner;
import java.util.Random;
public class PlayPig {

public static void main(String[] args) {
    // TODO Auto-generated method stub  
    Scanner scan = new Scanner(System.in);
    int player1 = 0;
    int player2 = 0;
    int a, b, c, player1turn, player2turn, input;
    int pig = 1;
    Random r = new Random();
    do{
        do {
            player1turn=0;
            a = r.nextInt(6)+1;
            b = r.nextInt(6)+1;
                if (a == 1 && b == 1){
                        player1=0;
                        break;
                }
                else if((a== 1 && b!= 1) || (a!=1 && b== 1){
                    player1turn=0;
                    break;
                }
                else{
                    player1turn= a+b ;
                }
                player1= player1+player1turn;
            System.out.println("Player1 score is " + player1 + " and player2          score is " + player2);
            System.out.print("Do you want to keep playing? Enter 1 to      continue. Enter any other number to pass.");
            input = scan.nextInt();
                if (input != 1)
                    break;
        }
        while       
            (player1turn <= 20);
        do{
            player2turn=0;
            a = r.nextInt(6)+1;
            b = r.nextInt(6)+1;
            if (a == 1 && b == 1){
                        player2=0;
                        break;
                }
                else if((a== 1 && b!= 1) || (a!=1 && b== 1){
                    player2turn=0;
                    break;
                }
                else{
                    player2turn= a+b ;
                }
                player2= player2+player2turn;
        }
        while
            (player2turn<=20);
    }
    while 
        (player1 < 100 || player2 < 100);
    if (player1>player2)
        System.out.print("Player 1 wins");
    else
        System.out.print("Player 2 wins");

}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM