Initial Commit
This commit is contained in:
83
CS3 - Demo/Main.java
Normal file
83
CS3 - Demo/Main.java
Normal file
@@ -0,0 +1,83 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
/*
|
||||
CS3 Demo
|
||||
|
||||
Variables and Basic Math
|
||||
This demo goes over how to store values in variables.
|
||||
These values can be predefined in your code, come from
|
||||
mathematical and other types of operations, or be collected
|
||||
from the user in some way. This demo also covers general
|
||||
math operations in Java
|
||||
*/
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
|
||||
//Below are some examples of program defined variables. Variables
|
||||
//that you would set up in your code, and use elsewhere. There are 4 different
|
||||
//types of variables here, all capable of storing different things.
|
||||
//String = Text, int = Whole Numbers, double = decimal (real) numbers, boolean = true/false
|
||||
//These are common variable types that you'll use in every Java program, but
|
||||
//there are many other types of variables. Scanner, as we have also seen, is
|
||||
//a type of variable.
|
||||
//Notice the yellow lines under the variable names, this is just VSCode letting
|
||||
//us know that the variables we've defined are never used, and therefore not needed
|
||||
//They are here only for an example
|
||||
String someString = "Some string value";
|
||||
int lifeUniverseEverything = 42;
|
||||
double someDouble = 2.22;
|
||||
boolean bananasAreGood = true;
|
||||
|
||||
//Below are several examples on how to retrieve different types of values from the
|
||||
//user. Strings, ints, booleans, and doubles can be grabbed using there appropriate
|
||||
//method, nextLine for Strings, nextInt for ints, nextBoolean for booleans, and
|
||||
//nextDouble for doubles. Each of these values are stored separately and then
|
||||
//reprinted down below to the user.
|
||||
//Notice how we can use the plus sign to combine other data types with Strings
|
||||
//to be printed. Java is smart enough to know that those values need to be
|
||||
//converted to String in order to combine properly. Just be careful, there
|
||||
//are scenarios were this won't work.
|
||||
Scanner scan = new Scanner(System.in);
|
||||
|
||||
System.out.print("Enter a string: ");
|
||||
String string = scan.nextLine();
|
||||
|
||||
System.out.print("Enter a integer: ");
|
||||
int intValue = scan.nextInt();
|
||||
|
||||
System.out.print("Enter a true/false value: ");
|
||||
boolean booleanValue = scan.nextBoolean();
|
||||
|
||||
System.out.print("Enter a double value: ");
|
||||
double doubleValue = scan.nextDouble();
|
||||
|
||||
System.out.println("The values you entered");
|
||||
System.out.println("String: " + string);
|
||||
System.out.println("Integer: " + intValue);
|
||||
System.out.println("Boolean: " + booleanValue);
|
||||
System.out.println("Double: " + doubleValue);
|
||||
|
||||
//Below common math operations are displayed, you should be familiar with
|
||||
//addition, multiplication, subtraction, and division already. But you may
|
||||
//not be familiar with modulous or remainder operations (the % sign operation).
|
||||
//This divides two integers (NOT DOUBLES) and rather than return the division result
|
||||
//it returns the remainder, so rather than 5 / 3 = 1, you have 5 % 3 = 2
|
||||
//Notice that the math operations are encased in parenthesis inside the
|
||||
//call to System.out.println, this is done to ensure Java's automatic String combination
|
||||
//mechanism doesn't get confused, and that our math operations are performed
|
||||
//before trying to combine the result with the rest of the String.
|
||||
System.out.print("Enter the first double value: ");
|
||||
double firstNumber = scan.nextDouble();
|
||||
|
||||
System.out.print("Enter the second double value: ");
|
||||
double secondNumber = scan.nextDouble();
|
||||
|
||||
System.out.println("" + firstNumber + " + " + secondNumber + " = " + (firstNumber + secondNumber));
|
||||
System.out.println("" + firstNumber + " - " + secondNumber + " = " + (firstNumber - secondNumber));
|
||||
System.out.println("" + firstNumber + " * " + secondNumber + " = " + (firstNumber * secondNumber));
|
||||
System.out.println("" + firstNumber + " / " + secondNumber + " = " + (firstNumber / secondNumber));
|
||||
System.out.println("" + (int)firstNumber + " % " + (int)secondNumber + " = " + (((int)firstNumber) % ((int)secondNumber)));
|
||||
|
||||
scan.close();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user