import java.util.Scanner;
public class Checker
{
public static void main(String[] args)
{
int number;
// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);
// Get a number from the user.
System.out.print("Enter a number in the range of 1 through 100: ");
number = keyboard.nextInt();
while ((number < 1) || (number > 100))
{
System.out.print("Invalid input. Enter a number in the range " +
"of 1 through 100: ");
number = keyboard.nextInt();
}
}
}
Checker.main(null)
public class LoopConversion
{
public static void main(String[] args)
{
int count ;
//convert to for loop
for (count=0; count<5; count++)
{
System.out.println("count is " + count);
}
}
}
LoopConversion.main(null)
public class PopQuiz
{
public static void main(String[] args)
{
int count;
for (count=0; count<5; count++)
{
System.out.println(count);
}
int count2 = 0;
while (count2 < 5)
{
System.out.println(count2);
count2++ ;
}
}
}
PopQuiz.main(null)
Homework- Choose one or do both
- Write a program where the user inputs their monthly budget. The loop should then ask the user to input each of their monthly expenses. These expenses should be kept in a running total. The final output should display if the user is over or under their budget for the month, and by how much.
- 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.
Also complete the quiz.
HW Part 2
import java.util.Scanner;
public class Numberguesser {
public static void
guessnumber()
{
Scanner scanner = new Scanner(System.in);
int number = 1 + (int)(100* Math.random());
int i, guess;
System.out.println(
"A number is chosen between 1 to 100."
+ "Guess the number"
+ " within 5 trials.");
for (i = 0; i < 5; i++) {
System.out.println(
"Guess a number:");
guess = scanner.nextInt();
if (number == guess) {
System.out.println("You guessed the number.");
break;
}
else if (number > guess&& i != 5 - 1) {
System.out.println("The number is greater than " + guess);
}
else if (number < guess && i != 5 - 1) {
System.out.println("The number is less than " + guess);
}
}
if (i == 5) {
System.out.println(
"You have used all 5 trials.");
System.out.println(
"The number was " + number);
}
}
// Driver Code
public static void
main(String arg[])
{
// Function Call
guessnumber();
}
}
Numberguesser.main(null);
Quiz