I need help I am new to Java programming and I don't know how to fix my code.
I am trying to make a 007 game. I have created the if statements and it isn't looping around. If I add a ! in front of the each statement in the do-while loop it causes a infinity loop.
How can I fix my programming.
import java.util.Scanner;
import java.util.Random;
public class DoubleOSeven {
public static void main(String[] args) {
System.out.println("Let's Play a game of 007 ");
System.out.println("You can SHOOT, BLOCK, AND RELOAD");
System.out.println("Both you and I start with one bullet");
Scanner input = new Scanner(System.in);
System.out.println("Let's Start, Enter (shoot, reload , or block) ");
String INput = input.nextLine();
//User and Computer ammo
int Userammo = 1;
int ammo = 1;
//Creates a random number between 1 and 3
Random rand = new Random();
int output = rand.nextInt(3) + 1;
do{
//User chooses shoot
if(INput.equals("shoot")){
Userammo --;
System.out.println("You took a shot, Your ammo count is at: " + Userammo);
//User chooses reload
}else if (INput.equals("reload")){
Userammo ++;
System.out.println("You reloaded, Your ammo count is at: " + Userammo);
//User chooses block
}else if(INput.equals("block")){
System.out.println("You blocked, Your ammo count is at: " + Userammo);
//If random is 1 shoot
}if(output == 1){
ammo ++;
System.out.println("I took a shot at you, My ammo count is at: " + ammo);
//If random is 2 block
}else if(output == 2){
System.out.println("I blocked, My ammo count is at: " + ammo);
//If random is 3 reload
}else if(output == 3){
ammo ++;
System.out.println("I reloaded, My ammo count is at: " + ammo);
//If both User and Computer shoot
}if(output == 1 && INput == "shoot"){
System.out.println("It's a tie you we both die");
}
}while((output == 3 && INput == "shoot") && (output == 1 && INput == "reload") && (output == 1 && INput == "shoot"));
}
}
while((output == 3 && INput == "shoot") && (output == 1 && INput == "reload") && (output == 1 && INput == "shoot"));
should be
while((output == 3 && INput.equals("shoot")) || (output == 1 && INput.equals("reload")) || (output == 1 && INput.equals("shoot")));
First of all: In order for the loop to work well, the question to the user and the computer random decision have to be inside the loop. In this way each loop has a new set of values for the variables.
// Declare variables
String INput = "";
Random rand = new Random();
int output = 0;
do{
// Ask user
System.out.println("Enter (shoot, reload , or block) ");
INput = input.nextLine();
// Computer decision
output = rand.nextInt(3) + 1;
...
Second: You need to have an explicit decision when to terminate the loop (ie the game). This can be done with one of the following
the user wants to end the game (that needs one more option quit in the user choices)
the computer wants to end the game (that needs a forth random number that means quit).
some condition from the combination of user/computer ammo and user/computer shoot. That is you can end the game if some player is shot and has zero ammo.
In all these cases you need to
declare a variable for the end of the game before the loop
decide what to do in the loop
continue the loop if not end of game
This is snippet for this:
boolean endOfGame = false;
...
do {
...
if ( INput.equals("quit")
|| (INput.equals("shoot") && ammo == 0)
|| (Userammo == 0 && output == 1) ) {
endOfGame = true;
}
...
} while (!endOfGame);
You could have the same efect if you put the decision in the while,
but in this way the decision for the end of game is more clear.
It is also possible to set the endOfGame to true in many different places in the loop.
Edit: Some notes on coding style etc
It is a common good practice the names of the variables to start with low case letters (eg instead of Userammo use userAmmo). Names starting with capital letters denote classes.
It makes the code easier to read if the names have some meaning. For example I would suggest the following changes for some of your variables
INput → userAction
Userammo → userAmmo
output → compAction
ammo → compAmmo
Use final constants to give meaning to numbers or strings that appear in your code repeatedly.
Start the if statement on a new line. (else if on the same line is ok)
Using these you could have written (and some comments are not needed)
public class DoubleOSeven {
public static final String S_SHOOT = "shoot";
public static final String S_BLOCK = "block";
public static final String S_RELOAD = "reload";
public static final int I_SHOOT = 1;
public static final int I_BLOCK = 2;
public static final int I_RELOAD = 3;
...
System.out.println("Enter ("+ S_SHOOT + ", " + S_RELOAD + " or " + S_BLOCK + ") ");
...
} else if(userAction.equals(S_BLOCK)){
System.out.println("You blocked, Your ammo count is at: " + userAmmo);
}
if(compAction == I_SHOOT){
compAmmo--; // I changed ++ to -- in order to be a fair game
System.out.println("I took a shot at you, My ammo count is at: " + compAmmo);
} else if(compAction == I_BLOCK){
System.out.println("I blocked, My ammo count is at: " + compAmmo);
...
As per your condition as below
while((output == 3 && INput == "shoot") && (output == 1 && INput == "reload") && (output == 1 && INput == "shoot"));
the loop will be executed only if the 3 conditions are true.
You need to make some changes in your condition
All your && cases that are evaluated for this loop result in a false.
Let's say, T for all true cases and F for all F cases
If T&&T&&T then yes we = T (the case for adding !)
If T&&F&&F then F
IF F&&T&&F then F
...
IF F&&F&&F then F
So you need to re-evaluate what condition you want to cause to loop.
Are we looking for
(output==3 && Input=="shoot") || (output==1 && input=="reload") || (output==1 && input=="shoot")
to cause the next iteration?
This would make the case, where any of them are T, we get T. If all of them are F, then we get F.
Related
I am trying to build a Number guessing game which has a while loop with 2 conditions in it. However even after 1 of those 2 conditions become false then also the loop keeps going on even though I have || operator between those conditions. Also if I clear out the first condition of while loop then the second one works just perfectly as I want it to but if it is present then idk why it doesn't stop. Here is my code:
import java.util.Random;
import java.util.Scanner;
public class NumberGuess {
public static void main(String args[]) {
Random num = new Random();
int number = 1 + num.nextInt(10);
Scanner inp = new Scanner(System.in);
boolean guessed = false;
System.out.println("Welcome to Number guess!");
int guess_count = 5;
while(guessed == false || guess_count > 0 ) {
System.out.print("Enter your guess: ");
int input = inp.nextInt();
if(input == number) {
System.out.println("You guessed it right!");
guessed = true;
}
else {
System.out.println("Nope!");
guessed = false;
guess_count--;
System.out.println("Guesses left: " + guess_count);
}
}
}
}
When counter goes below 0 then also the loop keeps going on but I don't want it to.
Please tell me where am I wrong.
You don't need an OR operator. What you need is an AND operator. Means your condition should be (guessed == false && guess_count > 0 ).
WHY?
Because the OR conditional operator works if either the condition is true, in your case if the user unable to guess 5 times then your guessed variable is still false while your guess_count is less than zero, so your one condition is true. The AND operator checks for both the condition.
The condition of your while loop should be changed from an OR || to an AND &&. Currently, the || will allow execution if either guessed == false OR guess_count > 0. Only one of these conditions must be true for the while loop to continue executing.
while(guessed == false && guess_count > 0 ) {
Change to an AND && means that if the number is guessed correctly, guessed will now be true and the while loop will halt after that iteration. I was not getting any errors for when the guess_counter dropped below zero. Try it again to be sure.
Consider modifying your loop like this:
while(guess_count > 0 ) {
System.out.print("Enter your guess: ");
int input = inp.nextInt();
if(input == number) {
System.out.println("You guessed it right!");
break;
}
else {
System.out.println("Nope!");
guessed = false;
guess_count--;
System.out.println("Guesses left: " + guess_count);
}
}
OR just modify you condition like this:
while(!guessed && guess_count > 0 ) {
You can correct your code by replacing || with &&. Why use an extra flag variable when you have guess_count decrementing to check for number of guesses. The program will terminate after guessed number is equal to the input. Here is simple implementation:
while(guess_count-->0) {
System.out.print("Enter your guess: ");
int input = inp.nextInt();
if(input == number) {
System.out.println("You guessed it right!");
guessed = true;
break; // The user has already guessed the number no need to guess more
}
else
System.out.println("Nope!\nGuesses left: " + guess_count);
I'm creating a little mini game of Hangman. The user has 10 chances to guess, but only 5 lives.
The app works, but will continue after the 5th life, even though, I was hoping it would throw the player out of that loop.
The instantiable class (Hangman.java) is working without problems.
The secret word is "julie" as described in the instantiable class.
My App class:
import javax.swing.JOptionPane;
public class HangmanApp {
public static void main(String args[]) {
String input, secret, result, playAgain;
char guess;
int i, j, k, lives;
Hangman myHangman = new Hangman();
do{
JOptionPane.showMessageDialog(null, "Hello, welcome to Hangman! You have 10 chances but only 5 lives! Best of luck");
lives = 5;
for (j = 10; j > 0; j--) {
while (lives >= 0){
input = JOptionPane.showInputDialog(null, "Please enter a letter");
guess = input.charAt(0);
//process
myHangman.setGuess(guess);
myHangman.compute();
result = myHangman.getResult();
if ((input.charAt(0) == 'j') || (input.charAt(0) == 'u') || (input.charAt(0) == 'l') || (input.charAt(0) == 'i') || (input.charAt(0) == 'e')) {
JOptionPane.showMessageDialog(null, "That letter is in the word! Current correct letters: " + result + ".");
} else {
lives--;
JOptionPane.showMessageDialog(null, "Sorry, that letter is not there. Current correct letters: " + result + ".");
}
//output
//JOptionPane.showMessageDialog(null, "Current correct letters: " + result);
};
lives = -1;
}
result = myHangman.getResult();
secret = myHangman.getSecret();
if (secret.equals(result)) {
JOptionPane.showMessageDialog(null, "Congratulations, you got it!! The word was " + secret + ".");
} else {
JOptionPane.showMessageDialog(null, "Sorry, you didn't get it, better look next time! The word was " + secret + ".");
}
playAgain = JOptionPane.showInputDialog("Do you want to play again? yes/no");
}while (playAgain.equals("yes"));
}
}
Try the following change:
while (lives > 0){
you start at 5 and then go down to 4 3 2 1 AND 0. with the change this will stop at 0
// don't need two nested cycles, you can do it in a single one
// The cycle exits if any one of the conditions fail
// max attempts exhausted or all the lives are lost
// -------------------v v------------------------
for (j = 10, lives=5; j > 0 && lives > 0 ; j--) {
// -------------------------------------^
// j - the number of attempt is decremented for each trial,
// no matter if successful or not
//... the rest of cycle logic, which will decrement the lives
// only in cases of unsuccessful attempts
}
So i am working on a school project and the project is to make a simplified yahtzee program with two of the yahtzee point systems, ex small ladder or three of a kind and so far i have made small ladder (1,2,3,4,5) and big ladder (2,3,4,5,6) to work but i have 2 questions.
1. How can i make a point system for three of a kind?
2. A part of the project is to make a category if you dont get one of the implied point systems, ex if you get (1,1,4,5,6) then the user is gonna get the option to choose which number he wants to pick for maximum points. I know i cant explain this very well since im swedish and my english isnt the best. But here is my code:
import javax.swing.*;
import java.util.*;
public class Projekt1 {
public static void main(String[] args) {
char fortsatta = 'j';
boolean ja;
do{
JOptionPane.showMessageDialog(null, "Välkommen till Johans Yatzy");
int starta = JOptionPane.showConfirmDialog(null, "Vill du starta spelet?", "Spela igen",
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
if(starta == JOptionPane.YES_OPTION ){
JOptionPane.showMessageDialog(null, "Du har nu startat spelet");
int[] tar = new int[5];
for(int i=0; i<5; i++){
tar[i] = (int)(Math.random()*6+1);
}
String output = "";
java.util.Arrays.sort(tar);
for (int i = 0; i < tar.length; i++) {
output = output + tar[i] + "\t";
}
JOptionPane.showMessageDialog(null, "Du har nu kastat dina tärningar och kasten blev följande: " + output);
if (tar[0] == 1 && tar[1] == 2 && tar[2]==3 && tar[3] == 4 && tar[4] == 5) {
JOptionPane.showMessageDialog(null, "Grattis, du fick liten stege som är värt 15 poäng");
}
else if (tar[0] == 2 && tar[1] == 3 && tar[2]==4 && tar[3] ==5 && tar[4] ==6) {
JOptionPane.showMessageDialog(null, "Grattis, du fick stor stege som är värt 20 poäng");
}
else{
JOptionPane.showMessageDialog(null, "Tärningskastet resulterade i varken liten stege eller stor stege");
}
}
else if(starta == JOptionPane.NO_OPTION){
JOptionPane.showMessageDialog(null, "Programmet kommer nu att stängas ner");
System.exit(0);
}
String startaom;
startaom = JOptionPane.showInputDialog("Vill du spela igen? Skriv J för att spela igen");
fortsatta = startaom.charAt(0);
}while(fortsatta == 'j');
}
}
I know you're only doing a simplified version of Yahtzee, so first, here is the simple way to check for three-of-a-kind:
if ((tar[0] == tar[1] && tar[1] == tar[2]) ||
(tar[1] == tar[2] && tar[2] == tar[3]) ||
(tar[2] == tar[3] && tar[3] == tar[4])) {
// found 3 of a kind
}
For a more full-blown version, there are a lot of points for various combinations of multiple-of-a-kind in the lower section:
Three-Of-A-Kind
Four-Of-A-Kind
Full House (Three-Of-A-Kind + Two-Of-A-Kind)
Yahtzee (Five-Of-A-Kind)
and for the extended Yatzy version (not Yahtzee):
One-Pair (Two-Of-A-Kind)
Two-Pair (Two-Of-A-Kind + Two-Of-A-Kind)
and for the upper section you need to count how many of a certain number is present.
So, you should start by doing the counts-by-number:
int[] count = new int[6];
for (int i = 0; i < 5; i++)
count[tar[i]-1]++;
Now find the top two counts:
int topIdx1 = 0, topIdx2 = 0;
for (int i = 1; i < 6; i++)
if (count[i] > count[topIdx1]) {
topIdx2 = topIdx1;
topIdx1 = i;
} else if (count[i] > count[topIdx2]) {
topIdx2 = i;
}
Now you can easily find your combination (and the points if doing Yatzy):
if (count[topIdx1] == 5) {
// Yahtzee
} else if (count[topIdx1] == 4) {
// Four-Of-A-Kind yatzyPoints = 4*(topIdx1+1)
} else if (count[topIdx1] == 3) {
if (count[topIdx2] == 2) {
// Full House yatzyPoints = 3*(topIdx1+1) + 2*(topIdx2+1)
} else {
// Three-Of-A-Kind yatzyPoints = 3*(topIdx1+1)
}
} else if (count[topIdx1] == 2) {
if (count[topIdx2] == 2) {
// Two-Pair yatzyPoints = 2*(topIdx1+1) + 2*(topIdx2+1)
} else {
// One-Pair yatzyPoints = 2*(topIdx1+1)
}
}
And for the upper section, the points are easily calculated:
points = number * count[number-1]
I'm assuming that the rules for Swedish Yahtzee are the same as the rules for Yahtzee in the United States.
How can i make a point system for three of a kind?
I'm assuming you're talking about the lower part of the score sheet.
You write a method that checks if your 5 dice have 3 of the same number, and return the sum of the dice as the score. Return zero if the 5 dice don't have 3 of the same number.
A part of the project is to make a category if you don't get one of the implied point systems, ex if you get (1,1,4,5,6) then the user is gonna get the option to choose which number he wants to pick for maximum points.
Now I'm assuming you're talking about the upper part of the score sheet.
You ask the user which category they wish to apply their points to. You only have to show the categories that have not been previously selected. The user can choose category 2 or 3, with a score of zero, if they wish.
If you're asking about how a computer should play categories, you calculate the category score that loses the minimum number of points from 3 of a kind.
In your example, category 1 would be the first choice, since you only lose one point. Category 2 loses 6 points, category 4 loses 8 points, category 3 loses 9 points, category 5 loses 10 points, and category 6 loses 12 points. As the game progresses, the computer has fewer category choices.
I am nearly finished with a Java project of mine. The objective is to play a game of Rock Paper Scissors with the user and the computer. The computer generates a random number and that number must correlate with either one of the three choices. I got it to where it can play successfully, but here is the catch, in the case of a tie, the game is to repeat and start over, otherwise it is to terminate. I have it to where it terminates, but cannot figure out how to get it to repeat the entire process in the case of a tie. The methods must remain the way they are, but my professor said the answer is if the entire thing is in a loop. My question is which loop should I use and where should it be placed? Here is the code:
public class FahrDylanRockPaperScissors{
public static void main (String [] args){
String computer = computerChoice();
String user = userChoice();
determineWinner(computer, user);
}
public static String computerChoice( ){
Random rand = new Random();
int cinput = rand.nextInt(3)+ 1;
String computer = "thing";
if (cinput == 1)
computer = "Rock";
if (cinput == 2)
computer = "Paper";
if (cinput == 3)
computer = "Scissors";
return computer;
}
public static String userChoice(){
Scanner sc = new Scanner (System.in);
String user = "default";
do{
System.out.println ("Let's Play a game! Rock, Paper, Scissors!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! " + "\nPlease enter either Rock, Paper, or Scissors: " + "\nGood Luck!");
user = sc.nextLine();
}
while (isValidChoice (user) == false);
return user;
}
public static boolean isValidChoice(String choice){
boolean status;
if (choice.compareTo("Rock")== 0)
status = true;
else if (choice.compareTo("Paper")== 0)
status = true;
else if (choice.compareTo("Scissors")== 0)
status = true;
else{
status = false;
System.out.println("Error! Make sure you are capitalizing your choices");
}
return status;
}
public static void determineWinner(String computer, String user){
System.out.println (" Computer Choice: " + computer);
System.out.println ("Your Choice : " + user);
if (computer.compareTo( "Rock" ) == 0 && user.compareTo ("Scissors") == 0)
System.out.println (" Computer wins! Better luck next time!");
if (computer.compareTo("Scissors")== 0 && user.compareTo("Paper") == 0)
System.out.println (" Computer wins! Better luck next time!");
if (computer.compareTo("Paper") == 0 && user.compareTo("Rock") == 0)
System.out.println (" Computer wins! Better luck next time!");
if (computer.compareTo("Rock") == 0 && user.compareTo("Paper") == 0)
System.out.println (" You win!!");
if (computer.compareTo("Scissors") == 0 && user.compareTo("Rock") == 0)
System.out.println (" You win!!");
if (computer.compareTo("Paper") == 0 && user.compareTo("Scissors") == 0)
System.out.println (" You win!!");
else if (computer.compareTo(user) == 0 )
System.out.println(" Tie! the game must be played again.");
}
}
You can use a do-while loop, because your code needs to be executed at least one time.
public static void main (String [] args){
boolean win = false;
do{
String computer = computerChoice();
String user = userChoice();
win = determineWinner(computer, user);
}while(!win);
}
For the first time you execute the whole code. Then the predicate is checked, and if someone won, the do-while will stop. But if win equals false it will be executed again.
You could achieve the same with only a while loop, or other loops. But because your code needs to be run at least one time a do-while suits well.
Edit:
You need to change your code, so that determineWinner returns back if someone won (return true) or if there is a tie (return false). I did not see that it currently has no return type when posting the answer.
A simple way to get the determineWinner method to work would be the following.
public static boolean determineWinner(String computer, String user){
System.out.println (" Computer Choice: " + computer);
System.out.println ("Your Choice : " + user);
if (computer.compareTo( "Rock" ) == 0 && user.compareTo ("Scissors") == 0)
System.out.println (" Computer wins! Better luck next time!");
if (computer.compareTo("Scissors")== 0 && user.compareTo("Paper") == 0)
System.out.println (" Computer wins! Better luck next time!");
if (computer.compareTo("Paper") == 0 && user.compareTo("Rock") == 0)
System.out.println (" Computer wins! Better luck next time!");
if (computer.compareTo("Rock") == 0 && user.compareTo("Paper") == 0)
System.out.println (" You win!!");
if (computer.compareTo("Scissors") == 0 && user.compareTo("Rock") == 0)
System.out.println (" You win!!");
if (computer.compareTo("Paper") == 0 && user.compareTo("Scissors") == 0)
System.out.println (" You win!!");
else if (computer.compareTo(user) == 0 ){
System.out.println(" Tie! the game must be played again.");
return false;
}
return true;
}
And for your coding style:
It's good practice to use brackets {} for if/else/for... even if you have only one statement, because it improves the readability of your code.
Edit 2:
Because you can't change something, the easiest way is probably the following:
public static void main(String[] args){
boolean tie = true;
do{
String computer = computerChoice();
String user = userChoice();
tie = (computer.compareTo(user) == 0);
determineWinner(computer, user);
}while(tie);
}
Even if determineWinner determines the winner you need it to give you feedback. If you can't get feedback, just determine if there will be a tie in your main-Method, and if you get a tie, repeat.
You could easily just make the three lines:
public static void playGame()
{
String computer = computerChoice();
String user = userChoice();
determineWinner(computer, user);
}
into one new method, something like playGame(). Then in the main function, you can call playGame(), and at the end of determineWinner(), you could have:
else if (computer.compareTo(user) == 0 )
{
System.out.println(" Tie! the game must be played again.");
playGame();
}
to play the game again.
Also, make sure you include the import statements for Random and Scanner. And make sure you close your scanner.
Use a flag for tie, something like this:
boolean tie = false;
combine with do while:
do {
} while (tie);
if tie == true than keep going until its false.
A do { ... } while () loop would be most natural for this problem, but pretty much any loop construct could serve.
Although you say
The methods must remain the way they are
you cannot do it if the determineWinner() method remains exactly as it now is. That method needs one way or another to communicate back to main() whether there is a tie, or else it must itself manage running a new game in the event of a tie. Were I free to do so, I would have it return a a boolean value indicating whether there was a tie. If it cannot be modified to return a value, then the other alternative would be for it to set a static variable to indicate whether there was a tie. Alternatively, you could solve this problem with recursion, but that would not involve a loop.
I was tasked with making a simple program that will play Rock-Paper-Scissors with the user. Unfortunately, when I try to run the program, my return(sentence+outcome) both returns null. I am new to using methods, so please explain what I am doing wrong here... Thank you!
package rockpaperscissors;
/**
*
* #author Owner
*/
import java.util.Scanner;
import java.io.IOException;
public class RockPaperScissors {
static int weaponChoice;
static int computerChoice;
static int tie = 0;
static int lose = 0;
static int win = 0;
static String outcome;
static String sentence;
/**
* #param args the command line arguments
* #throws java.io.IOException
*/
public static void main(String[] args) throws IOException {
Scanner userInput = new Scanner(System.in);
System.out.println(" =========================");
System.out.println("====ROCK=PAPER=SCISSORS====");
System.out.println(" =========================");
int playAgain = 1; //sets a play again function
do {
System.out.println("Please select your weapon.");
System.out.println("1 - Rock");
System.out.println("2 - Paper");
System.out.println("3 - Scissors");
System.out.println("Choose wisely:");
String choice = userInput.nextLine();
weaponChoice = Integer.parseInt(choice);
do {
if (weaponChoice != 1 && weaponChoice != 2 && weaponChoice != 3) {
System.out.println("Please choose again, grasshopper. There are only"
+ " three choices.");
System.out.println("1 - Rock");
System.out.println("2 - Paper");
System.out.println("3 - Scissors");
choice = userInput.nextLine();
weaponChoice = Integer.parseInt(choice);
}
} while (weaponChoice != 1 && weaponChoice != 2 && weaponChoice != 3);
if (weaponChoice == 1) {
System.out.println("You have selected Rock as your weapon.");
} else if (weaponChoice == 2) {
System.out.println("You have selected Paper as your weapon.");
} else {
System.out.println("You have selected Scissors as your weapon.");
}
//Computer's Choice
computerChoice = 1 + (int) (Math.random() * ((3 - 1) + 1));
if (computerChoice == 1) {
System.out.println("The computer has chosen Rock.");
} else if (computerChoice == 2) {
System.out.println("The computer has chosen Paper.");
} else {
System.out.println("The computer has chosen Scissors.");
}
determineOutcome(outcome, sentence);
System.out.println(sentence+outcome);
System.out.println("==SCORES==");
System.out.println("WINS: " + win);
System.out.println("TIES: " + tie);
System.out.println("LOSSES: " + lose);
System.out.println("Press 1 to play again, or any other number to exit.");
String play = userInput.nextLine();
playAgain = Integer.parseInt(play);
} while (playAgain == 1);
}
public static String determineOutcome(String outcome, String sentence) {
sentence = "Your result is: ";
do {
if (weaponChoice == 1 && computerChoice == 1 || weaponChoice == 2 && computerChoice == 2 || weaponChoice == 3 && computerChoice == 3) {
tie++;
outcome = "TIE";
} else if (weaponChoice == 1 && computerChoice == 3 || weaponChoice == 2 && computerChoice == 1 || weaponChoice == 3 && computerChoice == 2) {
win++;
outcome = "You WON!";
} else {
lose++;
outcome = "You LOSE. Better luck next time?";
}
} while (lose <0 || win < 0 || tie < 0);
return(sentence+outcome);
}
}
To make this work, you'll need to replace
determineOutcome(outcome, sentence);
System.out.println(sentence+outcome);
with
String valueReturned = determineOutcome(outcome, sentence);
System.out.println(valueReturned);
because Java is pass by value, not pass by reference. That means that the determineOutcome method will work with its own copies of outcome and sentence, and not modify the versions that belong to the method that called it.
But also, the method is not actually using the two arguments you pass into it. It would be much less confusing if you just omit the parameters entirely, and change it to public static String determineOutcome() ... and declare String sentence; and String outcome; inside the method.
When calling a function that is returning a result you must either call the function within System.out.println() or assign the result to a variable which you then print.
Option 1:
String result = determineOutcome(outcome, sentence);
System.out.println(result);
or option 2:
System.out.println(determineOutcome(outcome, sentence));
In addition to the points other people made in their answers, it looks like your while loop is never being executed. The variables tie, win and lose are never less than 0, so the condition of the while loop is never true. You can try changing it to:
while (lose <= 0 || win <= 0 || tie <= 0);
But be warned, this will result in an infinite loop, so you may need to rethink your logic. You may not need a loop here, depending on what you're trying to do.