簡體   English   中英

無法弄清楚為什么無法正確解決

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

所以,我正在為一個班級做這項作業。 這是一門Java類,我應該做一個游戲,擲出兩個骰子,將它們相加,然后將它們加到您的回合得分中。 然后詢問是否要繼續播放。 當轉彎得分達到20或決定通過時,它將進入計算機。 它應該每回合打印分數,然后當有人達到100分時,宣布獲勝者。 但是,無論如何,無論我跑了多少次,每回合結束時的得分都是0。 當一個玩家擲出1時,他們的回合分數將被取消,並轉移到另一位玩家上;如果他們擲出雙1,則到目前為止,他們將失去所有積分。 這是我的代碼,您能找出為什么分數變量不更新嗎? 謝謝。

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");

}}

主要問題是,您分配當前分數的else條件處於錯誤的區域。 (這些):

else {
    player1turn = a+b ;
} 

試試這個代碼:

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");

}

我已經修改了if循環。 您可以嘗試以下方法:

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