0

我正在尝试构建一个新的应用程序,我可以在参数的帮助下知道必须掷多少次骰子才能从参数中获得六分之一。希望你能理解。我在这方面很新,所以请帮助我。

这是我的代码:

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

}
}
4

1 回答 1

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));
}
}
于 2013-10-24T09:42:47.080 回答