Notes

  • Repeated sequences to simplify code of advanced algorithms
  • ++ and -- increase or decrease a value by 1
  • While Loops
    • Runs code inside loop as long as the specified condition is true
  • For Loops
    • Executes code inside loop as long as a condition is met
  • Recursion Loop
    • A function calls itself to repeat
  • Nested Iteration
    • When you have multiple loops inside each other
    • 2D and 3D Arrays are a good example of this
      • 2D Arrays require 2 loops
      • 3D Arrays require 3 loops

Homework:

  • Write a program where a random number is generated. Then the user tries to guess the number. If they guess too high display something to let them know, and same for if they guess a number that is too low. The loop must iterate until the number is guessed correctly.
import java.util.Random;
import java.util.Scanner;

/**
 * Write a program where a random number is generated.
 * Then the user tries to guess the number.
 * If they guess too high display something to let them know,
 * and same for if they guess a number that is too low.
 * The loop must iterate until the number is guessed correctly.
 */

 
public class RandomGuess
{
    public static int magicNumber;
    public static int guess;
    
    /*
     * 
     * 
     */
    public static void main(String[] Args)
    {
        magicNumber = getRandomNumber();
        
        Scanner scanner = new Scanner(System.in);
        

        do
        {
            System.out.printf("Guess an integer between 0 and 100: ");
            guess = scanner.nextInt();
            
            if(guess < magicNumber)
            {
                System.out.printf("Higher!\n");
            }
            if(guess > magicNumber)
            {
                System.out.printf("Lower!\n");
            }
        }
        while (guess != magicNumber);
        
        if(guess == magicNumber)
        {
            System.out.printf("\nYou win! The number was %d.\n", magicNumber);
        }
        
        scanner.close();
    }
    
    /*
     * @return the random number
     */
    public static int getRandomNumber()
    {
        Random number = new Random();
         int magicNumber = number.nextInt(0, 101);
        return magicNumber;
    }
  
    
}