-3

所以这是我现在的代码:

public class PigTry2
{

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        // Variables

        int start;
        int total;


        //Methods

        Scanner quest = new Scanner (System.in);
        die1 x = new die1 ();
        die2 z = new die2 ();


        // Game

        System.out.println("Hello. Would you like to play PIG? 1 for yes");
        start = quest.nextInt();
        if (start == 1){
            x.roll();
            z.roll();

            System.out.println("You roll: "+ z.getEyes() + " " + x.getEyes());
            do {
                System.out.println("Would you like to roll again");
                start = quest.nextInt();
                if (start == 1)
                x.roll();
                z.roll();
                System.out.println("You roll: "+ z.getEyes() + " " + x.getEyes());
            } while(z.getEyes() != 1 && x.getEyes() != 1);

            total = 

        }
    }
}

我尝试了几种不同的方法,我想将发生的滚动总数相加。我根本不知道该怎么做。任何人都可以帮助我吗?

4

1 回答 1

0

如果我明白你想要什么,我建议:

/**
 * @param args the command line arguments
 */
public static void main(String[] args)
{
    // Variables

    int start;
    int total = 0;


    //Methods

    Scanner quest = new Scanner (System.in);
    die1 x = new die1 ();
    die2 z = new die2 ();


    // Game

    System.out.println("Hello. Would you like to play PIG? 1 for yes");
    start = quest.nextInt();
    if (start == 1){
        x.roll();
        z.roll();
        total += 2; //You just rolled twice, so lets increment by 2

        System.out.println("You roll: "+ z.getEyes() + " " + x.getEyes());
        do {
            System.out.println("Would you like to roll again");
            start = quest.nextInt();
            if (start == 1)
            x.roll();
            z.roll();
            total += 2; //EDIT: Rolled again, so ingrement by 2 again
            System.out.println("You roll: "+ z.getEyes() + " " + x.getEyes());
        } while(z.getEyes() != 1 && x.getEyes() != 1);

        System.out.println("Total amount of rolls: " + total);

    }
}

其他:用户在抛出“1”之前无法取消滚动,因为 do-while-loop 中的 if 语句不会退出循环。您可以使用标志来指示用户是否要继续滚动:

boolean finished = false;
do {
    System.out.println("Would you like to roll again");
    start = quest.nextInt();
    if (start == 1) {
        x.roll();
        z.roll();
        total += 2; //EDIT: Rolled again, so ingrement by 2 again
        System.out.println("You roll: "+ z.getEyes() + " " + x.getEyes());
    } else {
        finished = true;
    }
} while(z.getEyes() != 1 && x.getEyes() != 1 && !finished);
于 2013-09-16T18:55:49.870 回答