String won't return - java

The method getResponseToCorrectAnswer and getResponseToIncorrectAnswer won't return any strings, but a print will work, so it's getting to the method. Does anyone know how to get these two methods to correctly return a String?
import java.util.Scanner;
public class CAI {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int answer;
for(int i = 1; i < 11; i++){
int num1 = generateRandomSingleDigitNumber();
int num2 = generateRandomSingleDigitNumber();
generateMultiplicationQuestion(num1, num2);
answer = input.nextInt();
validateMultiplicationAnswer(num1, num2, answer);
}//end for
}//end main
public static void generateMultiplicationQuestion(int num1, int num2){
System.out.println("What is the product of " + num1 + " x " + num2 + "?");
}//end generateMultiplicationQuestion
public static int generateRandomSingleDigitNumber(){
return (int)(Math.random()*10);
}//end generateRandomSingleDigitNumber
public static String getResponseToCorrectAnswer(){
//return ("Correct");
int num;
num = (int)(Math.random()*4);
if(num == 3){
return("Very good");
}else if(num == 2){
return("Excellent!");
}else if(num == 1){
return("Nice work!");
}else{
return("Keep up the good work!");
}//end if
}//end getResponseToCorrectAnswer
public static String getResponseToIncorrectAnswer(){
int num;
num = (int)(Math.random()*4);
if(num == 3){
return("No. Please try again.");
}else if(num == 2){
return("Wrong. Try once more");
}else if(num == 1){
return("Don't give up. Keep trying.");
}else{
return("Incorrect. Please answer again.");
}//end if
}//end getResponseToIncorrectAnswer
public static boolean validateMultiplicationAnswer(int num1, int num2, int answer) {
if(answer == num1 * num2){
getResponseToCorrectAnswer();
return true;
}else{
getResponseToIncorrectAnswer();
return false;
}//end if
}//end validateMultiplicationAnswer
}//end class

the method getResponseToCorrectAnswer and getResponseToIncorrectAnswer won't return any strings
Sure it will, but you're not assigning the string to anything so you don't see it.
Try this
String response = getResponseToCorrectAnswer();
System.out.println(response);
and you will see that response holds the return value.

Related

Print return value in Java

