此代码中的最终整数值总是错误的 - 帮助! [英] Final integer value in this code is always wrong - HELP!

查看:64
本文介绍了此代码中的最终整数值总是错误的 - 帮助!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 package com.company; 
import java.util.Scanner;


class PP1CourseWork2 {
static int dieRoll(){
int roll1 =(int)(Math.random()* 1000%6 + 1);
int roll2 =(int)(Math.random()* 1000%6 + 1);
int roll3 =(int)(Math.random()* 1000%6 + 1);
int tot = roll1 + roll2 + roll3;
return tot;
}


static void AddBonus(int myValue,int bonus,String keywordx){//使用一种方法来避免为每个变量重写代码
if (myValue == 10 || myValue == 11){
System.out.println(keywordx +:+[+ myValue +]+[0]);


}否则if(myValue> 11){
bonus =(myValue-10)/ 2;
System.out.println(keywordx +:+[+ myValue +]+[++ bonus +]);

}其他{
bonus =(11 - myValue)/ 2;
System.out.println(keywordx +:+[+ myValue +]+[ - + + +]);
}
}

public static void main(String [] args){
int str = 0; //所有变量的初始化...
int dex = 0; //...where dex = Dexterity
int cha = 0; // cha = Charisma
int con = 0; // con =宪法
int wis = 0; // wis = Wisdom
int inte = 0; // inte = Intelligence

int strBonus = 0; //对应于每个变量的奖金的初始化
int dexBonus = 0;
int chaBonus = 0;
int wisBonus = 0;
int inteBonus = 0;
int conBonus = 0;

int HP = 0; //初始化HP和级别
int level = 0;

String choice = null; // String值的初始化 - 选择,用户决定重新打印或打印其统计信息。

Scanner sc = new Scanner(System.in);

System.out.println(Enter Level:);
level = sc.nextInt();



System.out.println(Level:+[+ level +]);


do {

str = dieRoll();
dex = dieRoll();
cha = dieRoll();
con = dieRoll();
wis = dieRoll();
inte = dieRoll();

AddBonus(str,strBonus,Str); //为每个变量调用AddBonus方法。
AddBonus(dex,dexBonus,Dex);
AddBonus(con,conBonus,Con);
AddBonus(inte,inteBonus,Int);
AddBonus(wis,wisBonus,Wis);
AddBonus(cha,chaBonus,Cha);

HP =(int)((Math.random()* 1000%6 + 1)+(conBonus * level));
System.out.println(HP:+[+ HP +]);

System.out.println(如果你想重新滚动,输入R.);
System.out.println(如果您希望按原样打印这些统计数据,请键入任何其他字符。);
choice = sc.next();

} while(choice.equals(r)|| choice.equals(R));

AddBonus(str,strBonus,Str);
AddBonus(dex,dexBonus,Dex);
AddBonus(con,conBonus,Con);
AddBonus(inte,inteBonus,Int);
AddBonus(wis,wisBonus,Wis);
AddBonus(cha,chaBonus,Cha);

System.out.println(HP:+[+ HP +]);


}
}







SO它是我自己的代码,根据我讲师的指导来计算DnD属性 - 真的是一项任务。然而,虽然代码在准则方面接近完美 - 但HitPoints或HP的价值总是错误的,我无法弄清楚为什么在地球上就是这样。它应该是1-6 +(级别* conBonus)之间的随机数的值,但即使我的水平是一百万,它总是返回1-6之间的数字 - 大多数是2或3.任何问题除了我能看到吗?



我的尝试:



我有尝试逐行调试但似乎找不到错误。

解决方案

编译并不意味着你的代码是正确的! :笑:

将开发过程想象成编写电子邮件:成功编译意味着您使用正确的语言编写电子邮件 - 例如英语而不是德语 - 而不是电子邮件包含您的邮件想发送。



所以现在你进入第二阶段的发展(实际上它是第四或第五阶段,但你将在之后的阶段进入):测试和调试。



首先查看它的作用,以及它与你想要的有何不同。这很重要,因为它可以为您提供有关其原因的信息。例如,如果程序旨在让用户输入一个数字并将其翻倍并打印答案,那么如果输入/输出是这样的:

输入预期输出实际输出
1 2 1
2 4 4
3 6 9
4 8 16

然后很明显问题出在将它加倍的位 - 它不会将自身加到自身上,或者将它乘以2,它会将它自身相乘并返回输入的平方。

所以,你可以查看代码和很明显,它在某处:

  private   int   Double  int   value 
{
return value * value ;
}



一旦你知道可能出现的问题,就开始使用调试器找出原因。在你的线上设一个断点:

 str = dieRoll(); 

并运行你的应用程序。在执行代码之前,请考虑代码中的每一行应该做什么,并将其与使用Step over按钮依次执行每一行时实际执行的操作进行比较。它符合您的期望吗?如果是这样,请转到下一行。

