2016-11-15 1 views
0

우선, 비슷한 질문을하는 두 개의 다른 스레드를 발견했습니다. 문제는 그들이 특정 문제에 대해 formatting the if statements correctly이 아닌 right equal sign for string을 사용하지 않았다는 것입니다.if else 문이 실행되지 않는 이유는 무엇입니까?

내 임무를 위해, 플레이어가 주사위를 굴려서 100 포인트를 먼저 얻는 것에 대해 돼지라는 게임을 만들어야합니다. 플레이어가 1 턴에 1을 굴리면 추가 포인트를 얻지 못합니다. 플레이어가 두 개의 1을 굴리면 모든 포인트를 잃습니다. 나는 컴퓨터의 턴을 아직 코딩하지 않았으며 단지 플레이어에 집중했다. 내가 뭘 잘못하고 있는지 말해줘. 대단히 감사드립니다.

import java.util.Scanner; 
public class FourFive 
{ 
    public static void main (String[] args) 
    { 
    Pigs myopp = new Pigs(); 
    Scanner scan = new Scanner (System.in); 
    final int Round = 20; 
    int num1, num2; 

    int roundTotal = 0; 
    int playerTotal = 0; 
    int compTotal = 0; 
    int win = 100; 
    int turnOver = 1; 
    Pigs die1 = new Pigs(); 
    Pigs die2 = new Pigs(); 
    String play = "y"; 
    System.out.println("Type y to play"); 
    play = scan.nextLine(); 


while (play.equalsIgnoreCase("y")) 
    { 
     for (int roll = 1; roll <= 6; roll++)//Each die has 6 sides 
     { 
      num1 = die1.roll();//rolls first die 
      num2 = die2.roll();//rolls second die 
      int points = num1 + num2;// adds dies up to get total for this turn 
      System.out.println("You roll " + num1 + " and " + num2); 

      if (num1 == 1 || num2 == 1)//If either of the dies roll 1, no points 
       points += 0; 
      else if (num1 == 1 && num2 == 1)//if both are 1, lose ALL points 
       playerTotal = 0; 
      else 
       System.out.println("you earned " + points + " this round"); 

      playerTotal += points; total number of points per round added 
      System.out.println("You have a total of " + playerTotal); 


      System.out.println("Type y to play"); 
      play = scan.nextLine(); 
     } 
    } 
} 
} 

내 출력 :

Type y to play 
y 
You roll 4 and 2 
you earned 6 this round 
You have a total of 6 
Type y to play 
y 
You roll 6 and 5 
you earned 11 this round 
You have a total of 17 
Type y to play 
y 
You roll 1 and 1 
You have a total of 19 //total should be 0 because of the two 1's 
Type y to play 
y 
You roll 6 and 3 
you earned 9 this round 
You have a total of 28 
Type y to play 
y 
You roll 1 and 1 
You have a total of 30 //total should be 0 because of the two 1's 
Type y to play 
y 
You roll 6 and 4 
you earned 10 this round 
You have a total of 40 
Type y to play 
y 
You roll 5 and 2 
you earned 7 this round 
You have a total of 47 
Type y to play 
y 
You roll 5 and 1 
You have a total of 53 //shouldn't add any additional points because of the "1" 
Type y to play 
+0

을 첫 번째 조건이 두 번째로 이동하지 않습니다 사실이라면 ...은 if else 문의 – Li357

+0

것도 지금은 그렇다 경우와 순서에서 – Nub

+0

있지만 실행되지 다른-경우 검사, 어떤 일이 일어나 든 관계없이 playerTotal에 점수를 할당 할 수 있습니다. > playerTotal + = points; 이것은 수표를 벗어났습니다. –

답변

0

if 논리에 결함이있어 약간 중복됩니다. 이 시도 :

if (num1 == 1 && num2 == 1) { 
    playerTotal = 0; 
} 
else if (num1 != 1 && num2 != 1) { 
    playerTotal += points; 
    System.out.println("you earned " + points + " this round"); 
} 
System.out.println("You have a total of " + playerTotal); 
+0

방금 ​​그 난장판을 만났습니다. 코드 수정에 감사드립니다. – Nub

2

NUM1 조건이 걸리는 경우 다음 첫 번째, 1 인 경우. 'else if'조건을 검사하지 않습니다. 마찬가지로 num2가 1이면 if 조건이이를 사용합니다. 따라서 & &을 먼저 입력하십시오.

 if (num1 == 1 && num2 == 1)//if both are 1, lose ALL points 
      playerTotal = 0; 
     else if (num1 == 1 || num2 == 1)//If either of the dies roll 1, no points 
      points += 0; 
     else 
      System.out.println("you earned " + points + " this round"); 
+0

고마워, 지금 내가 뭘 잘못했는지 안다. – Nub

+0

그는 이미 주사위를 합산했기 때문에'+ ='대신'points = 0'을 설정해야 할 것이다. –

관련 문제