这个问题没有让我想到其他的东西,所以我决定解决它并完成。
这是代码:
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));
}
}