I'm a noob in Java and need some help with this. I wonder how to get a random name for each of the nameOne, nameTwo and NameThree strings without duplicating the whole switch statement. Can someone please give me an advice on how to do this without bloating up my code? My actual name list is very long.
public class multipleNamesPicker {public static void main(String[] args) {
String nameOne = null;
String nameTwo = null;
String nameThree = null;
char gender1 = 'a';
char gender2 = 'a';
char gender3 = 'a';
byte randomNumber1 = (byte)(Math.random()*2+1);
switch(randomNumber1) {
case 1: gender1 = 'w';
case 2: gender1 = 'm';
}
byte randomNumber2 = (byte)(Math.random()*5+1);
if(gender1 == 'w'){
switch(randomNumber2) {
case 1: nameOne = "Edna";
case 2: nameOne = "Martha";
case 3: nameOne = "Berta";
case 4: nameOne = "Margaret";
case 5: nameOne = "Anna";
}
}
else{
switch(randomNumber2) {
case 1: nameOne = "Peter";
case 2: nameOne = "Paul";
case 3: nameOne = "Pablo";
case 4: nameOne = "Henry";
case 5: nameOne = "George";
}
}
System.out.println(nameOne + ", " + nameTwo + " and " + nameThree);}
}
One easy way would be to put them in two Array's (One for female names and one for male names) and then have something like
if(gender1 == 'w'){
nameOne = femaleNames[randomNum];
}
Where femaleNames is your Array of female names and randomNum is your random number. Just make sure randomNum is within bounds of your Array
Related
Hopefully you can see what I am going for here. Obviously this code doesn't work but I'm basically trying to say if the random number is one of these values, run this code. If the random number is another value run that code. I need something that is equivalent to a big or statement without using a large if-else. Thanks
static int cardNumber = rnd.nextInt(13) + 1;
if (cardNumber == 1||11||12||13)
{
System.out.println(faceCard + " of " + suit);
}
else
{
System.out.println(cardNumber + " of " + suit);
}
You could benefit from a switch / case :
switch (caseNumber) {
case 1 : case 11 : case 12 : case 13 :
<specific case code>;
break;
default :
<general case code>;
break;
}
Well, it's not a large if/else, it's just two conditions:
static int cardNumber = rnd.nextInt(13) + 1;
if (cardNumber == 1 || cardNumber >= 11)
{
System.out.println(faceCard + " of " + suit);
}
else
{
System.out.println(cardNumber + " of " + suit);
}
Use a switch(int) to clarify
static int cardNumber = rnd.nextInt(13) + 1;
switch (cardNumber)
case 1:
case 11:
case 12:
case 13:
System.out.println(faceCard + " of " + suit);
break;
default:
System.out.println(cardNumber + " of " + suit);
break;
}
You could just simplify it I suppose, in your case specifically this might work..
int cn = rnd.nextInt(13) + 1;
if(cn <= 10 && cn > 1) {
System.out.println("Non-Face");
} else {
System.out.println("Face");
}
You could use a switch case with a fallthrough.
public static void main(String args[]) {
int cardNumber = 10;
switch (cardNumber) {
case 1:
case 11:
case 12:
case 13:
// your if case
break;
case 100:
// some other case (example)
break;
default:
// else case
break;
}
}
You could also write a method which tells you if a card has a face!
This would be a lot easier to use and would prevent redundant code.
Also you could the class card, instead of an integer to save your card value.
An Enum wouldn't be bad either.
class Card {
public static boolean hasFace(int cardNum) {
if (cardNum > 13)
throw new InvalidParameterException("There is no card with a value greater 31!");
boolean rval = false;
switch (cardNum) {
case 1:
case 11:
case 12:
case 13:
rval = true;
break;
default:
break;
}
return rval;
}
}
Main for case 2:
public static void main(String args[]) {
int cardNumber = 10;
if(Card.hasFace(cardNumber)){
// your if case
}
else {
// your else case
}
}
public static void main(String[] args) {
Scanner ms = new Scanner(System.in);
String binary = ms.nextLine();
binary=binary.trim();
//add leading zeroes if length divided by 4 has remainder.
while (binary.length() % 4 != 0) binary = "0" + binary;
String number = "";
for (int i = 0; i < binary.length(); i += 4) {
String num = binary.substring(i, i + 3);
switch(num)
{
case "0000" : number = "0"; break;
case "0001" : number = "1"; break;
case "0010" : number = "2"; break;
case "0011" : number = "3"; break;
case "0100" : number = "4"; break;
case "0101" : number = "5"; break;
case "0110" : number = "6"; break;
case "0111" : number = "7"; break;
case "1000" : number = "8"; break;
case "1001" : number = "9"; break;
case "1010" : number = "A"; break;
case "1011" : number = "B"; break;
case "1100" : number = "C"; break;
case "1101" : number = "D"; break;
case "1110" : number = "E"; break;
case "1111" : number = "F"; break;
}
System.out.println(number);
}
}
I need to use loop and a switch op to do the conversion. After making those changes. I get my result of binary 1111 1110 as F then E on the next line. How can I fix that? I don't want to use stringbuilder because I haven't learn that. Is there any other simple code to do that?
Your return statement is inside your for loop, so after the first iteration you will return from the function. Also, you are overwriting number at every itteration. You should instead replace number with a StringBuilder and user append().
public static void main(String[] args) {
Scanner ms = new Scanner(System.in);
String binary = ms.nextLine();
binary.trim();
//add leading zeroes if length divided by 4 has remainder.
while (binary.length() % 4 != 0) binary = "0" + binary;
StringBuilder number = new StringBuilder();
for (int i = 0; i < binary.length(); i += 4) {
String num = binary.substring(i, i + 4);
switch(num)
{
case "0000" : number.append("0"); break;
case "0001" : number.append("1"); break;
case "0010" : number.append("2"); break;
case "0011" : number.append("3"); break;
case "0100" : number.append("4"); break;
case "0101" : number.append("5"); break;
case "0110" : number.append("6"); break;
case "0111" : number.append("7"); break;
case "1000" : number.append("8"); break;
case "1001" : number.append("9"); break;
case "1010" : number.append("A"); break;
case "1011" : number.append("B"); break;
case "1100" : number.append("C"); break;
case "1101" : number.append("D"); break;
case "1110" : number.append("E"); break;
case "1111" : number.append("F"); break;
}
System.out.println(number.toString());
}
return;
}
Also, others have also mentinoed, your binary.trim() does not work as expected, it needs to be binary = binary.trim().
Strings are immutable
binary = binary.trim(); //not just binary.trim();
Also, you'd want to get the string from index 0 to 3, not 0 to 4. So it's (i, i+3)
So in here it should be:
for (int i = 0; i < binary.length(); i += 4) {
String num = binary.substring(i, i + 3);
Also, take out the return statement at the bottom, because it exits the method when you do one iteration
Its because you're returning from the first iteration of the loop.
Anyways, here's the piece of code that does just what you want , convert binary string to hexadecimal
static String binToHex(String binStr){
while(binStr.length() % 4 != 0){
binStr = "0" + binStr;
}
String hexString = "";
binStr = new StringBuilder(binStr).reverse().toString();
for(int index = 0, len = binStr.length(); index < len;){
int num = 0;
for(int indexInQuad = 0; indexInQuad < 4; indexInQuad++, index++){
int bit=Integer.parseInt(String.valueOf(binStr.charAt(index)));
num += (bit * Math.pow(2,indexInQuad));
}
hexString += Integer.toHexString(num).toUpperCase();
}
hexString = new StringBuilder(hexString).reverse().toString();
return hexString;
}
it also saves you the switch statements
just pass it the binary string value and it works seamlessly :D
The immediate problem with your code is that you return right after printing the first number! Remove the return, and it will print the other numbers, too.
Also, as noted by Josh, you have to do binary = binary.trim();, as trim() will not alter the string in-place but return a trimmed version of the string.
And finally, note that you could replace most of your code with just this...
int n = Integer.parseInt(binary, 2);
String s = Integer.toString(n, 16);
System.out.println(s.toUpperCase());
I am taking input from user in string and I want to iterate and test using case statement but it is not working. its not printing the statements.
import java.io.*;
import java.util.*;
public class fh3
{
public static void main(String args[])throws IOException
{
String sentence = "";
System.out.println("Enter the word : ");
Scanner scan = new Scanner(System.in);
String word = scan.next();
char[] chars = word.toCharArray();
for(int i = 0; i < word.length(); i++)
{
System.out.println("---" + chars[i]);
switch(chars[i])
{
case 0: sentence = " ";
System.out.println("B");
break;
case 1: sentence = "A";
break;
case 2: sentence = "B";
System.out.println("B");
break;
case 3: sentence = "C";
break;
}
sentence+=sentence;
System.out.println(sentence);
}
}
}
if i enter 20 den it should print"B "
but its printing as
Enter the word :
20
---2
---0
where i am getting wrong?
Since you're doing the switch on char type, your case should have the same. In your case, since you give the case as integer values, its just not matching. '0' is not equal to 0
switch(chars[i]) {
case '0': // switch on char '0' and not integer 0.
case '1': // switch on char '1' and not integer 1.
case '2': // switch on char '2' and not integer 2.
...
}
import java.io.IOException;
import java.util.Scanner;
public class Fh3 {
public static void main(String args[]) throws IOException {
String sentence = "";
System.out.println("Enter the word : ");
Scanner scan = new Scanner(System.in);
String word = scan.next();
//Switch case needs you to compare the expression with constants hence the final keyword.
final char CHARONE = '1';
final char CHARTWO = '2';
final char CHARTHREE = '3';
final char CHARFOUR = '4';
char[] chars = word.toCharArray();
for (int i = 0; i < word.length(); i++) {
System.out.println("---" + chars[i]);
switch (chars[i]) {
case 0:
sentence = " ";
System.out.println("B");
break;
case CHARONE:
sentence = "A";
break;
case CHARTWO:
sentence = "B";
System.out.println("B");
break;
case CHARTHREE:
sentence = "C";
break;
}
sentence += sentence;
System.out.println(sentence);
}
}
}
You were trying to compare int with char .. Clear ?
Because you're switching on characters, not integers :
switch(chars[i]){
case '0': sentence = " ";
System.out.println("B");
break;
case '1': sentence = "A";
break;
case '2': sentence = "B";
System.out.println("B");
break;
case '3': sentence = "C";
break;
}
your switch is accepting char but no suitable case is there.So its printing only this statement System.out.println("---" + chars[i]); two times(because word.length() returns 2 in your case)
import java.io.*;
import java.util.*;
public class fh3
{
public static void main(String args[])throws IOException
{
String sentence = "";
System.out.println("Enter the word : ");
Scanner scan = new Scanner(System.in);
String word = scan.next();
char[] chars = word.toCharArray();
for(int i = 0; i < word.length(); i++)
{
System.out.println("---" + chars[i]);
switch(chars[i])
{
case '0': sentence = " ";
System.out.println("B");
break;
case '1': sentence = "A";
break;
case '2': sentence = "B";
System.out.println("B");
break;
case '3': sentence = "C";
break;
}
sentence+=sentence;
System.out.println(sentence);
}
}
}
In Java, the char type maps to the int type via the Ascii table.
Therefore, if you want to check the char '0' and not the NUL char, you should do:
switch(chars[i]) {
case '0': // do the work
case '1': // do the work
// ...
}
I am working on a guessing game assignment. The computer randomizes 3 letters from R/B/G/Y and the user tries to guess the correct order and position of the combination. A counter is incremented to give hints, ie. count how many letters and/or positions the user gets right with each guess until they narrow it down.
This is my method that randomizes the computer's combo:
public static void newGame(){
// Randomize colour choice (# between 1 and 4)
col1 = (int)(Math.random()*4)+1;
col2 = (int)(Math.random()*4)+1;
col3 = (int)(Math.random()*4)+1;
switch ((int)col1){
case 1: ans1 = ("R");
break;
case 2: ans1 = ("G");
break;
case 3: ans1 = ("B");
break;
case 4: ans1 = ("Y");
break;
}
switch ((int)col2){
case 1: ans2 = ("R");
break;
case 2: ans2 = ("G");
break;
case 3: ans2 = ("B");
break;
case 4: ans2 = ("Y");
break;
}
switch ((int)col3){
case 1: ans3 = ("R");
break;
case 2: ans3 = ("G");
break;
case 3: ans3 = ("B");
break;
case 4: ans3 = ("Y");
break;
}
colchoice = ans1 + " " + ans2 + " " + ans3;
}//end newGame method
These are my counter methods:
public static void checkColoursCorrect(){
inputnumber(guess1,guess2,guess3);
// Determine how many user inputted letters equal the randomized ones
correctc = 0;
if (uguess1 == col1 || uguess1 == col2 || uguess1 == col3){
correctc++;
}
if (uguess2 == col1 || uguess2 == col2 || uguess2 == col3){
correctc++;
}
if (uguess3 == col1 || uguess3 == col2 || uguess3 == col3){
correctc++;
}
}//end checkColoursCorrect method
public static void checkPositionsCorrect(){
inputnumber(guess1,guess2,guess3);
correctp = 0;
if (uguess1 == col1){
correctp++;
}
if (uguess2 == col2){
correctp++;
}
if (uguess3 == col3){
correctp++;
}
if (uguess1 == col1 && uguess2 == col2 && uguess3 == col3){
System.out.println("User wins! Colour was " + colchoice);
}
}//end checkPositionsCorrect method
I think that, since the combo is randomized, each time the counter methods call col1/col2/col3, a NEW combo is used rather than the one initialized in the first place? If anyone could point me in the direction on how to fix this it would be appreciated.
Input number method:
public static void inputnumber(String guess1, String guess2, String guess3){
// Convert user's FIRST guess to a number
if ("R".equals(guess1)){
uguess1 = 1;
}
if ("G".equals(guess1)){
uguess1 = 2;
}
if ("B".equals(guess1)){
uguess1 = 3;
}
if ("Y".equals(guess1)){
uguess1 = 4;
}
// Convert user's SECOND guess to a number
if ("R".equals(guess2)){
uguess2 = 1;
}
if ("G".equals(guess2)){
uguess2 = 2;
}
if ("B".equals(guess2)){
uguess2 = 3;
}
if ("Y".equals(guess2)){
uguess2 = 4;
}
// Convert user's FINAL guess to a number
if ("R".equals(guess3)){
uguess3 = 1;
}
if ("G".equals(guess3)){
uguess3 = 2;
}
if ("B".equals(guess3)){
uguess3 = 3;
}
if ("Y".equals(guess3)){
uguess3 = 4;
}
}
As far as I understand, in first lines of your code you want to get random values for col1, col2 and col3.
I think Java may probably have same attitude as C, so you need to create a random number generator in your code in order to get new random numbers every time you run your program. Otherwise you will only get random numbers once, at time of compilation.
At this link you can see Oracle's documentation for class Random. You probably need to create a generator (using new Random()) and then newInt() to get random int values later in the code.
When you notice concurrent modification, making sure you do not access same fields from different methods is number one to be checked.
Having trouble with this the whole day. Please help me. I can't get the problem to display
The output shows
PROBLEM NUMBER 1
Answer:0
Correct....
PROBLEM NUMBER 2
Answer:1
Wrong....
It must show:
PROBLEM NUMBER 1
10 + 11 = ?
Answer: 21
Correct...*/
import java.util.Random;
import java.util.*;
import java.util.Scanner;
import javax.swing.JOptionPane;
import java.lang.Math;
public class MathIsSuperFun1{
Scanner input = new Scanner(System.in);
int correctAnswers;
int randomNum1;
int randomNum2;
int choice;
int corrrectAnswers, wrongAnswers;
String playerName ="";
int operation;
int userAnswer;
int correctAnswer = 0;
int userRemainder, correctRemainder;
int x = 0;
int temporaryNum1, temporaryNum2;
int range;
int randomNumber;
public static void main (String args[]){
MathIsSuperFun1 lab = new MathIsSuperFun1();
lab.init();
}
public void init(){
getName();
pickDifficulty();
pickOperation();
for(int x = 0; x < 10; x++)
{
System.out.println("\t\t\t~~~~~~~PROBLEM NUMBER" + (x + 1) + "~~~~~~~~");
assignNum();
getProblem();
checkAnswer();
}
}
//GET PLAYER NAME USING PANE
public static String getName(){
String playerName;
playerName = JOptionPane.showInputDialog(null, "Welcome!\nEnter your name and press OK.", "Math Is Super Fun!", JOptionPane.QUESTION_MESSAGE);
System.out.println("Do your best," + playerName + "!");
return playerName;
}
//GET PROBLEM BASED ON OPERATION
public void getProblem(){
switch(operation){
case 1:
System.out.println(randomNum1 + "+" + randomNum2 + "= ?\n");
correctAnswer = randomNum1 + randomNum2;
break;
case 2:
System.out.println(randomNum1 + "-" + randomNum2 + "= ?\n");
correctAnswer = randomNum1-randomNum2;
break;
case 3:
System.out.println(randomNum1 + "*" + randomNum2 + "= ?\n");
correctAnswer = randomNum1*randomNum2;
break;
case 4:
System.out.println(randomNum1 + "/" + randomNum2 + "= ?\n");
correctAnswer = randomNum1/randomNum2;
correctRemainder = randomNum1%randomNum2;
break;
}
System.out.print("Answer: ");
userAnswer = input.nextInt();
if(operation == 4){
System.out.print("Remainder: ");
userRemainder = input.nextInt();
}
return 0;
}
//PICK DIFFICULTY USING DIALOG
public void pickDifficulty(){
int choice = 0;
System.out.println("1 - Child's Play\n2 - No Sweat\n3 - Bitter\n4 - Cold-blooded\n5 - Brutal\n6 - Genius");
choice = input.nextInt();
}
//PICK OPERATIONS
public void pickOperation(){
int operation = 0;
System.out.println("1 - Addition\n2 - Subtraction\n3 - Multiplication\n4 - Division ");
operation = input.nextInt();
}
//GET NUMBER RANGE BASED ON DIFFICULTY
public int numberRange(){
int range = 0;
switch(choice){
case 1:
range = 100;
break;
case 2:
range = 1000;
break;
case 3:
range = 10000;
break;
case 4:
range = 100000;
break;
case 5:
range = 1000000;
break;
case 6:
range = 10000000;
break;
}
return range;
}
//GET CORRECT RANDOM RESPONSE USING CASE SWITCH BASED ON GETRANDOM METHOD
public void correctResponse(){
String responseCorrect = "";
switch (getRandom(5)){
case 1:
responseCorrect = "Correct. Keep up the good work!";
break;
case 2:
responseCorrect = "Correct. Keep aiming higher!";
break;
case 3:
responseCorrect = "Correct. Well done!";
break;
case 4:
responseCorrect = "Correct. Nice work!";
break;
case 5:
responseCorrect = "Correct. We're almost there!";
break;
}
System.out.println(responseCorrect);
correctAnswers += 1;
}
//GET WRONG RANDOM RESPONSE USING CASE SWITCH BASED ON GETRANDOM METHOD
public String wrongResponse(){
String responseWrong = "";
switch (getRandom(5)){
case 1:
responseWrong = "Wrong. Don't give up!";
break;
case 2:
responseWrong = "Wrong. You can do it!";
break;
case 3:
responseWrong = "Wrong. Try again puny human!";
break;
case 4:
responseWrong = "Wrong. You must be really weak at math!";
break;
case 5:
responseWrong = "Wrong. I pity you!";
break;
}
System.out.println(responseWrong);
System.out.println("The correct answer is:" + correctAnswer);
if(operation == 4)
System.out.println("Correct Remainder: " + correctRemainder);
return responseWrong;
}
public void checkAnswer(){
if(operation != 4 && userAnswer == correctAnswer){
correctResponse();
}
else if(operation == 4 && userAnswer == correctAnswer && userRemainder == correctRemainder){
correctResponse();
}
else{
wrongResponse();
}
}
public void assignNum(){
int temporaryNum1 = getRandom(numberRange());
int temporaryNum2 = getRandom(numberRange());
while(operation == 4 && temporaryNum1 == 0){
temporaryNum1 = getRandom(numberRange());
}
while(operation == 4 && temporaryNum2 == 0){
temporaryNum2 = getRandom(numberRange());
}
if(temporaryNum1 > temporaryNum2)
{
randomNum1 = temporaryNum1;
randomNum2 = temporaryNum2;
}
else
{
randomNum1 = temporaryNum2;
randomNum2 = temporaryNum1;
}
}
public int getRandom(int range){
randomNumber = (int)Math.floor((Math.random()*range)+1);
return randomNumber;
}
}
The reason your questions are not outputting is simple.
public void pickOperation(){
int operation = 0;
System.out.println("1 - Addition\n2 - Subtraction\n3 - Multiplication\n4 - Division ");
operation = input.nextInt();
}
You are creating a local variable, operation, and assigning the input value to it. This means that the field, operation, is never set, so when it comes to the switch statement in your getProblem method, it doesn't output anything because it doesn't match the switch statement.
To fix this, simply remove the int operation = 0; declaration.
Edit
Just noticed the same problem with your pickDifficulty method. I would strongly recommend you have a look at this tutorial on scope in Java.
Explanation of your Problem
Okay. So let's look at your code:
public void pickOperation(){
int operation = 0;
// Declare an int, called 'operation'.
System.out.println("1 - Addition\n2 - Subtraction\n3 - Multiplication\n4 - Division ");
// Set the newly declared value to an int from the keyboard.
operation = input.nextInt();
}
As soon as this method is finished, the value inside operation is destroyed. The reason why it isn't staying in your field int operation, is because you declared a more local operation variable. By removing the int operation = 0; at the start of this method, you force the JVM to look for the next available variable named operation in your class, which is in the field. That's why, when you remove the first assertion statement, your value for operation will stick around.
the problem is : int operation = 0; you need to take the value of the operation in operation variable which you create it in Main
Try this :
import java.util.Random;
import java.util.*;
import java.util.Scanner;
import javax.swing.JOptionPane;
import java.lang.Math;
public class Main {
Scanner input = new Scanner(System.in);
int correctAnswers;
int randomNum1;
int randomNum2;
int choice;
int corrrectAnswers, wrongAnswers;
String playerName = "";
int operation;
int userAnswer;
int correctAnswer = 0;
int userRemainder, correctRemainder;
int x = 0;
int temporaryNum1, temporaryNum2;
int range;
int randomNumber;
public static void main(String args[]) {
Main lab = new Main();
lab.init();
}
public void init() {
getName();
pickDifficulty();
pickOperation();
for (int x = 0; x < 10; x++) {
System.out.println("\t\t\t~~~~~~~PROBLEM NUMBER" + (x + 1) + "~~~~~~~~");
assignNum();
getProblem();
getAnswers();
checkAnswer();
}
}
//GET PLAYER NAME USING PANE
public static String getName() {
String playerName;
playerName = JOptionPane.showInputDialog(null, "Welcome!\nEnter your name and press OK.", "Math Is Super Fun!", JOptionPane.QUESTION_MESSAGE);
System.out.println("Do your best," + playerName + "!");
return playerName;
}
//GET PROBLEM BASED ON OPERATION
public void getProblem() {
switch (operation) {
case 1:
System.out.println(randomNum1 + "+" + randomNum2 + "= ?\n");
correctAnswer = randomNum1 + randomNum2;
break;
case 2:
System.out.println(randomNum1 + "-" + randomNum2 + "= ?\n");
correctAnswer = randomNum1 - randomNum2;
break;
case 3:
System.out.println(randomNum1 + "*" + randomNum2 + "= ?\n");
correctAnswer = randomNum1 * randomNum2;
break;
case 4:
System.out.println(randomNum1 + "/" + randomNum2 + "= ?\n");
correctAnswer = randomNum1 / randomNum2;
correctRemainder = randomNum1 % randomNum2;
break;
}
System.out.print("Answer: ");
userAnswer = input.nextInt();
if (operation == 4) {
System.out.print("Remainder: ");
userRemainder = input.nextInt();
}
// return 0;
}
//PICK DIFFICULTY USING DIALOG
public void pickDifficulty() {
System.out.println("1 - Child's Play\n2 - No Sweat\n3 - Bitter\n4 - Cold-blooded\n5 - Brutal\n6 - Genius");
choice = input.nextInt();
}
//PICK OPERATIONS
public void pickOperation() {
System.out.println("1 - Addition\n2 - Subtraction\n3 - Multiplication\n4 - Division ");
operation = input.nextInt();
}
//GET NUMBER RANGE BASED ON DIFFICULTY
public int numberRange() {
int range = 0;
switch (choice) {
case 1:
range = 100;
break;
case 2:
range = 1000;
break;
case 3:
range = 10000;
break;
case 4:
range = 100000;
break;
case 5:
range = 1000000;
break;
case 6:
range = 10000000;
break;
}
return range;
}
//GET CORRECT RANDOM RESPONSE USING CASE SWITCH BASED ON GETRANDOM METHOD
public void correctResponse() {
String responseCorrect = "";
switch (getRandom(5)) {
case 1:
responseCorrect = "Correct. Keep up the good work!";
break;
case 2:
responseCorrect = "Correct. Keep aiming higher!";
break;
case 3:
responseCorrect = "Correct. Well done!";
break;
case 4:
responseCorrect = "Correct. Nice work!";
break;
case 5:
responseCorrect = "Correct. We're almost there!";
break;
}
System.out.println(responseCorrect);
correctAnswers += 1;
}
//GET WRONG RANDOM RESPONSE USING CASE SWITCH BASED ON GETRANDOM METHOD
public String wrongResponse() {
String responseWrong = "";
switch (getRandom(5)) {
case 1:
responseWrong = "Wrong. Don't give up!";
break;
case 2:
responseWrong = "Wrong. You can do it!";
break;
case 3:
responseWrong = "Wrong. Try again puny human!";
break;
case 4:
responseWrong = "Wrong. You must be really weak at math!";
break;
case 5:
responseWrong = "Wrong. I pity you!";
break;
}
System.out.println(responseWrong);
System.out.println("The correct answer is:" + correctAnswer);
if (operation == 4) {
System.out.println("Correct Remainder: " + correctRemainder);
}
return responseWrong;
}
public void checkAnswer() {
if (operation != 4 && userAnswer == correctAnswer) {
correctResponse();
} else if (operation == 4 && userAnswer == correctAnswer && userRemainder == correctRemainder) {
correctResponse();
} else {
wrongResponse();
}
}
public void assignNum() {
int temporaryNum1 = getRandom(numberRange());
int temporaryNum2 = getRandom(numberRange());
while (operation == 4 && temporaryNum1 == 0) {
temporaryNum1 = getRandom(numberRange());
}
while (operation == 4 && temporaryNum2 == 0) {
temporaryNum2 = getRandom(numberRange());
}
if (temporaryNum1 > temporaryNum2) {
randomNum1 = temporaryNum1;
randomNum2 = temporaryNum2;
} else {
randomNum1 = temporaryNum2;
randomNum2 = temporaryNum1;
}
}
public int getRandom(int range) {
randomNumber = (int) Math.floor((Math.random() * range) + 1);
return randomNumber;
}
private void getAnswers() {
////////////////Not yet implemented";
}
}