If statement 关于掷骰子程序的建议
我正在尝试构建一个新的应用程序,在这个应用程序中,我可以借助一个参数知道在从参数中获得6的数量之前必须掷多少次骰子。希望你能理解。我在这方面很新,所以请帮助我 这是我的密码:If statement 关于掷骰子程序的建议,if-statement,while-loop,throw,dice,If Statement,While Loop,Throw,Dice,我正在尝试构建一个新的应用程序,在这个应用程序中,我可以借助一个参数知道在从参数中获得6的数量之前必须掷多少次骰子。希望你能理解。我在这方面很新,所以请帮助我 这是我的密码: package programe; import java.util.Random; public class Dice { public static void main(String[] args) { sixes(5); } public static void sixes(int x){
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");
}
}
}
package程序;
导入java.util.Random;
公共类骰子{
公共静态void main(字符串[]args){
六(5);
}
公共静态空六(int x){
int roll=0;
整数骰子;
整数计数;
随机=新随机();
因为(count=1;count这个问题没有让我考虑其他事情,所以我决定解决它,它就完成了
代码如下:
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));
}
}
公共类骰子{
公共静态字符串六(整数x){
int rolls=0;
字符串sixesRow=“”;
字符串结果=”;
随机r=新随机();
while(true){
rolls++;
int rDice=r.nextInt(7);
如果(rDice==6){
如果(sixestrow.charAt(sixestrow.length()-1)='6'| | sixestrow.charAt(0)=''){
六、如下图所示,替换(“”,’6’);
字符串sixesRowCurrent=sixesRow.concat(“6”);
sixesRow=sixesRow电流;
}
如果(行检查(六行,x)){
结果=“连续掷“+x+”个骰子获得“+x+”个六个!”;
打破
}
}
否则{
字符串sixesRowCurrent=sixesRow.concat(“0”);
sixesRow=sixesRow电流;
}
}
返回结果;
}
公共静态布尔行检查(字符串六行,整数x){
布尔值xTimesRow=false;
字符串testString=“”;
对于(int i=0;i
我不知道如何设置程序来计算它必须滚动多少次才能获得存储在参数中的6…你想知道你必须多久掷一次骰子才能得到5(x)个6吗?是的。我想编程。有人能帮我吗??