2013-10-23 3 views
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"); 
} 

} 
} 
+0

문제가 무엇입니까 : 여기

코드인가? – sharptooth

+0

필자는 매개 변수에 저장된 여섯 개를 가져 오기 전에 얼마나 많은 시간을 거쳐야 하는지를 세는 프로그램을 어떻게 설정하는지 이해할 수 없습니다 ... –

+0

5 (x) 개의 여섯 개를 얻기 위해 주사위를 얼마나 자주 던져야하는지 알고 싶습니다. ? –

답변

0

문제는 내가 그것을하고 완료를 해결하기로 결정 그래서 내가 다른 것들에 대해 생각하게 didnt한다 :

여기 내 코드입니다.

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)); 
} 
} 
관련 문제