Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)
Paste
Pasted as Java by pblakw ( 5 years ago )
/**
* This program gives a basic overview of
* math operations in java
*
* @pblake
* @2-25-21
*/
import java.util.Scanner;
public class BasicMath
{
public static void main(String [] args)
{
//this is an inline comment
//This is an example of an int variable
int x = 20;
//This creates an integer variable called x with a value of 5
int y = 7;
//ints represent posiitve or negative whole numbers
//Basic operations
System.out.println("x has value " + x);
System.out.println("y has value " + y);
//Adding two numbers
System.out.println("x + y = " + (x + y));
//Subtracting
System.out.println("x - y = " + (x - y));
//Multiplying
System.out.println("x * y = " + (x * y));
//Dividing is a little weird
System.out.println("x / y (as integers) = " + ( x / y));
/*
* Multi line comment
* When performing operations with integers, the result is
* also an integer.
* That said 20 / 7 = 2.857, but only returns 2
* 2 is the integer part of that answer.
*/
//Order of operations
int z = 4;
//x is 20, y is 7
//Order of operations PEMDAS
System.out.println(x - (y * z) + x / z);
// 20 - (28) + 20 / 4
// 20 - 28 + 5
// -8 + 5 (adding and substracting are on the same level)
// -3 (simply work left to right)
//Prompting for a int
Scanner in = new Scanner(System.in);
System.out.println("Please enter an integer : " );
int input = in.nextInt();
int output = input * 3 + 5;
System.out.println("3 times your number plus 5 is " + output);
}
}
Revise this Paste