垄断游戏while循环

时间:2016-04-06 02:14:16

标签: java loops

我是编程新手,我有一个“垄断游戏”任务。 我希望玩家在棋盘上移动,每次在广场上停下来都会从其余额中减去租金。 我得到了它,它第一次起作用,但每次我掷骰子时,平衡和位置都会恢复原状。我希望每当我掷骰子和平衡时,这些位置都会继续增加,以便在玩家停止的每个方格中减去。 这是我到目前为止的代码的一部分。

非常感谢你。

package monopoly;

import java.util.*;
import javax.swing.*;


public class Monopoly {

/**
 * @param args the command line arguments
 * @throws java.lang.Exception
 */
public static void main(String[] args) throws Exception 
{
    int sum;
    sum =0;
    String name;
    String token;
    int balance;
    balance = 1500;
    Player playerA = new Player();
    name = JOptionPane.showInputDialog("Enter your name:");
    playerA.setName(name);
    token = JOptionPane.showInputDialog("<html>Choose your token: <br>Race car"
    + ", Wheelbarrow, Battleship, TopHat, Shoe, Dog or Iron"); 
    playerA.setToken(token);
    playerA.setBalance(balance);


    JOptionPane.showMessageDialog(null,"<html>Hello, "+name+"!<br> Welcome to "
    + "Monoply!<br>You chose "+token+" as your token.");


    BoardSquare[] square = new BoardSquare[40]; // array of 40 monopoly squares



    // call the method to load the array
    loadArray(square);




    //***********************************************************************

    int[] Rent = {0,60,0,60,200,200,100,0,100,120,0,140,150,140,160,200,180,
     0,180,200,0,220,0,220,240,200,260,260,150,280,0,300,300,0,320,200,0,350,
     0,400};

    String[] places = {"Go","Mediterranean Ave.","Community Chest","Baltic Ave."
    ,"Income Tax","Reading Railroad","Oriental Ave","Chance","Vermont Ave.",
    "Connecticut Ave.","Jail/Just Visiting","St. Charles Place","Electric Company",
    "States Ave.","Virginia Ave.","Pennsylvania Railroad","St. James Place",
    "Community Chest","Tennessee Ave.","New York Ave.","Free Parking","Kentucky Ave.",
    "Chance","Indiana Ave.","Illinois Ave.","B & O Railroad","Atlantic Ave.",
    "Ventnor Ave.","Water Works","Marvin Gardens","Go To Jail","Pacific Ave.",
    "No. Carolina Ave.","Community Chest","Pennsylvania Ave.","Short Line Railroad",
    "Chance","Park Place", "Luxury", "Tax","Boardwalk"};



    JOptionPane.showMessageDialog(null,"<html>You are at  "+places[0]
    + "<br>The rent is $" +Rent[0]+"<br>Your balance is $"+balance);


     while(balance>0)
    {

     int n;
     n =0;
    int newposition;
    int newbalance;
    newposition = sum + n;
    newbalance = balance - Rent[newposition];

 //***********************************************************************    
    Object[] options = {"Leave the Game", "Roll the dice!"};

    int Answer = JOptionPane.showOptionDialog(null, "Would like to continue playing?",""
    + "Roll the dice?", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE,null, 
    options,options[1]);

    if(Answer == JOptionPane.YES_OPTION)
    {
        System.exit(0); 
    }
    else if (Answer == JOptionPane.NO_OPTION) {

      int dice1;
        int dice2;
        dice1 = (int)(Math.random()*6) + 1;
        dice2 = (int)(Math.random()*6) + 1;
        sum = dice1 + dice2;

        JOptionPane.showMessageDialog(null,"<html>"+name+", your first roll "
        + "was a "+dice1+"<br>Your second roll was a "+dice2+"<br>Your total "
        + "is "+sum);

        JOptionPane.showMessageDialog(null,"<html>"+name+"<br>"+token+""
        + "<br>You are at  "+places[newposition]+"<br>The rent is $"
        +Rent[newposition]+"<br>Your balance is $"+newbalance);

    }   

    } 


    }


//***********************************************************************


// method to load the BoardSquare array from a data file
public static void loadArray(BoardSquare[] square) throws Exception
{
    int i; // a loop counter

    // declare temporary variables to hold BoardSquare properties read from a file
    String inName;    
    String inType;
    int inPrice;
    int inRent;
    String inColor;

    // Create a File class object linked to the name of the file to be read
    java.io.File squareFile = new java.io.File("squares.txt");

    // Create a Scanner named infile to read the input stream from the file
    Scanner infile = new Scanner(squareFile);


    /* This loop reads data into the square array.
     * Each item of data is a separate line in the file.
     * There are 40 sets of data for the 40 squares.
     */
    for( i=0; i<40; i++)
    {
        // read data from the file into temporary variables
        // read Strings directly; parse integers
        inName  = infile.nextLine(); 
        inType  = infile.nextLine(); 
        inPrice = Integer.parseInt( infile.nextLine() );
        inRent  = Integer.parseInt( infile.nextLine() );
        inColor = infile.nextLine(); 

        // intialze each square with the BoardSquare constructor
        square[i] = new BoardSquare(inName, inType, inPrice, inRent, inColor);


    } // end for
    infile.close();

} // endLoadArray
//***********************************************************************

} //结束职业垄断 // ********* ***************************

0 个答案:

没有答案
相关问题