2015-11-22 43 views
0

好吧,所以我必须编写一个程序猪骰子,我大部分都完成并运行,但最大的问题是,分数不添加,我真的需要帮助,它使用面值从一个独立程序Die是随机生成的。PigDice java错误

import java.util.Scanner; 

public class pigDiceGame 
{ 
public static void main(String[]args) 
{ 
    // This section will ask the user is they know how to play the game of      Pig Dice 

    System.out.println("You are Going to Play the Pig Dice Game!"); 
    System.out.println(""); 
    System.out.println(""); 
    System.out.println("Do You Know the rules?...if not press 1...otherwise press any key to continue."); 

    // This sectionwill allow the user to input whether they want to read the rules or not 
    Scanner scanner = new Scanner (System.in); 
    int rules = scanner.nextInt(); 

    // This sectionwill actually explain the rules of the game 
    if (rules == 1) 
    { 
    System.out.println(""); 
    System.out.println(""); 
    System.out.println("Rules..."); 
    System.out.println(""); 
    System.out.println(""); 
    System.out.println("1. In one turn the player rolls the dice until either"); 
    System.out.println(" -They roll a one"); 
    System.out.println(" -Or they choose to stop"); 
    System.out.println("2. You add up the numbers on the dice and are trying             to reach 100."); 
    System.out.println("3. Rolling a 1 will clear your points for that turn."); 
    System.out.println("4. If you choose to stop you can keep the points you have earned."); 
    System.out.println("5. Rolling a pair counts double points"); 
    System.out.println(""); 
    System.out.println(""); 
    } 
    System.out.println("Lets begin..."); 
    System.out.println(""); 
    System.out.println(""); 



    // all the variables I am using in the game are here 

    int roll=1; 


    while (roll<=1) 
    { 
    System.out.println("Player-one roll,enter 1 to role or 2 to save"); 
    Scanner scan = new Scanner (System.in); 
    roll = scanner.nextInt(); 


    if(roll==1) 
    { 
    if(roll==1) 
    roll=roll-1; 


    Die c = new Die(); 
    Die d = new Die(); 
    int faceValue1=c.roll(); 
    int faceValue2=d.roll(); 



    System.out.println(); 
    System.out.print("You rolled a:: "+faceValue1); 
    System.out.print(" and "+faceValue2); 
    System.out.println(); 
    int score=faceValue1+faceValue2; 

    if(faceValue1==1||faceValue2==1) 
    { 
    System.out.println("You rolled a one, your score has been set to zero,unless you banked some points and your turn is over."); 
    roll=3; 
    System.out.println(); 
    } 
    else 
    System.out.println("Current score is "+score); 
    System.out.println(); 

    if(faceValue1==1&&faceValue2==1) 
    { 
    System.out.println("You rolled two ones, that adds 25 points to your score."); 
    score=score+25; 
    System.out.println("Current score is "+score); 
    System.out.println(); 
    } 

    if(faceValue1==faceValue2&&faceValue1!=1&&faceValue2!=1) 
    { 
    System.out.println("You rolled doubles, that is double the points."); 
    score=(faceValue1+faceValue2)*2; 
    System.out.println("Current score is "+score); 
    System.out.println(); 
    } 


    if(score==100) 
    { 
    System.out.println("Player-One WINS!"); 
    roll=9; 

    } 




     } 


    } 


    while (roll==3) 
    { 
    int score2=0; 


    System.out.println("Player-Two roll,enter 3 to role or 4 to save"); 
    Scanner scan = new Scanner (System.in); 
    roll = scanner.nextInt(); 




    Die c = new Die(); 
    Die d = new Die(); 
    int faceValue1=c.roll(); 
    int faceValue2=d.roll(); 
    int score=0; 


    System.out.println(); 
    System.out.print("You rolled a:: "+faceValue1); 
    System.out.print(" and "+faceValue2); 
    System.out.println(); 
    score2=faceValue1+faceValue2; 

    if(faceValue1==1||faceValue2==1) 
    { 
    System.out.println("You rolled a one, your score has been set to zero,unless you banked some points and your turn is over."); 
    score2=score-score; 
    roll=1; 

    } 
    else 
    System.out.println("Current score is "+score2); 
    System.out.println(); 

    if(faceValue1==1&&faceValue2==1) 
    { 
    System.out.println("You rolled two ones, that adds 25 points to your score."); 
    score2=score2+25; 
    System.out.println("Current score is "+score2); 
    System.out.println(); 
    } 
    if(faceValue1==faceValue2&&faceValue1!=1&&faceValue2!=1) 
    { 
    System.out.println("You rolled doubles, that is double the points."); 
    score2=(faceValue1+faceValue2)*2; 
    System.out.println("Current score is "+score2); 
    System.out.println(); 
    } 

    if(score==100) 
    System.out.println("Player-Two WINS!"); 
    roll=9; 


    } 

} 

}

+1

你可以用它产生的输出,你期望的和它出错的地方加强你的问题吗? – hotzst

回答

0

您稍后重新声明你的变量。你只需要实例化一次,每个变量,否则它被设置为0

int score=faceValue1+faceValue2; // (line 71) 
int score=0; // (line 132) 

它可能会更容易为你管理,看看发生了什么事情,如果你声明所有的变量在最顶部你的功能和方法。