如果没有,为什么不呢?它有何不同?



这是一项非常值得开发的技能,因为它可以帮助你在现实世界和发展中。和所有技能一样,它只能通过使用来改进!


Quote:

AddBonus(con,conBonus, con);



Java 中,方法参数始终按值传递:对的任何更改conBonus 在方法内执行的变量是调用者看不到的(也就是 0 main 方法)。

我建议你改变方法,以便返回奖金修改后的价值。然后用这种方式调用

 conBonus = addBonus(con,  CON); 


package com.company;
import java.util.Scanner;


class PP1CourseWork2 {
    static int dieRoll() {
        int roll1 = (int) (Math.random() * 1000 % 6 + 1);
        int roll2 = (int) (Math.random() * 1000 % 6 + 1);
        int roll3 = (int) (Math.random() * 1000 % 6 + 1);
        int tot = roll1 + roll2 + roll3;
        return tot;
    }


    static void AddBonus(int myValue, int bonus, String keywordx) { //Using a method to avoid rewriting codes for each and every variable
        if (myValue == 10 || myValue == 11){
            System.out.println(keywordx + " : " + "[" + myValue + "]" + "[0]");


        } else if (myValue > 11) {
            bonus = (myValue-10)/2;
            System.out.println(keywordx + " : " + "[" + myValue + "]" + "[+" + bonus + "]");

        } else {
            bonus = (11 - myValue)/2;
            System.out.println(keywordx + " : " + "[" + myValue + "]" + "[-" + bonus + "]");
        }
    }

    public static void main(String[] args) {
        int str =0;             //Initialization of all variables...
        int dex=0;              //...where dex = Dexterity
        int cha=0;              //cha = Charisma
        int con=0;              //con = Constitution
        int wis=0;              //wis = Wisdom
        int inte=0;             //inte = Intelligence
        
        int strBonus=0;         //Initialization of bonuses corresponding to each variable
        int dexBonus=0;
        int chaBonus=0;
        int wisBonus=0;
        int inteBonus=0;
        int conBonus=0;
        
        int HP=0;               //Initialization of HP and level
        int level=0;

        String choice = null;   //Initialization of the String value - choice, where user decides to reroll or print their stats.

        Scanner sc = new Scanner(System.in);

        System.out.println("Enter Level: ");
        level = sc.nextInt();



        System.out.println("Level :" + "[" + level + "]");


        do {

            str = dieRoll();
            dex = dieRoll();
            cha = dieRoll();
            con = dieRoll();
            wis = dieRoll();
            inte = dieRoll();

            AddBonus(str, strBonus, "Str");         //calling the AddBonus method for each and every variable.
            AddBonus(dex, dexBonus, "Dex");
            AddBonus(con, conBonus, "Con");
            AddBonus(inte, inteBonus,"Int");
            AddBonus(wis, wisBonus, "Wis");
            AddBonus(cha, chaBonus, "Cha");

            HP = (int) ((Math.random() * 1000 % 6 + 1) + (conBonus * level));
            System.out.println("HP :" + "[" + HP + "]");

            System.out.println("If you wish to re-roll, type R.");
            System.out.println("If you wish to print these stats as they are, type any other character.");
            choice = sc.next();

        } while (choice.equals("r") || choice.equals("R"));

        AddBonus(str, strBonus, "Str");
        AddBonus(dex, dexBonus, "Dex");
        AddBonus(con, conBonus, "Con");
        AddBonus(inte, inteBonus, "Int");
        AddBonus(wis, wisBonus, "Wis");
        AddBonus(cha, chaBonus, "Cha");

        System.out.println("HP :" + "[" + HP + "]");


    }
}




SO there it is- my own code to calculate DnD attributes based on guidelines from my lecturer - an assignment, really. However, while the code is near perfect in terms of the guidelines - the value of HitPoints, or HP - is always wrong and I cannot figure out why on Earth it's like that. It's supposed to be the value of a random number between 1-6 + (level*conBonus), but even if my level is a million, it always returns a number between 1-6 - mostly 2 or 3. Any issues anyone other than me can see?

What I have tried:

I have tried line by line debugging but cannot seem to find an error.

解决方案

Compiling does not mean your code is right! :laugh:
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:

Input   Expected output    Actual output
  1            2                 1
  2            4                 4
  3            6                 9
  4            8                16

Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:

private int Double(int value)
   {
   return value * value;
   }


Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on your line:

str = dieRoll();

and run your app. Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?

This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!


Quote:

AddBonus(con, conBonus, "Con");


In Java, method parameters are always passed by value: any change to conBonus variable performed inside the method is invisible by the caller (that is it remains 0 inside the main method).
I suggest you to change the method in order to return the bonus modified value. Then call it this way

conBonus = addBonus(con, "Con");


这篇关于此代码中的最终整数值总是错误的 - 帮助!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