gpt4 book ai didi

Java 字符串方法返回 Null?

转载 作者:行者123 更新时间:2023-12-01 22:33:31 26 4
gpt4 key购买 nike

我的任务是制作一个简单的程序,与用户一起玩石头剪刀布。不幸的是,当我尝试运行该程序时,我的 return(sentence+outcome) 都返回 null。我是使用方法的新手,所以请解释我在这里做错了什么......谢谢!

 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);
}
}

最佳答案

要使其正常工作,您需要替换

determineOutcome(outcome, sentence);
System.out.println(sentence+outcome);

String valueReturned = determineOutcome(outcome, sentence);
System.out.println(valueReturned);

因为Java是按值传递,而不是按引用传递。这意味着 defineOutcome 方法将使用其自己的 outcomesentence 副本,并且不会修改属于调用方法的版本它。

而且,该方法实际上并未使用您传递给它的两个参数。如果您完全省略参数,并将其更改为 public static String certainOutcome() ... 并声明 String Sent; ,那么混淆会少得多。字符串结果; 在方法内。

关于Java 字符串方法返回 Null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27219267/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com