I'm currently making a high-low guessing game in Java. I believe I have two issues hindering the game from functioning properly.
Firstly, I have a while-statement inside the playGame method that doesn't change as it is now, and I have trouble understanding how I can make it change if the correct guess is made. As I understand it I can't change the value of boolean bool = true; from the giveResponse method, it has to be done from the same method playGame? If there a better way of doing this than using a while-statement?
Secondly, when the correct guess is made and the playGame method return the value of parameter guessCount to the main method it should then be printed out. The instruction I have for this game says it should be printed in the main method after the return have been made.
import java.io.Reader;
import java.util.Scanner;
public class HiLo2 {
public static void main(String[] args) {
System.out.println("Decide a level of difficulty\r"
+ "1. 1-10 \r"
+ "2. 1-100 \r"
+ "3. 1-1000");
Scanner reader = new Scanner(System.in);
int n = reader.nextInt();
if (n==1) {
playGame(10);
}
else if (n==2) {
playGame(100);
}
else if (n==3) {
playGame(1000);
}
else {
System.out.println("This level of difficulty doesn't exist. Please try one between 1 and 3.");
}
}
public static int playGame(int maxNumber) {
int answer = (int)(Math.random() * maxNumber) +1;
int guessCount = 0;
boolean bool = true;
Scanner reader = new Scanner(System.in);
System.out.println("Guess a number between 1 and " + maxNumber);
while (bool) {
int guess = reader.nextInt();
guessCount = guessCount + 1;
giveResponse(answer, guess);
}
return guessCount;
}
public static void giveResponse(int answer, int guess) {
if (guess == answer) {
System.out.println("Your guess is correct!\r");
}
else if (guess > answer) {
System.out.println("Your guess is too high, guess again:\r");
}
else if (guess < answer) {
System.out.println("Your guess is too low, guess again:\r");
}
}
}
I hope the questions are clear and specific enough. Any pointers in the right direction is appreciated.
You could have the giveResponse method return a boolean
static boolean giveResponse(...)
and put return false; inside the if (guess == answer){ case, and return true at the end of the method. Instead of calling giveResponse as a void method, use bool = giveResponse(answer,guess).
And for the second question, add a int numberOfGuesses in the main method before the if statement, and call playGame with numberOfGuesses = playGame(...), then System.out.println("You guessed " + numberOfGuesses + " times."); after the if-statements.
I think this is what you're trying to do.
public class HiLo2 {
public static void main(String[] args) {
System.out.println("Decide a level of difficulty\r"
+ "1. 1-10 \r"
+ "2. 1-100 \r"
+ "3. 1-1000");
Scanner reader = new Scanner(System.in);
int n = reader.nextInt();
if (n==1) {
System.out.println("Guess count: "+playGame(10));
}
else if (n==2) {
System.out.println("Guess count: "+playGame(100));
}
else if (n==3) {
System.out.println("Guess count: "+playGame(1000));
}
else {
System.out.println("This level of difficulty doesn't exist. Please try one between 1 and 3.");
}
}
public static int playGame(int maxNumber) {
int answer = (int)(Math.random() * maxNumber) +1;
int guessCount = 0;
boolean bool = false;
Scanner reader = new Scanner(System.in);
System.out.println("Guess a number between 1 and " + maxNumber);
while (! bool) {
int guess = reader.nextInt();
guessCount = guessCount + 1;
bool = giveResponse(answer, guess);
}
return guessCount;
}
public static boolean giveResponse(int answer, int guess) {
if (guess == answer) {
System.out.println("Your guess is correct!\r");
return true;
}
else if (guess > answer) {
System.out.println("Your guess is too high, guess again:\r");
return false;
}
else if (guess < answer) {
System.out.println("Your guess is too low, guess again:\r");
return false;
}
}
}
Here is your complete solution
import java.io.Reader;
import java.util.Scanner;
public class HereSOl {
public static void main(String[] args) {
System.out.println("Decide a level of difficulty\r" + "1. 1-10 \r" + "2. 1-100 \r" + "3. 1-1000");
Scanner reader = new Scanner(System.in);
int n = reader.nextInt();
if (n == 1) {
System.out.println("Guesstcount : " + playGame(10));
} else if (n == 2) {
System.out.println("Guesstcount : " + playGame(100));
} else if (n == 3) {
System.out.println("Guesstcount : " + playGame(1000));
} else {
System.out.println("This level of difficulty doesn't exist. Please try one between 1 and 3.");
}
}
public static int playGame(int maxNumber) {
int answer = (int) (Math.random() * maxNumber) + 1;
int guessCount = 0;
boolean bool = true;
Scanner reader = new Scanner(System.in);
System.out.println("Guess a number between 1 and " + maxNumber);
while (bool) {
int guess = reader.nextInt();
guessCount = guessCount + 1;
boolean ans =giveResponse(answer, guess);
if (ans == true) {
break;
}
}
return guessCount;
}
public static boolean giveResponse(int answer, int guess) {
if (guess == answer) {
System.out.println("Your guess is correct!\r");
return true;
} else if (guess > answer) {
System.out.println("Your guess is too high, guess again:\r");
return false;
} else if (guess < answer) {
System.out.println("Your guess is too low, guess again:\r");
return false;
}
return false;
}
}
I think you should use enum, Here is my Solution.
import java.util.Scanner;
public class HiLo2 {
public static void main(String[] args) {
System.out.println("Decide a level of difficulty\r" + "1. 1-10 \r" + "2. 1-100 \r" + "3. 1-1000");
Scanner reader = new Scanner(System.in);
int n = reader.nextInt();
if (n == 1) {
playGame(10);
} else if (n == 2) {
playGame(100);
} else if (n == 3) {
playGame(1000);
} else {
System.out.println("This level of difficulty doesn't exist. Please try one between 1 and 3.");
}
}
public static int playGame(int maxNumber) {
int answer = (int) (Math.random() * maxNumber) + 1;
int guessCount = 0;
boolean bool = true;
Scanner reader = new Scanner(System.in);
System.out.println("Guess a number between 1 and " + maxNumber);
while (bool) {
int guess = reader.nextInt();
guessCount = guessCount + 1;
Answer ans = giveResponse(answer, guess);
System.out.println(ans.getMessage());
if (ans == Answer.CORRECT) {
bool = false;
System.out.println("You took " + guessCount + " guess to get right number.");
}
}
return guessCount;
}
public static Answer giveResponse(int answer, int guess) {
if (guess == answer) {
return Answer.CORRECT;
} else if (guess > answer) {
return Answer.HIGH;
} else {
return Answer.LOW;
}
}
private enum Answer {
CORRECT("Your guess is correct!\r"), HIGH("Your guess is too high, guess again:\r"), LOW("Your guess is too low, guess again:\r");
private String message;
public String getMessage() {
return this.message;
}
Answer(String message) {
this.message = message;
}
}
}

In Java code, need to figure out how to get back to the main menu after doing one of the options in the menu?

I am working on Java code. I have a menu, the user selects an option, does something in the option then returns to the menu until the exit is selected. I'm not sure how to get it to go back to the menu.
Here is what I have:
/**
*
* #author Lisa Hergert
*/
import java.util.Scanner;
import java.util.Random;
public class MathTutor {
//Create scanner for user input
Scanner keyboard = new Scanner(System.in);
//Declare variables
int choice;
Random generator = new Random();
public MathTutor () {
choice = 0;
}
public int getQuestionType() {
while (choice < 1 || choice >3) {
System.out.println("Math Tutor");
System.out.println("\t1) Addition problem");
System.out.println("\t2) Subtraction problem");
System.out.println("\t3) Quit");
System.out.println("Enter your choice (1 - 3): ");
choice = keyboard.nextInt();
if (choice < 1 || choice > 3) {
System.out.println("You must choose a number from 1-3");
}
}
return choice;
}
public void askQuestions () {
for (int i = 0; i < 4; i++) {
int num1 = genRandomNum();
int num2 = genRandomNum();
int max = choice;
if (max == 3) {
max = (int)(Math.random() * 3 + 1);
}
switch (max) {
case 1: addition(num1, num2);
break;
case 2: subtraction(num1, num2);
break;
default: System.out.println("Error");
System.exit(1);
}
}
}
public int genRandomNum() {
return (int)generator.nextInt(1000);
}
public void addition(int num1, int num2) {
if (num1 > num2) {
System.out.printf("%5d\n", num1);
System.out.printf("+ %3d\n", num2);
System.out.println("-------");
} else {
System.out.printf("%5d\n", num2);
System.out.printf("+ %3d\n", num1);
System.out.println("-------");
}
int sum = num1 + num2;
int answer = keyboard.nextInt();
if (num1 + num2 == answer) {
System.out.println("Good job, you got it right!");
} else {
System.out.println("Good try, the correct answer is: " + sum);
}
}
public void subtraction (int num1, int num2) {
if (num1 > num2) {
System.out.printf("%5d\n", num1);
System.out.printf("- %3d\n", num2);
System.out.println("-------");
int diff = num1 - num2;
int answer = keyboard.nextInt();
if (num1 - num2 == answer) {
System.out.println("Good job, you got it right!");
} else {
System.out.println("Good try, the correct answer is: " + diff);
}
} else {
System.out.printf("%5d\n", num2);
System.out.printf("- %3d\n", num1);
System.out.println("-------");
int diff = num2 - num1;
int answer = keyboard.nextInt();
if (num2 - num1 == answer) {
System.out.println("Good job, you got it right!");
} else {
System.out.println("Good try, the correct answer is: " + diff);
}
}
}
public static void main(String[] args) {
MathTutor tutor = new MathTutor();
int choice = tutor.getQuestionType();
tutor.askQuestions();
return;
}
}
Maybe I'm missing something but can't you just loop all the functionality?
public static void main(String[] args) {
MathTutor tutor = new MathTutor();
int choice = tutor.getQuestionType();
while(choice != 3) {
tutor.askQuestions();
choice = tutor.getQuestionType();
}
return;
}
import java.util.Scanner;
import java.util.Random;
/**
*
* #author Lisa Hergert
*/
public class MathTutor {
//Create scanner for user input
Scanner keyboard = new Scanner(System.in);
//Declare variables
int choice;
Random generator = new Random();
public MathTutor () {
choice = 0;
}
public int getQuestionType() {
while (choice < 1 || choice >3) {
System.out.println("Math Tutor");
System.out.println("\t1) Addition problem");
System.out.println("\t2) Subtraction problem");
System.out.println("\t3) Quit");
System.out.println("Enter your choice (1 - 3): ");
choice = keyboard.nextInt();
if (choice < 1 || choice > 3) {
System.out.println("You must choose a number from 1-3");
}
}
return choice;
}
public void askQuestions () {
int num1 = genRandomNum();
int num2 = genRandomNum();
int max = choice;
if (max == 3) {
max = (int)(Math.random() * 3+3);
}
switch (max) {
case 1: addition(num1, num2);
break;
case 2: subtraction(num1, num2);
break;
default: System.out.println("Thank you for your time.");
System.exit(1);
}
}
public int genRandomNum() {
return (int)generator.nextInt(1000);
}
public void addition(int num1, int num2) {
if (num1 > num2) {
System.out.printf("%5d\n", num1);
System.out.printf("+ %3d\n", num2);
System.out.println("-------");
} else {
System.out.printf("%5d\n", num2);
System.out.printf("+ %3d\n", num1);
System.out.println("-------");
}
int sum = num1 + num2;
int answer = keyboard.nextInt();
if (num1 + num2 == answer) {
System.out.println("Good job, you got it right!");
} else {
System.out.println("Good try, the correct answer is: " + sum);
}
MathTutor tutor = new MathTutor();
int choice = tutor.getQuestionType();
tutor.askQuestions();
}
public void subtraction (int num1, int num2) {
if (num1 > num2) {
System.out.printf("%5d\n", num1);
System.out.printf("- %3d\n", num2);
System.out.println("-------");
int diff = num1 - num2;
int answer = keyboard.nextInt();
if (num1 - num2 == answer) {
System.out.println("Good job, you got it right!");
} else {
System.out.println("Good try, the correct answer is: " + diff);
}
} else {
System.out.printf("%5d\n", num2);
System.out.printf("- %3d\n", num1);
System.out.println("-------");
int diff = num2 - num1;
int answer = keyboard.nextInt();
if (num2 - num1 == answer) {
System.out.println("Good job, you got it right!");
} else {
System.out.println("Good try, the correct answer is: " + diff);
}
}
MathTutor tutor = new MathTutor();
int choice = tutor.getQuestionType();
tutor.askQuestions();
}
public static void main(String[] args) {
MathTutor tutor = new MathTutor();
int choice = tutor.getQuestionType();
tutor.askQuestions();
}
}

Trying to get isMultiple to execute properly

this executes, however it returns all answers as positive "8 is a multiple of 15" even if it is false. Not sure what I'm not seeing.
Here is what i have:
import java.util.*;
public class Multiples {
public static void main(String [] args){
boolean run = true;
while(run = true){
Scanner input = new Scanner(System.in);
System.out.print("Enter one number:");
int num1 = input.nextInt();
System.out.print("Enter a second number:");
int num2 = input.nextInt();
boolean result = isMultiple(num1,num2);
if(result = true){
System.out.println(num2 + " is a multiple of " + num1);
}
else{
System.out.println(num2 + " is not a multiple of " + num1);
}
System.out.println("Do you want to enter another pair(y/n)?");
String a = input.next();
if(YesOrNo(a)){
break;
}
}
}
public static boolean YesOrNo(String a){
if(a.equals("y"))
return false;
else if(a.equals("n"))
return true;
else
return true;
}
public static boolean isMultiple (int x , int y){
if(x % y == 0 || y % x == 0)
return true;
else
return false;
}
}
if(result = true){
replace with
if(result == true){
(or simply)
if(result){
You are assigning it instead of comparing.

Are my loops preventing me from getting the expected output?

My goal is to determine, for a pair of integers, whether the second integer is a multiple of the first. My code is as follows:
import java.util.Scanner;
public class Multiples {
public static void main(String [] args) {
boolean run = true;
while(run) {
Scanner input = new Scanner(System.in);
System.out.print("Enter one number:");
int num1 = input.nextInt();
System.out.print("Enter a second number:");
int num2 = input.nextInt();
boolean result = isMultiple(num1, num2);
if(result) {
System.out.println(num2 + " is a multiple of " + num1);
} else {
System.out.println(num2 + " is not a multiple of " + num1);
}
System.out.print("Do you want to enter another pair(y/n)?");
run = YesOrNo(input.next());
}
}
public static boolean YesOrNo(String a) {
if(a.equals("y"))
return true;
else
return false;
}
public static boolean isMultiple (int x , int y) {
if(y % x == 0)
return true;
else
return false;
}
}
This is my output:
This is the expected output:
The expected output is confusing me.

Finding the smallest, largest, and middle values in Java

I did with this code. Is it correct way? I want to sort the numbers in ascending order. Is there better way for this?
import java.lang.Math;
public class Numbers
{
public static void main(String[] args)
{
int a=1;
int b=2;
int c=3;
if (a<b && a<c)
System.out.println("Smallest: a");
else if (a>b && a>c)
System.out.println("Biggest: a");
else if (a>b && a<c)
System.out.println("Mid: a");
else if (a<b && a>c)
System.out.println("Mid: a");
if (b<c && b<a)
System.out.println("Smallest: b");
else if (b>c && b>a)
System.out.println("Biggest: b");
else if (b>c && b<a)
System.out.println("Mid: b");
else if (b<c && b>a)
System.out.println("Mid: b");
if (c<a && c<b)
System.out.println("Smallest: c");
else if (c>a && c>b)
System.out.println("Biggest: c");
else if (c>a && c<b)
System.out.println("Mid: c");
else if (c<a && c>b)
System.out.println("Mid: c");
}
}
Expanding on Steve's answer (I assume you are new to Java and need a more complete example):
import java.util.Arrays;
public class Numbers
{
public static void main(String[] args)
{
int a=3;
int b=2;
int c=1;
int[] numbers = {a,b,c};
Arrays.sort(numbers);
System.out.println("The highest number is "+numbers[2]);
System.out.println("The middle number is "+numbers[1]);
System.out.println("The lowest number is "+numbers[0]);
}
}
You can store the three numbers in an array and then do
Arrays.sort(numbers);
/* numbers[0] will contain your minimum
* numbers[1] will contain the middle value
* numbers[2] will contain your maximum
*/
That's all!
In general it would be best to use a loop and a array for this type of thing that way if you have more than 3 numbers it will still work. Also you wont have to type nearly as much. Try something like this for finding the smallest number.
MyArray = new int[3];
MyArray[0] = 1;
MyArray[1] = 2;
MyArray[2] = 3;
int temp = a;
for (int i = 0; i < (number of numbers to check in this case 3); i++){
if (MyArray[i] < temp){
temp = MyArray[i];
}
}
System.out.println("Smallest number is: " + temp);
import java.util.Scanner;
public class SortingIntegers {
public static void main (String[] args){
int num1;
int num2;
int num3;
int largerstNum;
int smallestNum;
int middleNum;
Scanner sc = new Scanner(System.in);
System.out.println("Please enter the First Integer");
num1 = sc.nextInt();
System.out.println("Pleas enter the Second Integer");
num2 = sc.nextInt();
System.out.println("Please enter the third Integer");
num3 = sc.nextInt();
if (num1 > num2){
if (num1 > num3){
largerstNum = num1;
if (num2 > num3){
middleNum = num2;
smallestNum = num3;
}else {
middleNum = num3;
smallestNum = num2;
}
}
}else {
if (num1 > num3){
middleNum = num1;
if (num2 > num3){
largerstNum = num2;
smallestNum = num3;
}else {
largerstNum = num3;
smallestNum = num2;
}
}else {
smallestNum =num1;
if (num2 > num3){
largerstNum = num2;
middleNum = num3;
}else {
largerstNum = num3;
middleNum = num2;
}
}
System.out.println("Highest Number is : " + largerstNum);
System.out.println("Smallest Number is : " + smallestNum);
System.out.println("Middle Number is : " + middleNum);
}
}
}

Categories