Minecraft Blogs / Tutorial

Java Text Tutorial 4 - Math Operators

  • 250 views, 1 today
  • 1
  • 0
Cool_Man7's Avatar Cool_Man7
Level 16 : Journeyman Modder
9
And we're back! Welcome to episode 4 of Java Text Tutorials!



Table of Contentes:
4. What are math operators?
     4.1 Basic Operators
     4.2 Equality Operators
     4.3 How do I use these with my code?



4. What are math operators?
Math operators are basic symbols used with variables to manipulate their values. A simple way to put it - They change variables based off of what operator is used. These are incredibly useful for every program that involves math (such as a calculator). Using these will help you have a better game/program as they change the way things are. Such as health for instance. If you get shot your health will go down. This is where math operators come in. They will deduct your health from the amount of damage taken. This is just a small example of what you can do with math operators! These will help make your game/program go unique. Just another example - when you want to buy something from a shop, you should have your money deducted for the price of the item. If you just simply changed their money then someone who has $100000 could get reduced to $10, while someone with $5 goes and buys the same thing, he gets his balance up to $10. You see what I mean? You need to use math operators to help do this kind of stuff!


4.1 Basic Operators
There are many operators in Java that one needs to know. You need to understand their correct symbol and what they do, Here is a list of the most basic ones:
+     Addition Operator (Used for adding stuff. This works with strings as well but I will not go into that this tutorial.)
-      Subtraction Operator (Used for subtracting stuff.)
*      Multiplication Operator (Used for multiplying stuff.)
/      Division Operator  (Used to divide stuff. Note that / is used rather than \ or the typical division operator.)
%    Remainder Operator (This one is different. Whenever you divide something such as 5/2, 5/2 = 2 with 1 left over. This operator prints that one left over. Or 11/3, 11/3 = 3 with 2 left over. This is equal to that 2 left over.)

4.2 Equality Operators
These are similar to other operators however these are used for comparing things rather than changing them. Here is a list:
==   Equal to (This is used to check if something is equal to something else.)
!=    Not equal to (This is used to check if something is NOT equal to something else.)
>     Greater than (Used to check if a variable is greater than a number or another variable.)
>=   Greater than or equal to (Used to check if a variable is greater than or equal to something else.)
<     Less than (Used to check if something is less than something else.)
<=   Less than or equal to (Used to check if something is less than or equal to something else.)

4.3 How do I use these with my code?
That would depend on what you would like to do. So I've created a basic situation where they will come in use.

To subtract health from you when you get shot.
Lets just assume your health value will be set to 100. Whenever you get shot you lose 10 health. So here is our setup so far:
undefined
So whenever a player gets shot they lose 10 health. I've setup somethign that will check if someone is shot then they will lose health. Ignore the extra code just focus on two things, how I used == to see if something is true, and how I subtracted them.
undefined
Like I said, ignore what you don't know yet. What this is doing is checking if the boolean (will cover later) is true. If it is then lower the health. This is just a very simple design, but it provides an example of how operators are used. The == checks if isShot is true. If it is then it subtracts your health by 10 points. Yes, you can make variables equal to themselves and then modifying it. Just setting health = health; won't really do anything but take up space. Also, ignore the fact that I moved the integer health to inside public void getShot() as this will be covered in a later methods tutorial.

In the next tutorial we will talk about other types of variables.
CreditImage From Oracle(www.oracle.com)
Tags

Create an account or sign in to comment.

Planet Minecraft

Website

© 2010 - 2024
www.planetminecraft.com

Welcome