우선, 비슷한 질문을하는 두 개의 다른 스레드를 발견했습니다. 문제는 그들이 특정 문제에 대해 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
을 첫 번째 조건이 두 번째로 이동하지 않습니다 사실이라면 ...은 if else 문의 – Li357
것도 지금은 그렇다 경우와 순서에서 – Nub
있지만 실행되지 다른-경우 검사, 어떤 일이 일어나 든 관계없이 playerTotal에 점수를 할당 할 수 있습니다. > playerTotal + = points; 이것은 수표를 벗어났습니다. –