Java 消除代码中的递归
我的代码有递归再次执行调用等。我想在main或execute中使用do/while循环来消除这种情况,但如果这样做,询问用户是否希望再次播放的字符串将超出范围。我不能在循环之前或之后使用控制台2,如果在开始时询问他们是否想再次玩,这是没有意义的。 请随意指出我的初学者代码中的任何缺陷,因为可能有很多缺陷。谢谢 以下是我到目前为止关于递归的代码:Java 消除代码中的递归,java,recursion,do-while,Java,Recursion,Do While,我的代码有递归再次执行调用等。我想在main或execute中使用do/while循环来消除这种情况,但如果这样做,询问用户是否希望再次播放的字符串将超出范围。我不能在循环之前或之后使用控制台2,如果在开始时询问他们是否想再次玩,这是没有意义的。 请随意指出我的初学者代码中的任何缺陷,因为可能有很多缺陷。谢谢 以下是我到目前为止关于递归的代码: import java.util.*; import java.awt.*; import java.io.*; public class Rock
import java.util.*;
import java.awt.*;
import java.io.*;
public class RockPaperScissors {
public static int count = 0;
public static void main(String[] args) {
execute();
}
public static void execute(){
System.out.println("This program will allow you to play \n\"Rock, Paper, Scissors\" against a computer.");
System.out.println();
System.out.println("Enter 'r' for Rock, 'p' for Paper, or s for Scissors.");
System.out.println("Enter 'w' to have an insta-win for that round. Enter '-1' at anytime to exit program.");
String info = userInput();
int value = guessCode();
decideOutcome(value, info);
again();
}
public static String userInput() {
Scanner console = new Scanner (System.in);
String s = console.next();
return s;
}
public static int guessCode() {
Random r = new Random ();
return (r.nextInt(3)+1); // Random integer between 1 and 3;
}
public static void decideOutcome(int i, String j) {
try {
PrintStream output = new PrintStream(new FileOutputStream("records.txt", true));
if (j.equalsIgnoreCase("rock")|| j.equalsIgnoreCase("r")) {
count++;
switch (i){
case 1:
System.out.println("You've won! Computer picked scissors.");
output.println(count + " Win ");
break;
case 2:
System.out.println("You've tied.... Computer also picked rock.");
output.println(count + " Tie ");
break;
case 3:
System.out.println("You've lost. Computer picked paper.");
output.println(count + " Loss ");
break;
}
} else if (j.equalsIgnoreCase("paper")|| j.equalsIgnoreCase("p")) {
count++;
switch (i){
case 1:
System.out.println("You've lost; Computer picked scissors.");
output.println(count + " Loss ");
break;
case 2:
System.out.println("You've won! Computer picked rock.");
output.println(count + " Win ");
break;
case 3:
System.out.println("You've tied.... Computer also picked paper.");
output.println(count + " Tie ");
break;
}
} else if (j.equalsIgnoreCase("scissors")|| j.equalsIgnoreCase("s")) {
count++;
switch (i){
case 1:
System.out.println("You've tied.... Computer picked scissors.");
output.println(count + " Tie ");
break;
case 2:
System.out.println("You've lost; Computer picked rock.");
output.println(count + " Loss ");
break;
case 3:
System.out.println("You've won! Computer also picked paper.");
output.println(count + " Win ");
break;
}
} else if (j.equalsIgnoreCase("w")) {
count++;
System.out.println("You've effortlessly defeated the computer!");
output.println(count + " Win ");
} else if (j.equals("-1")) {
System.out.println("Thanks for playing!"); // need to find way to reach end.
if (count == 1) { // If the user terminates after the first match.
System.out.println("You've played a single match.");
} else if (count > 1) { // Anything more than 1 match played upon termination.
System.out.println("You've played " + count + " matches total.");
} else { // This is for exceptions when user inputs gibberish for their sign and then 'no' for the second input.
System.out.println("No matches were played.");
}
System.out.println("Good Bye!");
System.exit(0);
} else {
System.out.println("You didn't input the right thing.");
}
} catch (FileNotFoundException e) {
System.out.println("File was not found; try again");
}
}
public static void again() {
System.out.println("Do you want to play again? (Type in 'y' for Yes or 'n' for No.)");
Scanner console2 = new Scanner (System.in);
String t = console2.next();
while (t.equalsIgnoreCase("yes")||t.equalsIgnoreCase("y")) {
System.out.println();
System.out.println();
execute(); //
}
if (t.equalsIgnoreCase("no") || t.equalsIgnoreCase("n") || t.equals("-1")) {
System.out.println("Hope you had fun! I'm sure I've had just as much fun with making this program! Good Bye!");
if (count == 1) { // If the user terminates after the first match.
System.out.println("You've played a single match.");
} else if (count > 1) { // Anything more than 1 match played upon termination.
System.out.println("You've played " + count + " matches total.");
} else { // This is for exceptions when user inputs gibberish for their sign and then 'no' for the second input.
System.out.println("No matches were played.");
}
System.exit(0);
} else { // If the user doesn't input 'yes' or 'no.'
System.out.println("Not the proper response, but it's assumed that you don't want to continue.");
if (count == 1) { // If the user terminates after the first match.
System.out.println("You've completed a single match.");
} else if (count >= 2) { // Anything more than 1 match played upon termination.
System.out.println("You've completed " + count + " matches total.");
} else { // The user haphazardly messes up both inputs.
System.out.println("No matches were finished.");
}
System.exit(0);
}
}
}
您可以创建一个新的布尔变量keepplay,将其初始化为true,然后在玩家希望退出游戏时,将其设置为false 然后在主要方法中说:
public static void main(String[] args) {
System.out.println("This program will allow you to play \n\"Rock, Paper, Scissors\" against a computer.");
System.out.println();
System.out.println("Enter 'r' for Rock, 'p' for Paper, or s for Scissors.");
System.out.println("Enter 'w' to have an insta-win for that round. Enter '-1' at anytime to exit program.");
while (keepPlaying == true) {
again();
}
}
将输入代码放在方法的开头
然后你就不需要执行了。正如另一个答案所建议的那样,除非其他类需要,否则方法和变量应该是公共的。一个简单的更改是在主方法执行时执行,然后在主方法执行时测试,结果可能是一个简单的布尔值,以了解用户是否希望继续
顺便说一句,在我看来,execute应该是唯一的公共方法,没有理由让所有东西都是静态的。count是一个隐私的候选人。否则,重新访问该类将无法确保其初始值为0,例如。以下是您的代码的修改版本。有几行重复的代码可以在不同的条件下重复使用。我们已经修改了它们。将其与您的代码进行比较,以检查已更改的内容。希望能有帮助
import java.util.*;
import java.awt.*;
import java.io.*;
public class Test {
public static int count = 0;
private static Scanner console;
public static void main(String[] args) {
console = new Scanner(System.in);
execute();
}
public static void execute() {
String info = null;
boolean accept;
do {
System.out
.println("This program will allow you to play \n\"Rock, Paper, Scissors\" against a computer.");
System.out.println();
System.out
.println("Enter 'r' for Rock, 'p' for Paper, or s for Scissors.");
System.out
.println("Enter 'w' to have an insta-win for that round. Enter '-1' at anytime to exit program.");
info = userInput();
int value = guessCode();
decideOutcome(value, info);
accept = again();
} while (!info.equalsIgnoreCase("-1") && accept);
}
public static String userInput() {
String s = console.next();
return s;
}
public static int guessCode() {
Random r = new Random();
return (r.nextInt(3) + 1); // Random integer between 1 and 3;
}
public static void decideOutcome(int i, String j) {
try {
PrintStream output = new PrintStream(new FileOutputStream(
"records.txt", true));
if (j.equalsIgnoreCase("w")) {
count++;
System.out
.println("You've effortlessly defeated the computer!");
output.println(count + " Win ");
} else if (j.equals("-1")) {
System.out.println("Thanks for playing!"); // need to find way
// to reach end.
if (count == 1) { // If the user terminates after the first
// match.
System.out.println("You've played a single match.");
} else if (count > 1) { // Anything more than 1 match played
// upon termination.
System.out.println("You've played " + count
+ " matches total.");
} else { // This is for exceptions when user inputs gibberish
// for their sign and then 'no' for the second
// input.
System.out.println("No matches were played.");
}
System.out.println("Good Bye!");
System.exit(0);
} else if (j.equalsIgnoreCase("rock") || j.equalsIgnoreCase("r")
|| j.equalsIgnoreCase("paper") || j.equalsIgnoreCase("p")
|| j.equalsIgnoreCase("scissors")
|| j.equalsIgnoreCase("s")) {
count++;
switch (i) {
case 1:
if (j.equalsIgnoreCase("rock") || j.equalsIgnoreCase("r")) {
System.out
.println("You've won! Computer picked scissors.");
output.println(count + " Win ");
}
if (j.equalsIgnoreCase("paper") || j.equalsIgnoreCase("p")) {
System.out
.println("You've lost; Computer picked scissors.");
output.println(count + " Loss ");
}
if (j.equalsIgnoreCase("scissors")
|| j.equalsIgnoreCase("s")) {
System.out
.println("You've tied.... Computer also picked scissors.");
output.println(count + " Tie ");
}
break;
case 2:
if (j.equalsIgnoreCase("rock") || j.equalsIgnoreCase("r")) {
System.out
.println("You've tied.... Computer also picked rock.");
output.println(count + " Tie ");
}
if (j.equalsIgnoreCase("paper") || j.equalsIgnoreCase("p")) {
System.out.println("You've won! Computer picked rock.");
output.println(count + " Win ");
}
if (j.equalsIgnoreCase("scissors")
|| j.equalsIgnoreCase("s")) {
System.out
.println("You've lost; Computer picked rock.");
output.println(count + " Loss ");
}
break;
case 3:
if (j.equalsIgnoreCase("rock") || j.equalsIgnoreCase("r")) {
System.out
.println("You've lost. Computer picked paper.");
output.println(count + " Loss ");
}
if (j.equalsIgnoreCase("paper") || j.equalsIgnoreCase("p")) {
System.out
.println("You've tied.... Computer also picked paper.");
output.println(count + " Tie ");
}
if (j.equalsIgnoreCase("scissors")
|| j.equalsIgnoreCase("s")) {
System.out
.println("You've won! Computer picked paper.");
output.println(count + " Win ");
break;
}
}
} else {
System.out.println("You didn't input the right thing.");
}
} catch (FileNotFoundException e) {
System.out.println("File was not found; try again");
}
}
public static boolean again() {
System.out
.println("Do you want to play again? (Type in 'y' for Yes or 'n' for No.)");
String t = console.next();
if (t.equalsIgnoreCase("yes") || t.equalsIgnoreCase("y")) {
System.out.println();
System.out.println();
return true; //w
} else {
if (t.equalsIgnoreCase("no") || t.equalsIgnoreCase("n")
|| t.equals("-1")) {
System.out
.println("Hope you had fun! I'm sure I've had just as much fun with making this program! Good Bye!");
} else {
System.out
.println("Not the proper response, but it's assumed that you don't want to continue.");
}
if (count == 1) { // If the user terminates after the first match.
System.out.println("You've played a single match.");
} else if (count > 1) { // Anything more than 1 match played upon
// termination.
System.out
.println("You've played " + count + " matches total.");
} else { // This is for exceptions when user inputs gibberish for
// their sign and then 'no' for the second input.
System.out.println("No matches were played.");
}
return false;
}
}
}
import java.util.*;
import java.awt.*;
import java.io.*;
public class Test {
public static int count = 0;
private static Scanner console;
public static void main(String[] args) {
console = new Scanner(System.in);
execute();
}
public static void execute() {
String info = null;
boolean accept;
do {
System.out
.println("This program will allow you to play \n\"Rock, Paper, Scissors\" against a computer.");
System.out.println();
System.out
.println("Enter 'r' for Rock, 'p' for Paper, or s for Scissors.");
System.out
.println("Enter 'w' to have an insta-win for that round. Enter '-1' at anytime to exit program.");
info = userInput();
int value = guessCode();
decideOutcome(value, info);
accept = again();
} while (!info.equalsIgnoreCase("-1") && accept);
}
public static String userInput() {
String s = console.next();
return s;
}
public static int guessCode() {
Random r = new Random();
return (r.nextInt(3) + 1); // Random integer between 1 and 3;
}
public static void decideOutcome(int i, String j) {
try {
PrintStream output = new PrintStream(new FileOutputStream(
"records.txt", true));
if (j.equalsIgnoreCase("w")) {
count++;
System.out
.println("You've effortlessly defeated the computer!");
output.println(count + " Win ");
} else if (j.equals("-1")) {
System.out.println("Thanks for playing!"); // need to find way
// to reach end.
if (count == 1) { // If the user terminates after the first
// match.
System.out.println("You've played a single match.");
} else if (count > 1) { // Anything more than 1 match played
// upon termination.
System.out.println("You've played " + count
+ " matches total.");
} else { // This is for exceptions when user inputs gibberish
// for their sign and then 'no' for the second
// input.
System.out.println("No matches were played.");
}
System.out.println("Good Bye!");
System.exit(0);
} else if (j.equalsIgnoreCase("rock") || j.equalsIgnoreCase("r")
|| j.equalsIgnoreCase("paper") || j.equalsIgnoreCase("p")
|| j.equalsIgnoreCase("scissors")
|| j.equalsIgnoreCase("s")) {
count++;
switch (i) {
case 1:
if (j.equalsIgnoreCase("rock") || j.equalsIgnoreCase("r")) {
System.out
.println("You've won! Computer picked scissors.");
output.println(count + " Win ");
}
if (j.equalsIgnoreCase("paper") || j.equalsIgnoreCase("p")) {
System.out
.println("You've lost; Computer picked scissors.");
output.println(count + " Loss ");
}
if (j.equalsIgnoreCase("scissors")
|| j.equalsIgnoreCase("s")) {
System.out
.println("You've tied.... Computer also picked scissors.");
output.println(count + " Tie ");
}
break;
case 2:
if (j.equalsIgnoreCase("rock") || j.equalsIgnoreCase("r")) {
System.out
.println("You've tied.... Computer also picked rock.");
output.println(count + " Tie ");
}
if (j.equalsIgnoreCase("paper") || j.equalsIgnoreCase("p")) {
System.out.println("You've won! Computer picked rock.");
output.println(count + " Win ");
}
if (j.equalsIgnoreCase("scissors")
|| j.equalsIgnoreCase("s")) {
System.out
.println("You've lost; Computer picked rock.");
output.println(count + " Loss ");
}
break;
case 3:
if (j.equalsIgnoreCase("rock") || j.equalsIgnoreCase("r")) {
System.out
.println("You've lost. Computer picked paper.");
output.println(count + " Loss ");
}
if (j.equalsIgnoreCase("paper") || j.equalsIgnoreCase("p")) {
System.out
.println("You've tied.... Computer also picked paper.");
output.println(count + " Tie ");
}
if (j.equalsIgnoreCase("scissors")
|| j.equalsIgnoreCase("s")) {
System.out
.println("You've won! Computer picked paper.");
output.println(count + " Win ");
break;
}
}
} else {
System.out.println("You didn't input the right thing.");
}
} catch (FileNotFoundException e) {
System.out.println("File was not found; try again");
}
}
public static boolean again() {
System.out
.println("Do you want to play again? (Type in 'y' for Yes or 'n' for No.)");
String t = console.next();
if (t.equalsIgnoreCase("yes") || t.equalsIgnoreCase("y")) {
System.out.println();
System.out.println();
return true; //w
} else {
if (t.equalsIgnoreCase("no") || t.equalsIgnoreCase("n")
|| t.equals("-1")) {
System.out
.println("Hope you had fun! I'm sure I've had just as much fun with making this program! Good Bye!");
} else {
System.out
.println("Not the proper response, but it's assumed that you don't want to continue.");
}
if (count == 1) { // If the user terminates after the first match.
System.out.println("You've played a single match.");
} else if (count > 1) { // Anything more than 1 match played upon
// termination.
System.out
.println("You've played " + count + " matches total.");
} else { // This is for exceptions when user inputs gibberish for
// their sign and then 'no' for the second input.
System.out.println("No matches were played.");
}
return false;
}
}
}