Click Here
```java
import java.util.Scanner;
public class Name {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int randomNumber = (int) (Math.random() * 100) + 1;
int userGuess;
System.out.println("Welcome to the Number Guessing Game!");
System.out.println("I've picked a number between 1 and 100. Try to guess it!");
do {
System.out.print("Enter your guess: ");
userGuess = scanner.nextInt();
if (userGuess == randomNumber) {
System.out.println("Congratulations! You guessed the correct number: " + randomNumber);
break;
} else {
int difference = Math.abs(randomNumber - userGuess);
if (difference <= 10) {
System.out.println("You're very close! Try again.");
} else if (difference <= 20) {
System.out.println("You're close! Try again.");
} else if (difference <= 30) {
System.out.println("You're getting warmer! Try again.");
} else {
System.out.println("You're far away! Try again.");
}
}
} while (true);
scanner.close();
}
}
```
### Activity 4.1b - convert program
**Directions:** Convert Program A and Program B from a `for` loop to a `do-while` loop.
```java
//Program A
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a String with at least 3 characters: ");
String input = sc.nextLine();
String pattern = "aaa";
int howMany = 0;
for (int i = 0; i < input.length() - pattern.length() + 1; i++)
{
String currSeq = input.substring(i, i + pattern.length());
if (currSeq.equals(pattern))
howMany++;
}
System.out.println("There were " + howMany +
" substrings matching " + pattern);
}
}
```
```java
// Program B
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a String: ");
String input = sc.nextLine();
String output = "";
for (int i = input.length(); i > 0; i--)
output += input.substring(i - 1, i);
System.out.println(output);
}
}
```
### Activity 4.1c
**Directions:** Write a program using a `do-while` loop that does the following:
- Write a do-while loop that asks a user for their name, which must be at least four characters long.
- Write another do-while that asks for the user’s age, which must be between 18 and 65, inclusive. Recall the nextIntmethod Scanner class.
**Sample Output**
```java
What is your first name?
Jon
Your name needs to be at least 4 characters long.
What is your first name?
Jonathan
What is your current age?
50
Jonathan is 50 years old.
```
You have learned about primitive data types like `int`, `double`, and `boolean`. Java has a `char` data type that stores a single character. When using a `char` data type, use single quotes to assign a character to your `char` variable. For example:
`char reply = 'y';`
Java chars and strings are closely related. A String is a sequence of chararcters that reference an object. Using a new method, you can find individual characters in a String. Remember, a `char` is a primitive data type, you can compare primitive data types with == in conditionals.
---------------------------------------------------------------------------------------
### Assignment `char` - Oracle
**Directions:** Using a do-while, validate the input so the user types only one character. Write a `switch` statement to show the character “is a vowel” or “ is a consonant.”
```java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a letter");
String str = sc.nextLine();
}
}
```
**Sample Output**
```java
Enter a letter
b
b is a consonant.
```
### Break and Continue Keywords in Java - Oracle
Break and Continue keywords in Java
The `Break` statement in Java is used most often in one of the two cases below.
- Break quits the loop and jumps out of it (both for and while).
- Break statement exits a case in the switch statement.
Example 1
```java
public class Main {
public static void main(String[] args) {
// Testing break statement in while loop
System.out.println("Test Break statement in While loop");
int i = 0;
while (i < 5) {
if (i == 2) {
break;
}
System.out.println(i++);
}
}
}
```
Sample Output
```java
Test Break statement in While loop
0
1
```
The `continue` statement in Java is most often used in one of the two cases below.
- It skips the following statements and moves to the next iteration in the for loop.
- Continue in while loops hop the following statements and jump to the conditional statement.
Example 2
```java
public class Main {
public static void main(String[] args) {
// Testing continue statement in while loop
System.out.println("Test Continue in While loop");
int i = 0;
while (i < 5) {
if (i == 2) {
i++;
continue;
}
System.out.println(i++);
}
}
}
```
**Sample Output**
```java
Test Continue in While loop
0
1
3
4
```
**Differences between continue and break**
The considerable difference between break and continue is that the break exits a loop at once. Once a break statement is executed, the loop will not run again. However, after executing the continue statement, the following lines of code will be skipped for the current iteration only. The loop will begin to execute again.
**Break and Continue in While Loop**
Both `Break` and `Continue` can be used in a `while` loop.
Example 1
```java
public class Main {
public static void main(String[] args) {
// Testing both break and continue statements side by side
String [] weekdays = { "Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday" };
System.out.println("Test Break statement in While loop");
System.out.println("\nWorking Days:\n");
int i = 0;
while (i < weekdays.length ) {
if (weekdays[i].equals("Saturday") || weekdays[i].equals("Sunday")) {
i++;
break;
// Not any working day will be printed
// because the loop breaks on Sunday
// once the loop breaks it moves out of the loop
}
System.out.println(weekdays[i++]);
}
System.out.println("\nTest Continue statement in While loop");
System.out.println("\nWorking Days:\n");
int j = 0;
while (j < weekdays.length ) {
if (weekdays[i].equals("Saturday") || weekdays[i].equals("Sunday")) {
j++;
continue;
// All the working/business days will be printed
// when the loop encounters Saturday or Sunday
// it skips that iteration and continues to the next iteration
}
System.out.println(weekdays[i++]);
}
}
}
```
**Sample Output**
```java
Test Break statement in While loop
Working Days:
Test Continue statement in While loop
Working Days:
Monday
Tuesday
Wednesday
Thursday
Friday
```
The `break` and `continue` keywords can be a valuable tool when writing Java programs.
### Activity Break and Continue - Oracle
**Directions** Modify the `selection sort` program below by using the break and continue keywords.
- Use a `break` statement to stop the selection sort when it encounters a negative value.
- Show a message that the sort was aborted.
- Use a `continue` statement to continue the sort when the next value is a positive value.
```java
import java.util.ArrayList;
public class Main
{
public static void main(String[] args)
{
ArrayList