Cleaning up comments from the non-comment branch
This commit is contained in:
parent
c90b3a95fe
commit
f402bf42b8
@ -1,55 +1,17 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
/*
|
||||
CS2 Demo
|
||||
|
||||
System.out and System.in
|
||||
This demo goes over how to write and read text
|
||||
from the console so that we can have some basic input and
|
||||
output from our Java program
|
||||
*/
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
//System.out.println is used when you want
|
||||
//to put some text into the console when your program is
|
||||
//running. A lot of the time, you'll see the first
|
||||
//programming example your trying to learn is
|
||||
//a "Hello World!" example. System.out.println
|
||||
//is one of many ways that you can output text
|
||||
//to the console
|
||||
System.out.println("Hello World!");
|
||||
|
||||
System.out.println("This is console out!");
|
||||
|
||||
//A Scanner is used together with System.in to collect
|
||||
//text, numbers, true and false values, etc. from users
|
||||
//of your program. While Scanner is not the only
|
||||
//way of interacting with System.in, it's one of the
|
||||
//more straightforward ways
|
||||
Scanner scan = new Scanner(System.in);
|
||||
|
||||
//Notice that we use System.out.print and System.out.println
|
||||
//here, the difference is one moves the text cursor to the
|
||||
//next line after writing the text you asked for (println)
|
||||
//and one doesn't (print)
|
||||
System.out.print("Write some text: ");
|
||||
|
||||
//Scanner.nextLine() can be used to capture text
|
||||
//from the user. Below, we combine some already written
|
||||
//text with whatever the user ends up typing in. The nice
|
||||
//thing about Scanner.nextLine() is that the program
|
||||
//won't move forward until the user hits Enter on the
|
||||
//keyboard
|
||||
System.out.println("You wrote: " + scan.nextLine());
|
||||
|
||||
//Some resources, like Scanner, need to be closed. This
|
||||
//"frees" the resource so that it's available for another part
|
||||
//of this program (or another program running on the computer)
|
||||
//to access it. In this case, because we're at the end of our
|
||||
//program, closing Scanner isn't really necessary, but it's
|
||||
//a good practice to get into, because there are some resources
|
||||
//that need to be closed before your program finishes, otherwise
|
||||
//you may "break" that resource for other programs.
|
||||
|
||||
scan.close();
|
||||
}
|
||||
}
|
@ -1,42 +1,12 @@
|
||||
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: ");
|
||||
@ -57,15 +27,6 @@ public class Main {
|
||||
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();
|
||||
|
||||
|
@ -1,55 +1,14 @@
|
||||
import java.util.Random;
|
||||
|
||||
/*
|
||||
CS5 Demo
|
||||
|
||||
For Loops and Random Numbers
|
||||
This demo goes over how to write for loops and how
|
||||
there generally used. This demo also includes Java's
|
||||
Random class, so we can generate random values while using for
|
||||
loops.
|
||||
*/
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
//This for loop prints numbers from 0 through 9 (inclusive)
|
||||
//A for loop in this instance does what is known as "iterating"
|
||||
//over a series of values, in this case, we are iterating over
|
||||
//a range of numbers. In order to use a for loop in this way
|
||||
//we need to define three pieces inside the parenthesis
|
||||
//int i = 0; This first piece (initialization) defines
|
||||
//what value you are using to iterate your loop. This can
|
||||
//actually be left out (unlike the next two pieces) if you're
|
||||
//using a variable already defined elsewhere in your code
|
||||
//i < 10; This is your condition, what determines if the loop
|
||||
//should continue to run. So long as the variable i is < 10,
|
||||
//the loop runs.
|
||||
//i++ - This is our modifier, the thing that is changing to
|
||||
//ensure that at some point our loop will exit (and avoid an
|
||||
//infinite loop). i++ just means to add 1 to i. Each time the
|
||||
//loop finishes a run of whatever is inside the brackets, i++
|
||||
//will get called to update the value of i to i = i + 1;
|
||||
//For this loop, all we're doing is printing the value of
|
||||
//i as it changes while the loop runs.
|
||||
for(int i = 0; i < 10; i++) {
|
||||
System.out.println("Index Number: " + i);
|
||||
}
|
||||
|
||||
//Similar to Scanner, we can create other types of objects
|
||||
//and in this case, we are creating one called Random.
|
||||
//This just gives us access to a (pseudo) random value generator.
|
||||
//This wouldn't be good if you wanted truely random values, but
|
||||
//it'll serve out purposes well enough for now.
|
||||
Random random = new Random();
|
||||
|
||||
//With this for loop, we're trying to print 5 of each type of
|
||||
//primitive value (int, double, and boolean) that we've seen so
|
||||
//far. If all goes well, this will print the following for each
|
||||
//variable type
|
||||
//ValueName Value i+1: randomValue
|
||||
//Where ValueName is the current random value type we're trying to create
|
||||
//i is the current for loop i value
|
||||
//randomValue is the value generated by the Random object for
|
||||
//that specific type of variable.
|
||||
for(int i = 0; i < 5; i++) {
|
||||
System.out.println("Int Value " + (i + 1) + ": " + random.nextInt());
|
||||
System.out.println("Double Value " + (i + 1) + ": " + random.nextDouble());
|
||||
|
Loading…
Reference in New Issue
Block a user