Java 分数计算器

Java 分数计算器,java,Java,好的,我对java非常陌生。 我正在为一个我长期搁置的项目设计一个分数计算器。不过,我想知道如何做到这一点,为我自己的知识 该程序应该为每个玩家要求掷骰子,并将其添加到先前的骰子中。 我假设一个while循环可以实现这一点,但每次它通过循环时,它都会将变量重置为当前滚动。因此,我不能得到一个总数 下面是一些代码: static int players; static String p1; static String p2; static String p3;

好的,我对java非常陌生。
我正在为一个我长期搁置的项目设计一个分数计算器。不过,我想知道如何做到这一点,为我自己的知识

该程序应该为每个玩家要求掷骰子,并将其添加到先前的骰子中。
我假设一个while循环可以实现这一点,但每次它通过循环时,它都会将变量重置为当前滚动。因此,我不能得到一个总数

下面是一些代码:

    static int players;
    static String p1;
    static String p2;
    static String p3;
    static String p4;
    static int maxScore;
    static int roll1;
    static int roll2;
    static int roll3;
    static int roll4;
    static int total1;
    static int total2;
    static int total3;
    static int total4;
    public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);
    System.out.print("Enter number of players: ");
    players=keyboard.nextInt();
    System.out.print("Enter Maximum Score: ");
    maxScore=keyboard.nextInt();
    if(players==2){                    //will add more ifs when i get the code right
        System.out.println("Please enter players names.");
        System.out.print("Player 1: ");
        p1=keyboard.next();
        System.out.print("Player 2: ");
        p2=keyboard.next();
        System.out.println(p1 + "\t \t " + p2 + "\n"
        + "_______ \t _______ \n" );  //displays scorecard look with players names

        {


        while (total1 < maxScore && total2<maxScore) { 
        //scorecard quits when maxScore is reached by either player
        int roll;
        total1=(roll+roll1); 

        System.out.print("");
        roll=keyboard.nextInt(); //asks for current roll

        System.out.print("\n"+"_____"+"\n");
        System.out.print(roll+"+"+"\n"+roll1+"\n"+"_____"+"\n"+(roll+roll1)+"\n");
        /*i want this to display total score + last roll and then 
        *total it again on the next line*/
        roll1=roll;
        }
静态int播放器;
静态字符串p1;
静态字符串p2;
静态字符串p3;
静态字符串p4;
静态int-maxScore;
静态int-roll1;
静态int-roll2;
静态int-roll3;
静态int-roll4;
静态整数total1;
静态整数2;
静态整数3;
静态整数4;
公共静态void main(字符串[]args){
扫描仪键盘=新扫描仪(System.in);
System.out.print(“输入玩家数量:”);
players=键盘.nextInt();
系统输出打印(“输入最大分数:”);
maxScore=keyboard.nextInt();
if(players==2){//当我得到正确的代码时,将添加更多的if
System.out.println(“请输入球员姓名”);
系统输出打印(“播放器1:”);
p1=键盘。下一步();
系统输出打印(“玩家2:”);
p2=键盘。下一步();
System.out.println(p1+“\t\t”+p2+“\n”
+“\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
{

而(total1
total1+=(roll+roll1); 
这和

total1= total1+(roll+roll1); 
你只是没有把这些卷加到总价值中

同样值得注意的是,将实例变量设置为public和static不是一个好主意。如果它们是私有的,而不是静态的,那就更好了

    private int players;

希望答案对您在Java编程过程中有所帮助

一些提示:

  • 变量
    roll
    没有任何作用。
    roll1
    等将存储每个玩家的最后一次掷骰

  • 如果可能的话,初始化变量。应该避免依赖默认值,因为这可能会在学习过程中给您带来问题(
    NullPointerException
    会在某个时候拜访您)

  • 在循环中,您有
    total1=(roll+roll1);
    。这是错误的。您的变量
    total1
    roll
    roll1
    在程序到达该点时未初始化。因为它们是整数,所以它们是(静默的)初始化为0,因此
    total1
    此时产生0,这并没有完成多少工作。之后,继续检索滚动。尝试另一种方法,首先滚动,然后相加

  • <>你提到你是java新手,但是,在将来的某个时候,你可能会考虑用数组来实现这个程序。你会发现它会节省你现在编写的重复代码。

    总结并转化为代码指南(针对2名玩家):

    公共类MyScoreCalculator{
    静态字符串p1=“”;
    静态字符串p2=“”;
    静态整数maxScore=0;
    静态int roll1=0;
    静态int roll2=0;
    静态整数total1=0;
    静态整数total2=0;
    公共静态void main(字符串[]args){
    扫描仪键盘=新扫描仪(System.in);
    //对话获取数据。。。
    //显示带有玩家姓名的记分卡外观
    而(total1
    您的total1计算应该是
    total1+=roll
    ,并在新的roll输入之后进行。此外,如果roll1代表最后一个roll,请相应地命名变量,这样更易于阅读


    由于您有许多玩家,请尝试抽象概念并将输入与“记帐”分开。例如,您可以创建一个PlayerScore类,其中包含总计和最后一个输入(以及玩家的姓名),该方法负责添加和保存最后的输入,以及预打印信息。然后,您可以拥有一个PlayerCore集合并对其进行迭代,询问当前滚动并更新信息。

    看起来您正在将
    roll1
    分配给
    roll
    ,而不是添加其值。您可以尝试:
    roll1+=滚动;
    while循环之前的开口大括号是什么?应该用什么变量来保存总数?它是total1吗?如果是,只需将滚动加载到roll1中,然后执行total+=roll1(+=表示total=total+roll)感谢您的回复。在我的原始代码中,我确实将总数添加到了下一卷中。我一定是以某种方式忽略了这一点。我遇到的问题是,当它显示总数时,它会显示一个“0”谢谢。我终于意识到我在循环中定义总数太早了。我知道我有很多东西要学习。这是我的第一个编程语言我才干了一个星期。谢谢你的帮助though@derek缓慢而稳定,这是每个人学习编程的方式。这没什么错。我指出的第四个提示迟早会出现在你的学习道路上,就像查尔斯在他的回答中给出的建议一样,尽管这有一点更高级的东西,对于初学者来说。如果这些答案中的任何一个解决了你的问题,不要忘记接受它。
    public class MyScoreCalculator {
        static String p1 = "";
        static String p2 = "";
        static int maxScore = 0;
        static int roll1 = 0;
        static int roll2 = 0;
        static int total1 = 0;
        static int total2 = 0;
    
        public static void main(String[] args) {
            Scanner keyboard = new Scanner(System.in);
            // Dialogue to get data...
            // Display scorecard look with players names
    
            while (total1 < maxScore && total2 < maxScore) { 
                //scorecard quits when maxScore is reached by either player
                roll1 = keyboard.nextInt(); // ask for current roll
    
                System.out.println(total1 + "+");
                System.out.println(roll1);
                System.out.println("_____");
                System.out.println(roll1 + total1);
    
                total1 = total1 + roll1;
    
                // Do the same for next player.
            }
        }
    }