2015-11-22 70 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) 

它可能會更容易爲你管理,看看發生了什麼事情,如果你聲明所有的變量在最頂部你的功能和方法。