In: Computer Science
We will create a program that will guess a number between 1 and 100 chosen by the user.Your program will be iteratively built in several stages, where each stage focuses on implementing just one type of operation. Each stage will build upon the work from the previous stage. This is known as iterative design, a process that allows us to focus on working on a single, simple task at a time, but the resulting software slowly evolves into something much more complex.
Required Concepts: The final homework requires that you implement the following concepts: 1. Storage operations (store data in program) 2. Output operations (print data to screen) 3. Input operations (get data from user) 4. Selection operations (multiple selection based on player input) 5. Mathematical operations (arithmetic, equality, relational) 6. Repetition operations (loop game logic to model multiple turns) 7. Nested selection operations (validate that user’s selection meets rules requirements) CODE IN JAVA!!
import java.util.Random;
import java.util.Scanner;
public class GuessNumber {
public static void main(String[] args) {
Scanner sc = new
Scanner(System.in);
Random r = new Random();
//generating random number
int rand = r.nextInt(100);
System.out.println("Ranom number
generated from 1-100");
int count = 0;
while (true) {
System.out.println("Enter your guess");
// taking number
from user
int num =
sc.nextInt();
//validating if
the given number is in range
if (num < 0
|| num > 100) {
System.out.println("Number must be between
1-100");
continue;
}
//checking if
both are equal
if (num == rand)
{
System.out.println("Good guess");
break;
} else if (num
< rand) {
System.out.println("Low.. try again");
} else {
System.out.println("High.. try again");
}
}
sc.close();
}
}
Note : If you like my answer please rate and help me it is very Imp for me