Java-一旦我的 proram 具有第一个值 运行,我如何输入新的输入?
Java-How can I put a new input in once my proram has run with the first value?
抱歉,我对编程还很陌生,我正在尝试制作一个石头剪刀布程序。它似乎工作正常,但一旦我输入(石头、布、剪刀),它就会给我我的结果,然后在控制台中终止。我怎样才能得到它,这样我就可以玩更多的回合并且它可以保存我的分数而无需再次 运行 程序。我尝试使用 for 和 while 循环,但它所做的只是打印游戏的结果,因为它循环多次,然后终止。
再次抱歉,如果有人问过这个问题或者这个问题非常明显,我到处都在寻找,却一无所知。
主要代码
public class RockPaperScissorsMain {
public static void main(String[] args) {
System.out.println("Hello and welcome to Rock, Paper, Scissors");
System.out.println("Please select either Rock, Paper or Scissors...");
System.out.println("You can type \"Quit\" at any time to exit the game =)");
ActionClass myMethod= new ActionClass();
myMethod.method();
}
}
代码
package rockPaperScissors;
import java.util.Scanner;
import java.util.Random;
public class ActionClass {
int winCounter=0;
int loseCounter=0; //COUNTER VARIABLES
int tieCounter=0;
String[] selection= {"Rock", "Paper", "Scissors"};
Random rand =new Random(); // VARIABLES
Scanner scanny = new Scanner(System.in);
char inputChar= scanny.nextLine().charAt(0);
String opponentSelection= selection[rand.nextInt(3)];
char playerSelection= Character.toLowerCase(inputChar);
public void method() {
if(playerSelection=='q') {
System.out.println("===============================================================================");
System.out.println("THANK YOU FOR PLAYING TAKE CARE!!!");
System.out.println("==============================================================================");
System.exit(0); }
else if (playerSelection=='r') {
System.out.println("you said "+selection[0]+ "\t\t\t\t\t\t\t\t\t\t\t Your opponent chooses... " + opponentSelection);
System.out.println("======================================================================================================================================================");
}
else if(playerSelection== 'p') System.out.println("you said " +selection[1]+ "\t\t\t\t\t\t\t\t\t\t\t Your opponent chooses... " +opponentSelection);
else if( playerSelection=='s') System.out.println("you said " +selection[2]+ "\t\t\t\t\t\t\t\t\t\t\t Your opponent chooses... " +opponentSelection);
else System.out.println("\nSorry please select a valid option of ROCK, PAPER or SCISSORS");
if (opponentSelection.equals(selection[0])) { System.out.println("you TIED"); tieCounter++;}
if (opponentSelection.equals(selection[1])) {System.out.println("you LOSE"); loseCounter++;}
if (opponentSelection.equals(selection[2])) { System.out.println("you WIN"); winCounter++;}
if ((inputChar=='r')||inputChar=='p'||inputChar=='s') {System.out.println("Good Game!!!");
System.out.println("\n\n\t\t\t\t\tPlay again!! please select either Rock, Paper or Scissors...");
System.out.println("\n\t\t\t\t\tYou've WON: "+winCounter+ " times!!!" +"You\'ve TIED " +tieCounter+" times!!! You've LOST: " +loseCounter+ " times!!!" );
}
}}
您的 ActionClass class 中有大量错误。我在这里修复了它:
import java.util.Scanner;
import java.util.Random;
public class ActionClass {
int winCounter = 0;
int loseCounter = 0; //COUNTER VARIABLES
int tieCounter = 0;
public void method() {
String[] selection = {"Rock", "Paper", "Scissors"};
Random rand = new Random(); // VARIABLES
Scanner scanny = new Scanner(System.in);
char inputChar = scanny.nextLine().charAt(0);
char playerSelection = Character.toLowerCase(inputChar);
while (playerSelection != 'q') {
String opponentSelection = selection[rand.nextInt(3)];
if (playerSelection == 'q') {
System.out.println("===============================================================================");
System.out.println("THANK YOU FOR PLAYING TAKE CARE!!!");
System.out.println("==============================================================================");
System.exit(0);
} else if (playerSelection == 'r') {
System.out.println("you said " + selection[0] + "\t\t\t\t\t\t\t\t\t\t\t Your opponent chooses... " + opponentSelection);
System.out.println("======================================================================================================================================================");
} else if (playerSelection == 'p')
System.out.println("you said " + selection[1] + "\t\t\t\t\t\t\t\t\t\t\t Your opponent chooses... " + opponentSelection);
else if (playerSelection == 's')
System.out.println("you said " + selection[2] + "\t\t\t\t\t\t\t\t\t\t\t Your opponent chooses... " + opponentSelection);
else System.out.println("\nSorry please select a valid option of ROCK, PAPER or SCISSORS");
if (opponentSelection.equals(selection[0])) {
System.out.println("you TIED");
tieCounter++;
}
if (opponentSelection.equals(selection[1])) {
System.out.println("you LOSE");
loseCounter++;
}
if (opponentSelection.equals(selection[2])) {
System.out.println("you WIN");
winCounter++;
}
if ((inputChar == 'r') || inputChar == 'p' || inputChar == 's') {
System.out.println("Good Game!!!");
System.out.println("\n\n\t\t\t\t\tPlay again!! please select either Rock, Paper or Scissors...");
System.out.println("\n\t\t\t\t\tYou've WON: " + winCounter + " times!!!" + "You\'ve TIED " + tieCounter + " times!!! You've LOST: " + loseCounter + " times!!!");
}
playerSelection = scanny.nextLine().charAt(0);
}
}
}
基本上,您需要将逻辑放入一个循环中,这就是这一行的作用:
while (playerSelection != 'q')
程序获取输入并进行R,P,S检查后,在while循环结束前最后一行再次调用scanner获取用户的下一个意图。
抱歉,我对编程还很陌生,我正在尝试制作一个石头剪刀布程序。它似乎工作正常,但一旦我输入(石头、布、剪刀),它就会给我我的结果,然后在控制台中终止。我怎样才能得到它,这样我就可以玩更多的回合并且它可以保存我的分数而无需再次 运行 程序。我尝试使用 for 和 while 循环,但它所做的只是打印游戏的结果,因为它循环多次,然后终止。 再次抱歉,如果有人问过这个问题或者这个问题非常明显,我到处都在寻找,却一无所知。 主要代码
public class RockPaperScissorsMain {
public static void main(String[] args) {
System.out.println("Hello and welcome to Rock, Paper, Scissors");
System.out.println("Please select either Rock, Paper or Scissors...");
System.out.println("You can type \"Quit\" at any time to exit the game =)");
ActionClass myMethod= new ActionClass();
myMethod.method();
}
}
代码
package rockPaperScissors;
import java.util.Scanner;
import java.util.Random;
public class ActionClass {
int winCounter=0;
int loseCounter=0; //COUNTER VARIABLES
int tieCounter=0;
String[] selection= {"Rock", "Paper", "Scissors"};
Random rand =new Random(); // VARIABLES
Scanner scanny = new Scanner(System.in);
char inputChar= scanny.nextLine().charAt(0);
String opponentSelection= selection[rand.nextInt(3)];
char playerSelection= Character.toLowerCase(inputChar);
public void method() {
if(playerSelection=='q') {
System.out.println("===============================================================================");
System.out.println("THANK YOU FOR PLAYING TAKE CARE!!!");
System.out.println("==============================================================================");
System.exit(0); }
else if (playerSelection=='r') {
System.out.println("you said "+selection[0]+ "\t\t\t\t\t\t\t\t\t\t\t Your opponent chooses... " + opponentSelection);
System.out.println("======================================================================================================================================================");
}
else if(playerSelection== 'p') System.out.println("you said " +selection[1]+ "\t\t\t\t\t\t\t\t\t\t\t Your opponent chooses... " +opponentSelection);
else if( playerSelection=='s') System.out.println("you said " +selection[2]+ "\t\t\t\t\t\t\t\t\t\t\t Your opponent chooses... " +opponentSelection);
else System.out.println("\nSorry please select a valid option of ROCK, PAPER or SCISSORS");
if (opponentSelection.equals(selection[0])) { System.out.println("you TIED"); tieCounter++;}
if (opponentSelection.equals(selection[1])) {System.out.println("you LOSE"); loseCounter++;}
if (opponentSelection.equals(selection[2])) { System.out.println("you WIN"); winCounter++;}
if ((inputChar=='r')||inputChar=='p'||inputChar=='s') {System.out.println("Good Game!!!");
System.out.println("\n\n\t\t\t\t\tPlay again!! please select either Rock, Paper or Scissors...");
System.out.println("\n\t\t\t\t\tYou've WON: "+winCounter+ " times!!!" +"You\'ve TIED " +tieCounter+" times!!! You've LOST: " +loseCounter+ " times!!!" );
}
}}
您的 ActionClass class 中有大量错误。我在这里修复了它:
import java.util.Scanner;
import java.util.Random;
public class ActionClass {
int winCounter = 0;
int loseCounter = 0; //COUNTER VARIABLES
int tieCounter = 0;
public void method() {
String[] selection = {"Rock", "Paper", "Scissors"};
Random rand = new Random(); // VARIABLES
Scanner scanny = new Scanner(System.in);
char inputChar = scanny.nextLine().charAt(0);
char playerSelection = Character.toLowerCase(inputChar);
while (playerSelection != 'q') {
String opponentSelection = selection[rand.nextInt(3)];
if (playerSelection == 'q') {
System.out.println("===============================================================================");
System.out.println("THANK YOU FOR PLAYING TAKE CARE!!!");
System.out.println("==============================================================================");
System.exit(0);
} else if (playerSelection == 'r') {
System.out.println("you said " + selection[0] + "\t\t\t\t\t\t\t\t\t\t\t Your opponent chooses... " + opponentSelection);
System.out.println("======================================================================================================================================================");
} else if (playerSelection == 'p')
System.out.println("you said " + selection[1] + "\t\t\t\t\t\t\t\t\t\t\t Your opponent chooses... " + opponentSelection);
else if (playerSelection == 's')
System.out.println("you said " + selection[2] + "\t\t\t\t\t\t\t\t\t\t\t Your opponent chooses... " + opponentSelection);
else System.out.println("\nSorry please select a valid option of ROCK, PAPER or SCISSORS");
if (opponentSelection.equals(selection[0])) {
System.out.println("you TIED");
tieCounter++;
}
if (opponentSelection.equals(selection[1])) {
System.out.println("you LOSE");
loseCounter++;
}
if (opponentSelection.equals(selection[2])) {
System.out.println("you WIN");
winCounter++;
}
if ((inputChar == 'r') || inputChar == 'p' || inputChar == 's') {
System.out.println("Good Game!!!");
System.out.println("\n\n\t\t\t\t\tPlay again!! please select either Rock, Paper or Scissors...");
System.out.println("\n\t\t\t\t\tYou've WON: " + winCounter + " times!!!" + "You\'ve TIED " + tieCounter + " times!!! You've LOST: " + loseCounter + " times!!!");
}
playerSelection = scanny.nextLine().charAt(0);
}
}
}
基本上,您需要将逻辑放入一个循环中,这就是这一行的作用:
while (playerSelection != 'q')
程序获取输入并进行R,P,S检查后,在while循环结束前最后一行再次调用scanner获取用户的下一个意图。