循环程序方法

时间:2013-11-19 01:57:07

标签: java eclipse loops methods

因此,对于我的班级,我们只是学习编写方法。我正在写一个掷2个骰子的游戏,如果它们是相同的你就赢了,如果它们不同则你输了。我应该循环它直到用户决定退出。那么我将打印播放和赢/输的轮数。它完美地运行一次,但我不知道如何循环直到用户决定退出。我不能循环整个方法,所以我不知道该怎么做。我认为布尔可能以某种方式在main方法中工作,但不确定。这是我到目前为止... 感谢。

import java.util.Scanner;

public class Project07 {

    public static void main(String[] args)
    {
        welcome();
        int r1 = rolldie();
        int r2 = rolldie();
        printdice(r1,r2);
        boolean b;      
        b = ispair(r1,r2);
        report(b);
    }

    public static void welcome()
    {
        System.out.println("Welcome to Computer Dice");
        System.out.println("You will first roll your dice");
        System.out.println("Next the outcome of your roll will be determined");
        System.out.println("any pair and you Win");
        System.out.println("anything else and you Lose");
        System.out.println();
        System.out.println();
        String y;
        Scanner stdIn= new Scanner(System.in);
        do{
            System.out.println("Press enter to roll");
            y=stdIn.nextLine();
        } while(!y.equals(""));

    }

    public static int rolldie()
    {
        int die;
        die = (int)(Math.random()*6+1);
        return die;
    }

    public static void printdice(int r1, int r2)
    {
        System.out.println("Your Roll: "+(r1) + (" ") + (r2) );
        System.out.println();
    }

    public static boolean ispair(int r1,int r2)
    {
        boolean b;
        if (r1==r2)
        {
            b=true;
        }
        else
        {
            b=false;
        }
        return b;
    }

    public static void report(boolean b)
    { 

        Scanner stdIn= new Scanner(System.in);
        String one;
        int gamesplayed=0;
        int sumwins=0;
        int sumlosses= 0;

        if(b==true)
        {
            System.out.println("You Win!");
            System.out.println();
            sumwins=sumwins+1;
        }
        else 
        {
            System.out.println("You Lose!");
            System.out.println();
            sumlosses=sumlosses+1;
        }
        gamesplayed=gamesplayed+1;

        do{
            System.out.println("Do you want to play again (y/n): ");
            one= stdIn.next();
        } while(!one.equalsIgnoreCase("y") && !one.equalsIgnoreCase("n"));

        if (one.equalsIgnoreCase("y"))
        {

        }
        else
        {
            System.out.println("You Played "+(gamesplayed)+ (" rounds"));
            System.out.println("You won "+(sumwins)+ (" rounds"));
            System.out.println("You lost "+(sumlosses)+ (" rounds"));
        }
    }
}

2 个答案:

答案 0 :(得分:0)

无论你需要什么(滚动2个骰子),询问用户是否想要继续(Java: How to get input from System.console()用户仍然希望这样做。

答案 1 :(得分:0)

您可以尝试这样的事情:

    String y;
    Scanner stdIn= new Scanner(System.in);
    do{
        System.out.println("Press enter to roll");
        y=stdIn.nextLine().charAt(0);
    } while(y.equals("y"));

然后,如果用户键入除了以y开头的单词之外的任何内容,它将终止循环。