java - 根据输入建立新变量

时间:2013-05-31 17:40:14

标签: java object methods syntax

所以,该计划要求保龄球比赛。一旦你询问了多少玩家,每个玩家获得2次击倒10个引脚(由随机数字模拟)。如果你在第一次击球时全部击倒,那么你得到20分,如果你第二次得到15分,除了那个得分=扔1 +扔2。

对我来说,棘手的部分是让每个玩家交替使用帧(2次投掷= 1帧,每位玩家最多10帧),同时程序会记录每个人的得分!我以为我有这个,但是randoms只是添加了底部的输出

import java.util.Scanner;

public class Bowling {

    public static void main(String [] args){
        Scanner input = new Scanner (System.in);
        Game aNew = new Game();
        int player;
        int i;
        int j;
        int nPlay;



        System.out.print("How many players are there?: ");
        nPlay = input.nextInt();
            for (j = 1; j<= 10; j++) {
                for (i = 1; i <= nPlay; i++ ){
                     player = i; // i tried player i = new Player() but get error "value
                     aNew.getScore(player); // already used in scope""
                }
            }

游戏课程:

import java.lang.Math;

    public class Game {
    int score = 0;
    int player;
    int ran1;
    int ran2;

    public Game() {
    }

    public int getScore(int player){
        ran1 = (int) (9 * Math.random());
        ran2 = (int) (((10 - ran1)) * Math.random());

        if (ran1 == 10){
            score += 20;
        } else if (ran1 + ran2 == 10){
            score += 15;
        } else {
            score += ran1 + ran2;
        }   System.out.println("Player " + player + " score is: " + score + "\n");
        System.out.println("ran1: " + ran1 + "  ran2: " + ran2);
        return score;
    }
}

玩家类:

public class Player {
   /** this class is not doing anything, however i would like for an object to store a score for every player to keep track if that is possible?*/ 
    int score;

    public Player (){
        int score;
    }
}

输出:(可疑每次最后一位玩家有多高或者我从未注意到它不起作用!)

How many players are there?: 2

Player 1 score is: 2

ran1: 1  ran2: 1
Player 2 score is: 11

ran1: 8  ran2: 1
Player 1 score is: 16

ran1: 5  ran2: 0
Player 2 score is: 21

ran1: 4  ran2: 1
Player 1 score is: 29

ran1: 8  ran2: 0
Player 2 score is: 35

ran1: 5  ran2: 1
Player 1 score is: 40

ran1: 3  ran2: 2
Player 2 score is: 47

ran1: 6  ran2: 1
Player 1 score is: 56

ran1: 8  ran2: 1
Player 2 score is: 61

ran1: 5  ran2: 0
Player 1 score is: 68

ran1: 7  ran2: 0
Player 2 score is: 77

ran1: 8  ran2: 1
Player 1 score is: 85

ran1: 4  ran2: 4
Player 2 score is: 90

ran1: 4  ran2: 1
Player 1 score is: 97

ran1: 7  ran2: 0
Player 2 score is: 103

ran1: 3  ran2: 3
Player 1 score is: 107

ran1: 4  ran2: 0
Player 2 score is: 114

ran1: 3  ran2: 4
Player 1 score is: 121

ran1: 7  ran2: 0
Player 2 score is: 125

ran1: 0  ran2: 4

2 个答案:

答案 0 :(得分:1)

您的播放器课程目前无效。通过首先为您的Player.score变量创建访问器和修改器来解决此问题。甚至可能给他们起个名字:

public class Player {
     int score;
     String name;
     public Player(String name) { this.name = name; }
     public void getName() { return this.name; }
     public void setScore(int newScore) { score = newScore; }
     public int getScore() { return score; }

}

现在你的游戏类应该有像

这样的收集类型的玩家
public class Game {
    List<Player> players = new List<Player>();
    public Game() {
    }
    public void AddPlayer(Player p) {
        players.add(p);
    }
    public Player getPlayer(int index) { return players.get(index); }
    public void playerBowl(Player p) {
        ran1 = (int) (9 * Math.random());
        ran2 = (int) (((10 - ran1)) * Math.random());

        if (ran1 == 10){
            p.setScore(p.getScore() +20);
        } else if (ran1 + ran2 == 10){
            p.setScore(p.getScore() +15);
        } else {
            p.setScore(p.getScore() + ran1 + ran2);
        }   System.out.println("Player " + p.getName() + " score is: " + p.getScore() + "\n");
        System.out.println("ran1: " + ran1 + "  ran2: " + ran2);
    }
}

我会离开主要人员去弄清楚:D。对不起,如果语法稍微偏离,我在编辑窗口中写了:/

答案 1 :(得分:0)

我觉得你的麻烦从这里开始:

player = i; // i tried player i = new Player() but get error "value
aNew.getScore(player); // already used in scope""

正确的方法是:

Player i = new Player(); //mind the capital!

其余的:你可能想要使用一个玩家列表并在你知道有多少玩家后初始化这个列表。最后,getScore函数不应修改Game类中的score变量,而应修改Player的score值变量(player.score = 20)。你需要在游戏课上了解你的玩家,但你可以通过以下几种方式来做到这一点:将玩家作为参数传递给getScore,或者在较早的时候传递整个玩家列表。

总而言之,有相当多的代码要为你重写,但我确信使用这段代码会非常有教育意义。