In: Computer Science
Write a program in java which randomly generates two integer number n1 and n2 (suppose the range for each integer is [1, 100]), then asks the user what is the value of n1*n2, if the user’s answer is correct, call method printGoodComment to print out something nice, otherwise, call printBadComment to print out something “mean”.
The method signatures are:
public static void printGoodComment()
and
public static void printBadComment()
in your printGoodComment method, it will randomly print one sentence from the following lists:
good job
excellent
terrific
nice work
in your printBadComment method, it will randomly print one sentence from the following lists:
sorry, try next time
oops, you need more work
hmm, it is not correct
Hint: use Math.random and a switch statement in the printGoodComment and printBadComment methods.
/***************************************Assignment6.java************************/
import java.util.Random;
import java.util.Scanner;
public class Assignment6 {
public static void main(String[] args) {
Scanner scan = new
Scanner(System.in);
Random r = new Random();
char option = ' ';
do {
int n1 =
r.nextInt(100) + 1;
int n2 =
r.nextInt(100) + 1;
System.out.print("What is the value of " + n1 + "*" + n2 + ":
");
int result =
scan.nextInt();
scan.nextLine();
if (result == n1
* n2) {
printGoodComment();
} else {
printBadComment();
}
System.out.print("Do you want to continue(y/n): ");
option =
scan.nextLine().toLowerCase().charAt(0);
} while (option != 'n');
scan.close();
}
public static void printGoodComment() {
Random r = new Random();
int num = r.nextInt(3);
switch (num) {
case 0:
System.out.println("Good job!");
break;
case 1:
System.out.println("Excellent!");
break;
case 2:
System.out.println("Terrific!");
break;
case 3:
System.out.println("Nice work!");
break;
}
}
public static void printBadComment() {
Random r = new Random();
int num = r.nextInt(2);
switch (num) {
case 0:
System.out.println("Sorry, try next time!");
break;
case 1:
System.out.println("OOPs, you need more work!");
break;
case 2:
System.out.println("Hmm, it is not correct!");
break;
}
}
}
/***************************output************************/
What is the value of 69*63: 2345
Sorry, try next time!
Do you want to continue(y/n): y
What is the value of 20*9: 180
Good job!
Do you want to continue(y/n): y
What is the value of 30*8: 240
Terrific!
Do you want to continue(y/n): y
What is the value of 98*60: 987
OOPs, you need more work!
Do you want to continue(y/n): n
Please let me know if you have any doubt or modify the answer, Thanks :)