Question

In: Computer Science

The third task is to further refine the program so that it uses a loop to...

The third task is to further refine the program so that

  1. it uses a loop to keep asking the user for numbers until the user gets the correct number.

HINT: You need functions eight from the sample code (see previous activity).

Steps

Please use Netbeans to revise your NumberGuessGame program to satisfy the above requirements. Please submit a print screen of your successful code including the Output window with the "Build Successful" message to this assignment.

Solutions

Expert Solution

Code:

import java.util.Random;
import java.util.Scanner;
class guessnum
{
   public static void main(String[] args)
   {
       Random random=new Random();/*Random object*/
       int randnum=random.nextInt(100)+1;/*Random number between 1 to 100*/
       int guess=0;/*Declaring variable*/
       Scanner scnr=new Scanner(System.in);/*Scanner object*/
       System.out.println("Guess the Number:");
       for(int i=0;guess!=randnum;i++)
       {
           guess=scnr.nextInt();/*Reading users guess*/
           if(guess>randnum)
           {/*If guess is greater than random*/
               System.out.println("Too High\nTry again!");
           }
           else if(guess<randnum)
           {/*If guess is less than random*/
               System.out.println("Too Low\nTry again!");
           }
           else
           {/*If guessed correctly*/
               System.out.print("Congratulation you guessed the number");
           }
       }  
   }
}

Output:

Indentation:


Related Solutions

Write a program that uses loops (both the for-loop and the while loop). This assignment also...
Write a program that uses loops (both the for-loop and the while loop). This assignment also uses a simple array as a collection data structure (give you some exposure to the concept of data structure and the knowledge of array in Java). In this assignment, you are asked to construct an application that will store and retrieve data. The sequence of data retrieval relative to the input is Last In First Out (LIFO). Obviously, the best data structure that can...
Write a program that uses a for loop to print One of the months of the...
Write a program that uses a for loop to print One of the months of the year is January One of the months of the year is February ...
Write a program using c++. Write a program that uses a loop to keep asking the...
Write a program using c++. Write a program that uses a loop to keep asking the user for a sentence, and for each sentence tells the user if it is a palindrome or not. The program should keep looping until the user types in END. After that, the program should display a count of how many sentences were typed in and how many palindromes were found. It should then quit. Your program must have (and use) at least four VALUE...
IN JAVA Create a program that uses a nested for loop to display a rectangle of...
IN JAVA Create a program that uses a nested for loop to display a rectangle of #'s with a given number of rows and columns,. This should only display the # when the column is an odd number (see examples below). Get the number of rows and columns from the user, and display the result. Examples: If the user provided rows=4, and columns=7, the program should display a pattern as follows: # # # # # # # # #...
e. Task 5 (3 pts): (This task uses Strings and an if..then..elseif cascade) A program that...
e. Task 5 (3 pts): (This task uses Strings and an if..then..elseif cascade) A program that prompts the user for their party affiliation (Democrat, Republican, or Independent) and responds accordingly with a Donkey,
2) create a python program that uses a for loop and range to print out the...
2) create a python program that uses a for loop and range to print out the values 10 8 6 4 2 3) Create a python program that yses a for loop to print out ["bob","al","bert"]
Write a program that uses a while loop with a priming read to ask the user...
Write a program that uses a while loop with a priming read to ask the user to input a set positive integers. As long as the user enters a number greater than -1, the program should accumulate the total, keep track of the number of numbers being entered and then calculate the average of the set of numbers after the user enters a -1. This is a sentinel controlled-loop. Here is what a sample run should look like: Enter the...
In visual Studio C++ Create a program that uses a for loop to input the high...
In visual Studio C++ Create a program that uses a for loop to input the high temperature, and low temperature for each day of the week. The high and low will be placed into two elements of the array. For each loop the high and low will be placed into the next set of elements of the array. After the temps for all seven days have been entered into the array, a for loop will be used to pull out...
In C++ using a single dimensional array Create a program that uses a for loop to...
In C++ using a single dimensional array Create a program that uses a for loop to input the day, the high temperature, and low temperature for each day of the week. The day, high, and low will be placed into three elements of the array. For each loop the day, high, and low will be placed into the next set of elements of the array. After the days and temps for all seven days have been entered into the array,...
Write a program that uses a for loop output a string in reverse printing it character...
Write a program that uses a for loop output a string in reverse printing it character by character. C++ Program Example: "Enter some text:" this is a test "your text in reverse is:" tset a si siht Please note that you can find the length of the string by using the length function: string str = "hello" cout << str.length(); Also you can et a character in a string by providing an index to the function string str = "hello"...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT