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.
Related
I have a game that's running perfectly. I want to put a line of code that asks the player if they want to play again at the end of the game. I would also like to keep a score system for every player and computer win.
I'm having trouble with the input = Integer.parseInt(sc.nextInt()); line
import java.util.Scanner;
public class Sticks {
public static boolean whoStart(String choice) {
int ran = (int) (Math.random() * 2 + 1);
String ht = "";
switch (ran) {
case 1:
ht = "head";
break;
case 2:
ht = "tails";
}
if (ht.equals(choice.toLowerCase())) {
System.out.println("you start first");
return true;
} else {
System.out.println("computer starts first");
return false;
}
}
public static int playerTurn(int numberOfSticks) {
System.out.println(" \nthere are " + numberOfSticks + " sticks ");
System.out.println("\nhow many sticks do you wanna take? 1 or 2?");
Scanner in = new Scanner(System.in);
int sticksToTake = in.nextInt();
while ((sticksToTake != 1) && (sticksToTake != 2)) {
System.out.println("\nyou can only take 1 or 2 sticks");
System.out.println("\nhow many sticks do you wanna take?");
sticksToTake = in.nextInt();
}
numberOfSticks -= sticksToTake;
return numberOfSticks;
}
public static int computerTurn(int numberOfSticks) {
int sticksToTake;
System.out.println("\nthere are " + numberOfSticks + " sticks ");
if ((numberOfSticks - 2) % 3 == 0 || (numberOfSticks - 2 == 0)) {
sticksToTake = 1;
numberOfSticks -= sticksToTake;
} else {
sticksToTake = 2;
numberOfSticks -= sticksToTake;
}
System.out.println("\ncomputer took " + sticksToTake + " stick ");
return numberOfSticks;
}
public static boolean checkWinner(int turn, int numberOfSticks) {
int score = 0;
int input;
int B = 1;
int Y=5, N=10;
if ((turn == 1) && (numberOfSticks <= 0)) {
System.out.println("player lost");
return true;
}
if ((turn == 2) && (numberOfSticks <= 0)) {
System.out.println("player won");
score++;
return true;
}
System.out.println("Your score is "+ score);
System.out.println("Do you want to play again? Press (5) for Yes / (10) for No");
// ----- This line -----
input = Integer.parseInt(sc.nextInt());
if (input == Y) {
B = 1;
System.out.println("Rock, Paper, Scissors");
} else if (input == N) {
System.exit(0);
System.out.println("Have A Good Day!");
}
}
public static void main(String args[]) {
int turn;
int numberOfSticks = 21;
Scanner in = new Scanner(System.in);
System.out.println("choose head or tails to see who starts first");
String choice = in.next();
if (whoStart(choice) == true) {
do {
turn = 1;
numberOfSticks = playerTurn(numberOfSticks);
if (checkWinner(turn, numberOfSticks) == true) {
break;
};
turn = 2;
numberOfSticks = computerTurn(numberOfSticks);
checkWinner(turn, numberOfSticks);
} while (numberOfSticks > 0);
} else {
do {
turn = 2;
numberOfSticks = computerTurn(numberOfSticks);
if (checkWinner(turn, numberOfSticks) == true) {
break;
};
turn = 1;
numberOfSticks = playerTurn(numberOfSticks);
checkWinner(turn, numberOfSticks);
} while (numberOfSticks > 0);
}
}
}
The title of your question almost answered you what you need to add: a loop!
I suggest you to refactor your function main and extract all your game logic from it to be stored within a dedicated function for the sake of the readability. Let's call it startGame().
Your main is going to become shorter and can represent a good location to introduce this loop, such as:
public static void main(String[] a) {
boolean isPlaying = true;
Scanner in = new Scanner(System.in);
while(isPlaying) {
startGame();
// Your message to continue or stop the game
if(in.next().equalsIgnoreCase("No")) {
isPlaying = false;
}
}
}
I recommend you to use a boolean that is checked in your while loop rather than using a break statement, as it brings a better control flow in your application.
Just put everything in a while(true) loop and use a break; if they choose no. Something like:
static int playerPoints = 0;
public static void main(String args[]) {
int turn;
int numberOfSticks = 21;
Scanner in = new Scanner(System.in);
while(true){
...
System.out.println("You have " + playerPoints + " points!")
System.out.println("Do you want to play again?");
if (!in.nextLine().toLowerCase().equals("yes")){
break;
}
}
}
Edit: ZenLulz's answer is better than this one, mainly because it encourages better programming practice. Mine works but isn't the best way to solve the issue.
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.
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.
Problem "Write a method isMultiple that determines, for a pair of integers , whether the second integer is a multiple of the first. The method should take two integer arguments and return true if the second is a multiple of the first and false otherwise. [Hint: Use the remainder operator .] Incorporate this method into an application that inputs a series of pairs of integers (one pair at a time) and determines whether the second value in each pair is a multiple of the first.har()"
Keep getting "The value of your output is incorrect." Have tried doing multiple things to fix but not sure what's wrong. When I click for feedback I get
Expected Output:
·Enter·one·number:Enter·a·second·number:9·is·a·multiple·of·3↵
Do·you·want·to·enter·another·pair(y/n)?Enter·one·number:Enter·a·second·number:99·is·a·multiple·of·11↵
Do·you·want·to·enter·another·pair(y/n)?Enter·one·number:Enter·a·second·number:7·is·a·multiple·of·7↵
Do·you·want·to·enter·another·pair(y/n)?Enter·one·number:Enter·a·second·number:3·is·not·a·multiple·of·9↵
Do·you·want·to·enter·another·pair(y/n)?↵
Actual Output:
·Enter·one·number:Enter·a·second·number:9·is·a·multiple·of·3↵
Do·you·want·to·enter·another·pair(y/n)?↵
Enter·one·number:
Any help will be GREATLY appreciated ^_^
Code I have so far:
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;
}
}
When you are checking you have to do it like this:
if(result == true){
First of all I am not asking anyone to do anything just need a little help to fix this bug with boolean. I put false but the program stops. I got two parts to the program.
First part where i did the calculations:
class FibonacciNumbers {
FibonacciNumbers() {} //default constructor
public int fOf(int n) {
if (n == 0) //the base case
{
return 0;
} else if (n == 1) {
return 1;
} else {
return fOf(n - 1) + fOf(n - 2);
}
}
}
Second where the main method is:
import java.util.*;
public class FibonacciNumbersTesters {
public static void main(String[] args) {
FibonacciNumbers fNumbers = new FibonacciNumbers(); //creates new object
Scanner in = new Scanner(System.in);
String again;
String test;
boolean IsRepeat = true;
boolean isQuit;
try {
isQuit = false;
while (!isQuit) {
System.out.print("Enter the number you want to convert to Fibanocci('q' to quit): ");
int n = in.nextInt();
System.out.print("The Fibanocci number for " + n + " is: ");
n = fNumbers.fOf(n);
System.out.println(n);
System.out.print("Do you want to run again? (Y or N): ");
again = in.next();
if (again.equalsIgnoreCase("N")) {
System.out.println("Thank you! Please terminate the program by entering 'Q' or 'q' OR you can cotinue by entering anything else: ");
String toQuit = in.next();
if ((toQuit.charAt(0) == 'q') || (toQuit.charAt(0) == 'Q')) {
System.out.println("Good-bye!");
isQuit = true;
}
} else {
IsRepeat = true;
}
}
} catch (InputMismatchException ex) {
test = in.nextLine();
if ((test.charAt(0) == 'q') || (test.charAt(0) == 'Q')) {
System.out.println("Good-bye!");
isQuit = true;
} else {
System.out.println("Invalid input!");
System.out.println("Try again! ");
isQuit = false;
}
}
}
}
This part where i put isQuit = false; at the end it just stops. I want it to continue.
Try putting your try catch statement inside of your while loop.