投掷骰子计划的建议

时间:2013-10-23 12:09:49

标签: if-statement while-loop throw dice

我正在尝试构建一个新的应用程序,我可以借助参数知道在从参数中获取六个数之前必须抛出骰子的次数。希望你能理解。我是新手,所以请帮助我。

这是我的代码:

package programe;
import java.util.Random;

public class Dice {

public static void main(String[] args) {

    sixes(5);   
}

public static void sixes(int x){    

int roll = 0;
int dice;
int count;

Random random = new Random();

for(count = 1; count <= roll; count++){

    dice = 1 + random.nextInt(6);
    System.out.println("It takes " + roll + "before you get" + x + " sixes in a row");
}

}
}

1 个答案:

答案 0 :(得分:0)

问题并没有让我想到其他的东西所以我决定解决它并完成它。

以下是代码:

public class Dice {


public static String sixes(int x){

    int rolls = 0;

    String sixesRow =" ";
    String result = "";

    Random r = new Random();

    while(true){

        rolls++;
        int rDice = r.nextInt(7);
        if (rDice == 6){
            if(sixesRow.charAt(sixesRow.length()-1) == '6' || sixesRow.charAt(0) == ' '){
                sixesRow.replace(' ', '6');
                String sixesRowCurrent = sixesRow.concat("6");
                sixesRow = sixesRowCurrent;
            }

            if(rowCheck(sixesRow, x)){
                result = "Took " + rolls + " throws to get " + x + " sixes in a row!";
                break;
            }
        }
        else{
            String sixesRowCurrent = sixesRow.concat("0");
            sixesRow = sixesRowCurrent; 
        }
    }
    return result;
}


public static boolean rowCheck(String sixesRow, int x){

    boolean xTimesRow = false;
    String testString = ""; 
    for(int i = 0; i < x; i++){

        String loopString = testString.concat("6");
        testString = loopString;
    }   
    if(sixesRow.contains(testString)){
        xTimesRow = true;
    }   
    return xTimesRow;
}


public static void main(String[] args){

    System.out.println("Please insert the amount of sixes in a row: ");
    Scanner sc = new Scanner(System.in);
    int sixes = sc.nextInt();

    System.out.println(sixes(sixes));
}
}
相关问题