How do you add the value of one integer to another? [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I took it upon myself to make an RPG game, and already it's starting to confuse me a little.
In the fighting part of the game, I use (what is probably) a pretty bad system to register how much damage the enemy takes when you attack it.
int healthStatPlayer = 1;
Double damageTaken = 0.0;
Double maxHealth = (healthStatPlayer * 100.0);
Double playerHealth = (maxHealth - damageTaken);
...
Double attackDamageEnemy (attackStatEnemy * damageMultiplierEnemy);
...
{
if (actionCommand.equals("Attack"))
{
System.out.println("You dealt " + attackDamage + " damage to the enemy.");
damageTakenEnemy = (damageTakenEnemy + attackDamage);
//The above line is where the problem is, it's wrong - both with regards to code and to mathematics - because I don't know how to make it right
System.out.println("The enemy has " + enemyHealth + " health
left.");
}
}
I had it set up so that the current health of the enemy is its max health minus any damage taken. This meant that, to be able to alter the current health, I would have to add the value of the player's attack damage to the enemy's "damage taken."
I just want to be able to add the number value of attackDamage (in this case, 22.5) to the value of damageTakenEnemy (initially 0, since all enemies start with max health).

Your attackDamageEnemy variable is missing an = and you need to make sure to set the enemyHealth to enemyHealth - damageTakenEnemy otherwise the variable will never actually be affected by your damage values.

Just use the plus operator.
So it would be myVariable + myOtherVariable, you can also store this in a variable like this myStoreVar = myVar + myOtherVar.
If you want to increment a variable with something you can do myStoreVar += myVar, which will just plus myVar to myStoreVar.
In your case the answer would be damageTakenEnemy += attackDamage which is the same as damageTakenEnemy = damageTakenEnemy + attackDamage. You can clearly see here which to use, the shorter one.
You also have operators like -, *, / and %.

Related

Values printing in an undesired location [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 days ago.
Improve this question
I am trying to create a code that calculates the y value based on the value of X. Here is a segment.
for(x = -3.0; x <= 4.0; x += 0.50) {
y= (9*x*x*x - 27*x*x - 4*x + 12)/(Math.sqrt(3*x*x + 1) + Math.abs(5 - x*x*x*x));
System.out.printf("\n\tX = %9.1f\t\tY = %13.3f",x,y);`
I tried to run this code, and the table came out good, however the X and Y values are printing far away from the X = and Y =. I was expecting the values to print right next to the = sign.
To left-justify the values you can prefix the placeholder with a dash, like so:
System.out.printf("\n\tX = %-9.1f\t\tY = %-13.3f", x, y);
Difference:
%-9.1f instead of %9.1f
%-13.3f instead of %13.3f

Having some problems with SalesByQtr Assignment [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have an assignment that requires me to make the SalesByQtr java program i've gotten most of the program done but I have a couple of problems
Firstly: My total is being displayed as a double which I know I need printf to add a decimal place. The same thing is happening for Average.
Second: My division with the highest sales is saying Division 1 has the highest sales in qtr 1 and it repeats this in each qtr also Division 1 does not have the highest sales
Finally: My assignment requires that in qtrs 2-4 to display the change in sales and I can't figure out how to do that.
Here is the example output my professor is looking for
Example Output (bullet points represent spaces)
Q1:
D1:·1000.00↵
D2:·1700.00↵
D3:·300.00↵
D4:·2000.00↵
D5:·1400.00↵
D6:·500.00↵
Total:·6900.00↵
Average·sales:·1150.00↵
Division·with·highest·sales:4↵
Q2:
D1:·2500.00,·Change:·1500.00↵
D2:·500.00,·Change:·-1200.00↵
D3:·700.00,·Change:·400.00↵
D4:·2500.00,·Change:·500.00↵
D5:·1450.00,·Change:·50.00↵
D6:·1050.00,·Change:·550.00↵
Total:·8700.00,·Change:·1800.00↵
Average·sales:·1450.00↵
Division·with·highest·sales:1↵
Q3:
D1:·3000.00,·Change:·500.00↵
D2:·1000.00,·Change:·500.00↵
D3:·120.00,·Change:·-580.00↵
D4:·2700.00,·Change:·200.00↵
D5:·980.00,·Change:·-470.00↵
D6:·750.00,·Change:·-300.00↵
Total:·8550.00,·Change:·-150.00↵
Average·sales:·1425.00↵
Division·with·highest·sales:1↵
Q4:
D1:·2700.00,·Change:·-300.00↵
D2:·1300.00,·Change:·300.00↵
D3:·155.00,·Change:·35.00↵
D4:·3500.00,·Change:·800.00↵
D5:·1450.00,·Change:·470.00↵
D6:·1023.00,·Change:·273.00↵
Total:·10128.00,·Change:·1578.00
Average·sales:·1688.00
Division·with·highest·sales:4
Here's my code:
My Code
public static void main(String[] args) {
int divs = 6;
int qtrs = 4;
double errorCheck;
double[][] sales = new double[divs][qtrs];
double[] qtrsales = new double[qtrs];
int highestDiv = 0;
int[] highestDivi = new int[qtrs];
Scanner keyboard = new Scanner(System.in);
for (int div = 0; div < divs; div++) {
for (int qtr = 0; qtr < qtrs; qtr++) {
System.out.printf("Enter sales figures for division %d, quarter %d:", (div + 1), (qtr + 1));
errorCheck = keyboard.nextDouble();
while (errorCheck < 0) {
System.out.printf("Enter sales figures for division %d, quarter %d:", (div + 1), (qtr + 1));
errorCheck = keyboard.nextDouble();
}
sales[div][qtr] = errorCheck;
}
}
for (int qtr = 0; qtr < 4; qtr++) {
System.out.printf("Q%d:\n", (qtr + 1));
for (int div = 0; div < divs; div++) {
qtrsales[qtr] += sales[div][qtr];
System.out.printf("\tD%d: %.2f\n", (div + 1), sales[div][qtr]);
}
for (int qtrS = 0; qtrS < 1; qtrS++) {
System.out.printf("Total:" + qtrsales[qtr] + ", Change:");
System.out.printf("Average sales:" + (qtrsales[qtr] / divs));
}
for (int div = 0; div < 1; div++) {
highestDiv = 0;
if (sales[highestDiv][qtr] < sales[(div + 1)][qtr]) {
highestDiv = (div + 1);
}
highestDivi[qtr] = highestDiv;
System.out.println("Division with the highest sales:" + highestDivi[qtr]);
}
}
}
}
So here is the output that I think you want:
Q1:
D1: 1234.45
D2: 1232.54
D3: 23987.54
D4: 1233.56
D5: 234.56
D6: 1234.45
Total: 29157.10
Average sales: 4859.52
Division with the highest sales: 3
Q2:
D1: 234.56
D2: 1234.45
D3: 1232.54
D4: 23987.54
D5: 1233.56
D6: 234.56
Total: 28157.21 (change: 28157.21)
Average sales: 4692.87
Division with the highest sales: 4
Q3:
D1: 1233.56
D2: 234.56
D3: 1234.45
D4: 1232.54
D5: 23987.54
D6: 1233.56
Total: 29156.21 (change: 29156.21)
Average sales: 4859.37
Division with the highest sales: 5
Q4:
D1: 23987.54
D2: 1233.56
D3: 234.56
D4: 1234.45
D5: 1232.54
D6: 23987.54
Total: 51910.19 (change: 51910.19)
Average sales: 8651.70
Division with the highest sales: 1
IMPORTANT: If that's not what you want, then I've managed to fail at following directions and you probably shouldn't listen to anything else I have to say.
This output was had by making a number of small changes. Of course, I'm not going to hand them over to you (that would be doing your homework for you, and for that I would need to charge you an exorbitant sum of money) so instead I'll just point you in the right direction.
To start with, there is this bit of code:
for (int qtrS = 0; qtrS < 1; qtrS++)
Study that line of code carefully and discover what it is actually doing. I'll give you a hint: it isn't wrong necessarily, but there is something you could do to improve it.
You have a similar loop further down:
for (int div = 0; div < 1; div++)
Study this one more carefully - it may appear to have the same problem as the previous, but it does not. Once you solve this riddle, your remaining issues will present themselves more clearly. (Note: if your 'fix' causes your program to crash - good. Keep going, you're on the right track.)
In addition, I'd like to offer the following advice for that final loop:
Your data uses a zero-based index. Looking at your code, you already know this. Your output in some cases is one-based (you print Q1, not Q0). Again, looking at your code, you already know this. My advice is to be consistent about how you deal with this indexing discrepancy. Specifically, always index your data using zero-based indexes and only + 1 those indices when printing the output for the user.
Now... about that Change calculation. If you don't know how to do something, try explaining the problem to yourself in very clear and precise terms (like writing a program, but without the overhead of language syntax, array indexing and all that other complicated mumbo-jumbo.) If you did, it might sound something like this:
"I need to calculate the change for quarters 2-4. This means that I need to print a change for each of those quarters (Q2, Q3, Q4). More specifically, I'll need three "change values". How would a change value be calculated? Starting at one quarter and figuring out how much changed to the next? That's just a subtraction. So all I really need is find a way to get those two values (for each quarter) so I can perform that subtraction. Oh, and I have to make sure to only do it for those quarters (2-4)."
Remember, the trick is to bob your head while talking to yourself in the car so people think you're singing along.
Good luck.

generate random objects to get spawning for java [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm trying to generate a random double for this section of my assignment.
The question is
"Each week, each female Guppy who is 10 weeks old or older has a 25 percent chance of spawning".
I would like to generate a random double to determine if each female Guppy should spawn or not.
My code so far:
Random r = new Random();
if (isFemale == true && getAgeInWeeks() >= 10) {
//?
}
I don't see any reason to generate a random double according to your question. What you need is an integer ranging from 0 to 3 inclusive where each number account for 25% for the spawning.
Random r = new Random();
if (isFemale == true && getAgeInWeeks() >= 10) {
// Generate a random integer in [0,3]
// Since there is a 25% or 1/4 chance
if(Math.abs(r.nextInt()) % 4 == 1){
//Note that the 1 in the condition can be replaced by any
// integer in [0,3]
//Put spawning code here
}
}
Check out this link for more information on random:
Random (Java Platform SE 7
To generate a random double, you can look at this question, however, this problem can more easily be solved by generating random ints.
In your situation, generating an int between 0 to 3 is what you want because checking if it is 0 will be true 25% of the time (1 Value / 4 Possible values = 25%).
EDIT: If you would like to also generate a random number to see how many spawn a Guppy will have use threadLocalRandomInstance.nextInt(int bound); like before.
These constraints can be translated to code like this:
import java.util.concurrent.ThreadLocalRandom;
public class Test {
public static void main(String[] args) {
ThreadLocalRandom tlr = ThreadLocalRandom.current();
int num = tlr.nextInt(3 + 1); //Bound is exclusive so add 1.
int spawn;
if(num == 0) {
spawn = tlr.nextInt(100 + 1); //Again, bound is exclusive so add 1.
} else spawn = 0;
System.out.println("This guppy had " + spawn + " spawn.");
}
}
I use ThreadLocalRandom as it is more straightforward as supported by this answer.
If you are not using Java 1.7+, use Random#nextInt(int) instead as also shown by that answer.

Logic errors within java segment [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
The following Java code segment is supposed to print, as a double, the mean average of a sequence of non-negative integers entered by the user. A negative input signals the end of the sequence (it is not itself part of the sequence). However, the code is not working. I am supposed to find 4 logic errors within this segment. Please help me find the 4 logic errors?? I know one is its integer division.
public class practice
{
public static void main (String[]args)
{
int sum = 0;
int numVals = 0;
Scanner scan = new Scanner(System.in);
System.out.println(("enter next integer (-ve to stop): "));
int i = scan.nextInt();
while (i > 0)
{
sum = sum + i;
numVals = numVals + 1;
}
System.out.println("average = " + sum / numVals);
}
}
I won't give you full solution, however, it'll be helpful if you pay attention to:
int division as you said,
does your loop terminate? Is someone changing i? Hint: No, and
how do you ask for input? Do you see any loops there? Why it's asking you for only one input?
Not an error, but pay attention to Java Naming Conventions, class name should begin with upper case
Since the homework is for logic errors, I could point out other errors.
the class name should start with upper case letter.
the println doesn't need two nested parenthesis.
the sum should be a long rather than an int to avoid overflows.

How to output game properly [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have been trying to fix my snakes and ladders code. For example if Player1's position on the gameboard is 98 and he rolls a 5 Player1's position should be 97. So,(100-98=2,(5-2)=3,100-3=97). I ahve been trying to implement this into my code in the possiblity that either player1 or player2 roll higher than 100.
MY CODE:
System.out.println (player1+" Rolled a " + P1Roll );
System.out.println (player2+" Rolled a " + P2Roll);
//If player1 position is greater than 100
if(P1Position+P1Roll>100){
P1=100-P1Position;
difference=P1Position-P1Roll;
P1Position=100-difference;
}
//If palyer2 position is greater than 100
else if(P2Position+P2Roll>100){
P2 = 100-P2Position;
difference=P2Position-P2Roll;
P2Position=100-difference;
}
System.out.println ("------------------------------------------------------------------------");
//calculate player positions
P1Position = P1Position + P1Roll;
P2Position = P2Position + P2Roll;
//call position methods
P1Position = Player1(P1Position, P1Roll, snakes, ladder, arrow);
P2Position = Player2(P2Position, P2Roll, snakes, ladder, arrow);
//Print out players current positions
System.out.println("==========================================================================");
System.out.println (player1+" is currently on square " + P1Position);
System.out.println (player2+" is currently on square " + P2Position);
System.out.println("==========================================================================");
According to your calculation it should be
difference=P1Roll-P1;
And for player 2:
difference=P2Roll-P2;
And try this more simple way for all cases:
P1Position = 100 - Math.abs(P1Position + P1Roll - 100);
Try removing the else after player 1, unless the methods are called after 1 player rolls.
(Another way of saying it: do both players roll, then you move them? If yes, remove the else. If player 1 rolls, and then you update, then keep the else. I don't see anything else wrong otherwise).
The else statement is treating the conditionals as such: "Is player 1's position plus roll greater than 100? If so, do this block, and that's it. If player 1's position plus roll is NOT greater than 100, try the next block". So, if player 1 does have to be "manipulated" using the if block, player two may never get the move to happen.
I hope my answer is not too confusing.

Categories