Java字符串方法返回Null?
我的任务是制作一个简单的程序,与用户一起玩石头剪刀。不幸的是,当我尝试运行程序时,我的返回(句子+结果)都返回null。我是新的使用方法,所以请解释我在这里做错了什么。。。谢谢大家!Java字符串方法返回Null?,java,methods,Java,Methods,我的任务是制作一个简单的程序,与用户一起玩石头剪刀。不幸的是,当我尝试运行程序时,我的返回(句子+结果)都返回null。我是新的使用方法,所以请解释我在这里做错了什么。。。谢谢大家! package rockpaperscissors; /** * * @author Owner */ import java.util.Scanner; import java.io.IOException; public class RockPaperScissors { static i
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);
}
}
包装剪刀;
/**
*
*@作者所有者
*/
导入java.util.Scanner;
导入java.io.IOException;
公营剪纸机{
静态int武器选择;
静态选择;
静态int-tie=0;
静态int-lose=0;
静态int-win=0;
静态字符串结果;
静态字符串句;
/**
*@param指定命令行参数
*@抛出java.io.IOException
*/
公共静态void main(字符串[]args)引发IOException{
扫描仪用户输入=新扫描仪(System.in);
System.out.println(“==============================================”);
System.out.println(“==石头=布=剪刀==”;
System.out.println(“==============================================”);
int playreach=1;//设置一个playreach函数
做{
System.out.println(“请选择您的武器”);
System.out.println(“1-Rock”);
System.out.println(“2张纸”);
System.out.println(“3-剪刀”);
System.out.println(“明智地选择:”);
String choice=userInput.nextLine();
weaponChoice=Integer.parseInt(选项);
做{
if(武器选择!=1&&weaponChoice!=2&&weaponChoice!=3){
System.out.println(“请再次选择,蚱蜢。只有”
+"三个选择";;
System.out.println(“1-Rock”);
System.out.println(“2张纸”);
System.out.println(“3-剪刀”);
choice=userInput.nextLine();
weaponChoice=Integer.parseInt(选项);
}
}while(weaponChoice!=1&&weaponChoice!=2&&weaponChoice!=3);
如果(武器选择==1){
System.out.println(“您选择了岩石作为您的武器。”);
}否则如果(武器选择==2){
System.out.println(“您已选择纸张作为武器。”);
}否则{
System.out.println(“您已选择剪刀作为武器。”);
}
//计算机的选择
computerChoice=1+(int)(Math.random()*((3-1)+1));
如果(计算机选项==1){
System.out.println(“计算机选择了摇滚”);
}else if(computerChoice==2){
System.out.println(“计算机选择了纸张”);
}否则{
System.out.println(“计算机选择了剪刀”);
}
确定时间(结果、句子);
System.out.println(句子+结果);
System.out.println(“==分数=”);
System.out.println(“WINS:+win”);
System.out.println(“TIES:+tie”);
System.out.println(“损失:“+lose”);
System.out.println(“按1再次播放,或按任何其他数字退出”);
String play=userInput.nextLine();
playreach=Integer.parseInt(play);
}while(playreach==1);
}
公共静态字符串确定名(字符串结果、字符串语句){
句子=“你的结果是:”;
做{
如果(weaponChoice==1&&computerChoice==1 | | | weaponChoice==2 | | weaponChoice==3&&computerChoice==3){
tie++;
结果=“TIE”;
}else if(weaponChoice==1&&computerChoice==3 | | | weaponChoice==2&&computerChoice==1 | | weaponChoice==3&&computerChoice==2){
win++;
结果=“你赢了!”;
}否则{
丢失++;
结果=“你输了。下次好运吗?”;
}
}而(lose要使此功能正常工作,您需要更换
determineOutcome(outcome, sentence);
System.out.println(sentence+outcome);
与
因为Java是按值传递的,而不是按引用传递的。这意味着determinationOtcome
方法将使用它自己的结果
和句子
副本,而不会修改属于调用它的方法的版本
但是,该方法实际上并没有使用您传递给它的两个参数。如果您完全忽略参数,并将其更改为publicstaticstringdeterminationome(),那么就不会那么混乱了…
并在方法内部声明字符串语句;
和字符串结果;
。若要使此工作正常,需要替换
determineOutcome(outcome, sentence);
System.out.println(sentence+outcome);
与
因为Java是按值传递的,而不是按引用传递的。这意味着determinationOtcome
方法将使用它自己的结果
和句子
副本,而不会修改属于调用它的方法的版本
但是,该方法实际上并没有使用您传递给它的两个参数。如果您完全忽略参数,并将其更改为publicstaticstringdeterminationome(),那么就不会那么混乱了…
并在方法内部声明字符串语句;
和字符串结果;
。若要使此工作正常,需要替换
determineOutcome(outcome, sentence);
System.out.println(sentence+outcome);
与
因为Java是按值传递的,而不是按引用传递的。这意味着determinationOtcome
方法将使用它自己的结果
和句子
副本,而不会修改属于调用它的方法的版本
但是,该方法实际上并没有使用传递给它的两个参数。如果完全忽略参数,并将其更改为publicstaticstringdetermineoutcom()…
和declareString句子;
和stringoutcom,那么就不会那么混乱了
System.out.println(determineOutcome(outcome, sentence));
while (lose <= 0 || win <= 0 || tie <= 0);