2014-06-17 2 views
-2

나는이 코드를 깔끔하게 보이게하는 데 몇 가지 문제가 있습니다. IF가 많은 경우입니다. & OR 구문을 명료화해야합니다.거푸집 롤 조합 자바

아이디어 : 5 개의 오지를 던져 예를 선택하십시오. 똑같은 타입의 3 개 1,1,1 그리고 주사위 두 개를 다시 던지고 점수를 모으기.

까다로운 것은 dice1, dice2, dice3, dice4, dice5를 플레이어가 얻는 점수를 결정하기 위해 규칙 집합과 비교하는 것입니다. 어떤 코드가 가능한 모든 조합을 검사 할 것입니까? "dicex = 1 & dicex = 1 & dicex = 1"플레이어에게 1000 포인트를 주시겠습니까?

int player1points = 0; 
int player2points = 0; 
Scanner in = new Scanner(System.in); 
Random n1 = new Random(); 
int dice1 = n1.nextInt(6) + 1; 
int dice2 = n1.nextInt(6) + 1; 
System.out.println("Dice 1 shows " +dice1); 
System.out.println("Dice 2 shows " +dice2); 
System.out.println("Dice 3 shows " +dice3); 
System.out.println("Dice 4 shows " +dice4); 
System.out.println("Dice 5 shows " +dice5); 

if /* 1+1+1+1+1 = 2000 */ (dice1==1&&dice2==1&&dice3==1&&dice4==1&&dice5==1){ 

System.out.println("You got 2000 points! Throw again?"); 
int points2000 = 2000; 
player1points = player1points + points2000; 
System.out.print("You have "); 
System.out.print(player1points); 
System.out.print(" points. Do you want to throw again?"); 
System.out.println("Yes/No?"); 
s = in.nextLine(); 
} 

else if /* 1+1+1 = 1000 */  (dice1==1&&dice2==1&&dice3==1||dice2==1&&dice3==1&&dice4==1||dice3==1&&dice4==1&&dice5==1|| dice4==1&&dice5==1&&dice1==1||dice5==1&&dice1==1&&dice2==1||dice1==1&&dice2==1&&dice4==1||d ice2==1&&dice3==1&&dice5==1||dice3==1&&dice4==1&&dice1==1||dice4==1&&dice5==1&&dice1==1||dice1==1&&dice3==1&&dice5==1){ 
System.out.println("You got 1000 points!"); 
int points1000 = 1000; 
player1points = player1points + points1000; 
System.out.print("You have "); 
System.out.print(player1points); 
System.out.print(" points. Do you want to throw again?"); 
System.out.println("Yes/No?"); 
s = in.nextLine(); 

} 
+2

이것은 자바 스크립트가 아닙니다 ... –

+0

@AnubianNoob 그렇다면 코드와 태그를 보았습니다. 그래도 고정 시켰습니다 : P –

+2

많은 영어 원어민조차도 잘못 이해합니다. "주사위"는 복수형입니다. (하나 이상). "죽는다"는 단수이다. 예 : 1 개의 주사위, 2 개의 주사위. ("오지"라는 단어는 문제의 양면 개체와 관련이 없으며 전혀 관련이없는 동사의 결합입니다.) –

답변

2

롤의 결과를 arrayList에 넣은 다음 동일한 숫자가 해당 배열 목록에 나타나는 횟수를 계산하십시오. 3 1이 있으면 1000 점을주고, 그렇지 않으면 5 점은 2000 점을줍니다.

코드의 경우 대부분 같은 부분에서 시작합니다. 배열 목록을 채우는 방법을 추가하고 주사위 굴림 결과를 반복/계산하여 가장 많이 나타나는 횟수와 반복 횟수를 반환하십시오.

int player1points = 0; 
int player2points = 0; 
int count = 0; 
int mostCommon = 0; 
int maxCount = 0; 
ArrayList<Integer> diceRolls = new ArrayList<Integer>(); 
Scanner in = new Scanner(System.in); 
Random n1 = new Random(); 

//rollDice method (parameter would be number of dice to roll) 

//checkResults method (count number of times each number 1-6 shows up) 

if(maxCount == 3){ 
    player1points += 1000; 
    //re-roll 2 dice if necessary 
else if(maxCount == 5){ 
    player2points += 2000; 
    //re-roll dice if wanted 

그건 당신이 할 수있는 한 가지 방법입니다. 또한 대용량 블록을 더 읽기 쉬운 기능으로 나누는 것이 좋은 습관이며, 디버깅이 쉬워집니다! 더 이상 도움이 필요하면 언제든지 물어보십시오!

+4

안녕하세요. 대답을 진술하게하고 코드를 제공하면 도움이 될 것입니다. –

+0

감사합니다. 나는 그것을 최대한 빨리 고쳐 줄 것이다. – mrzepka