Java 一种占用更少空间的更高效的方法?顺便说一句,这是一种方法
这里是一个石头剪纸项目的部分代码,我只是想知道是否有一种方法可以做到这一点,占用更少的空间。最好效率更高。基本上这是一种方法,这种方法所做的是比较用户输入,看一方是否击败另一方。 谢谢Java 一种占用更少空间的更高效的方法?顺便说一句,这是一种方法,java,Java,这里是一个石头剪纸项目的部分代码,我只是想知道是否有一种方法可以做到这一点,占用更少的空间。最好效率更高。基本上这是一种方法,这种方法所做的是比较用户输入,看一方是否击败另一方。 谢谢 public String determineWinner() { String winner = "yolo"; //if fail if(compChoice.equals("S") && (playChoice.equals("s"))) { win
public String determineWinner()
{
String winner = "yolo"; //if fail
if(compChoice.equals("S") && (playChoice.equals("s")))
{
winner = "nobody. There was a tie because you guessed the same thing.";
}
if(compChoice.equals("P") && (playChoice.equals("p")))
{
winner = "nobody. There was a tie because you guessed the same thing.";
}
if(compChoice.equals("R") && (playChoice.equals("r")))
{
winner = "nobody. There was a tie because you guessed the same thing.";
}
if(compChoice.equals(playChoice)) //R R, R P, R S
{
winner = "nobody. There was a tie because you guessed the same thing.";
}
if(compChoice.equals("R") && (playChoice.equals("P") || playChoice.equals("p"))) //R P
{
winner = "player because Paper beats Rock.";
}
if(compChoice.equals("R") && (playChoice.equals("S") || playChoice.equals("s"))) //R S
{
winner = "computer because Rock beats Scissors.";
}
if(compChoice.equals("P") && (playChoice.equals("R") || playChoice.equals("r")))//P R
{
winner = "computer because Paper beats Rock.";
}
if(compChoice.equals("P") && (playChoice.equals("S") || playChoice.equals("s")))//P S
{
winner = "player because Scissors beats Paper.";
}
if(compChoice.equals("S") && (playChoice.equals("R") || playChoice.equals("r"))) //S R
{
winner = "player because Rock beats Scissors.";
}
if(compChoice.equals("S") && (playChoice.equals("P") || playChoice.equals("p"))) //S P
{
winner = "computer because Scissors beats Paper.";
}
return winner;
}
为了提高效率,我建议您使用嵌套的
if
else
语句,而不是仅当
if {} else{if(){} else{....}}
在代码中,每个if
循环都将被执行,这会降低效率,因此请使用嵌套的if-else
您可以使用更高效的
char
比较
char cc = compChoice.charAt(0);
char pc = playChoice.charAt(0);
if (cc == 'S' && pc == 's') {
...
} else if (
...
JqueryLearner是对的,但如果您这样做,您会得到更干净的
if-else
:
if () {
} else if () {
} else if () {
} else {
}
你需要更多地考虑一般规则和简化,而不是细节
例如,如果您接受输入并将其设置为小写,那么您只需要检查r,s等,而不是r,s等
如果你检查这两个是否相等(你不在乎它的r和r,或者s和s,你只需要它们是相同的),这将处理所有的绘图
然后您只需要检查r>s>p>r.您可以使用if子句来检查计算机和播放器的选择是否相同,例如:
if(compChoice.equalsIgnoreCase(playerChoice)){
return "TIE"
}
注意我使用了ignoreCase
来避免对小写和大写使用多个if
或
创建选项阵列:[纸、石头、剪刀]
现在纸打败石头,石头打败剪刀,剪刀打败纸。
您必须从该数组中计算comp和player选项的索引,然后使用以下公式计算结果:
int result = (playerChoiceNum - compChoiceNum) % 3
playerChoiceNum和compChoiceNum是来自数组的索引。
若结果为0,则为平局;若结果为负,则为红利赢;若结果为正,则为玩家赢 你可以这样做
String[] words = "Rock,Paper,Scissors".split(",");
// turn the choice into an index where higher wins.
// i.e. 0 < 1 < 2 < 0 (using clock arithmetic)
int human = "RPS".indexOf(playChoice.toUpperCase());
int comp = "RPS".indexOf(compChoice.toUpperCase());
// if the index is the same, no winner
if (human == comp)
return "No winner, choices are the same";
// if the human has the higher index (using clock arithmetic), the human wins.
if (human == (comp+1) % 3)
return "Human winner as " + words[human] + " beats " + words[comp];
// otherwise the computer must have won.
return "Computer winner as " + words[comp] + " beats " + words[human];
String[]words=“石头、布、剪刀”。拆分(“,”);
//把选择变成一个指数,高者获胜。
//即0<1<2<0(使用时钟算法)
int human=“RPS.indexOf(playChoice.toUpperCase());
int comp=“RPS”.indexOf(compChoice.toUpperCase());
//如果索引相同,则没有赢家
如果(人==公司)
返回“没有赢家,选择相同”;
//如果人类有更高的指数(使用时钟算法),人类获胜。
如果(人类==(成分+1)%3)
将“人类赢家”返回为“+文字[人类]+”节拍“+文字[合成];
//否则电脑一定赢了。
返回“计算机优胜者为”+文字[comp]+“beats”+文字[human];
使用枚举如何
以下是整个游戏
public static enum RPS {
ROCK, PAPER, SCISSORS;
// Simple method to get the appropriate enum.
public static RPS fromString(String in) {
if (in.toLowerCase().startsWith("r")) {
return ROCK;
} else if (in.toLowerCase().startsWith("p")) {
return PAPER;
} else if (in.toLowerCase().startsWith("s")) {
return SCISSORS;
}
return null;
}
// Calculate the winner in a contest.
public Boolean wins(RPS in) {
if (this == in) { // Tie!
return null;
}
switch (this) {
case ROCK:
if (in == SCISSORS) { // Rock beats scissors.
return true;
}
return false;
case PAPER:
if (in == ROCK) { // Paper beats rock.
return true;
}
return false;
case SCISSORS:
if (in == PAPER) { // Scissors beats paper.
return true;
}
return false;
}
return null;
}
}
public static void main(String[] args) {
Random r = new Random(System.currentTimeMillis());
String[] choices = new String[] { "R", "P", "S" };
Scanner in = new Scanner(System.in);
while (in.hasNextLine()) {
System.out.println("Please enter (R)ock, (P)aper "
+ "or (S)cissors to play. (Q)uit.");
String c = in.nextLine();
c = c.trim();
if (c.toLowerCase().startsWith("q")) {
break;
}
RPS player = RPS.fromString(c);
RPS computer = RPS.fromString(choices[r
.nextInt(choices.length)]);
System.out.println("Computer picked " + computer);
if (player.wins(computer) == null) {
System.out.println("It's a Tie");
} else if (player.wins(computer)) {
System.out.println("You won");
} else {
System.out.println("The comptuer won");
}
}
}
好的,首先你的问题有误导性
但是 如果您使用
.equalsIgnoreCase()
这将允许您同时检查这两种情况,“P”和“P”表示纸张,因此纸张停止。您必须同时检查“P”和“P”。
if和else if在这里工作得更好,如下所示:
如果一条语句不正确,它将转到否则如果不正确,它将转到下一条语句查看哪个是正确的。
然后,else
表示,如果上述if
和else-if
均不正确,则必须使用此选项
if(compChoice.equalsIgnoreCase(playChoice)) //R R, R P, R S
{
winner = "nobody. There was a tie because you guessed the same thing.";
}
else if(compChoice.equals("R") && (playChoice.equalsIgnoreCase("P"))) //R P
{
winner = "player because Paper beats Rock.";
}
else if(compChoice.equals("R") && (playChoice.equalsIgnoreCase("S"))) //R S
{
winner = "computer because Rock beats Scissors.";
}
else if(compChoice.equals("P") && (playChoice.equalsIgnoreCase("R")))//P R
{
winner = "computer because Paper beats Rock.";
}
else if(compChoice.equals("P") && (playChoice.equalsIgnoreCase("S")))//P S
{
winner = "player because Scissors beats Paper.";
}
else if(compChoice.equals("S") && (playChoice.equalsIgnoreCase("R"))) //S R
{
winner = "player because Rock beats Scissors.";
}
else//S P
{
winner = "computer because Scissors beats Paper.";
}
return winner;
这是我能做到的最短、最有效的方法。这也行
public String determineWinner() {
Map<String, String> shortcutMap = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
Map<String, Map<String, Integer>> winMap = new TreeMap<String, Map<String, Integer>>(String.CASE_INSENSITIVE_ORDER);
shortcutMap.put("R", "Rock");
shortcutMap.put("P", "Paper");
shortcutMap.put("S", "Scissors");
winMap.put("R", new TreeMap<String, Integer>(String.CASE_INSENSITIVE_ORDER));
winMap.put("P", new TreeMap<String, Integer>(String.CASE_INSENSITIVE_ORDER));
winMap.put("S", new TreeMap<String, Integer>(String.CASE_INSENSITIVE_ORDER));
winMap.get("R").put("R", 0); // Rock draw against Rock,
winMap.get("R").put("P", -1); // Rock loose against Paper,
winMap.get("R").put("S", 1); // Rock win against Scissors,
winMap.get("P").put("R", 1); // Paper win against Rock,
winMap.get("P").put("P", 0); // Paper draw against Paper,
winMap.get("P").put("S", -1); // Paper loose against Scissors,
winMap.get("S").put("R", -1); // Scissors loose against Rock,
winMap.get("S").put("P", 1); // Scissors win against Paper,
winMap.get("S").put("S", 0); // Scissors draw against Scissors,
String winner = "yolo"; // if fail
Integer result = winMap.get(compChoice).get(playChoice);
if (result > 0) {
winner = "computer because " + shortcutMap.get(compChoice) + " beats " + shortcutMap.get(playChoice) + ".";
}
else if (result < 0) {
winner = "player because " + shortcutMap.get(playChoice) + " beats " + shortcutMap.get(compChoice) + ".";
}
else {
winner = "nobody. There was a tie because you guessed the same thing.";
}
return winner;
}
public String determineWinner(){
Map shortcutMap=newtreemap(String.CASE不区分顺序);
Map winMap=newtreemap(String.CASE不区分大小写顺序);
shortcutMap.put(“R”、“Rock”);
shortcutMap.put(“P”,“Paper”);
shortcutMap.put(“S”、“剪刀”);
put(“R”,新的树映射(String.CASE不区分大小写的顺序));
put(“P”,新的树映射(String.CASE不区分大小写的顺序));
put(“S”,新的树映射(String.CASE不区分大小写的顺序));
winMap.get(“R”).put(“R”,0);//岩石对岩石绘制,
winMap.get(“R”).put(“P”,-1);//在纸上晃动,
winMap.get(“R”).put(“S”,1);//石头战胜剪刀,
winMap.get(“P”).put(“R”,1);//纸面赢了摇滚,
winMap.get(“P”).put(“P”,0);//用纸画,
winMap.get(“P”).put(“S”,-1);//剪子上的纸松了,
winMap.get(“S”).put(“R”,-1);//剪刀在岩石上松开,
winMap.get(“S”).put(“P”,1);//剪刀赢了纸,
winMap.get(“S”).put(“S”,0);//剪刀拉剪刀,
字符串winner=“yolo”;//如果失败
整数结果=winMap.get(compChoice).get(playChoice);
如果(结果>0){
winner=“计算机因为”+shortcutMap.get(compChoice)+“击败”+shortcutMap.get(playChoice)+”;
}
否则如果(结果<0){
winner=“玩家因为”+shortcutMap.get(playChoice)+“击败”+shortcutMap.get(compChoice)+”;
}
否则{
winner=“没人。因为你猜的是同一件事,所以打成了平局。”;
}
返回赢家;
}
更具体一点,问题还不清楚。可能更适合使用“开关”而不是“如果”。使用enum
类型在这里也可能有用。你也可以使用equalsIgnoreCase来检查playChoice既不是“P”也不是“P”。不幸的是,我还没有学会enum,但正如他们所说的,这是重要的思想。谢谢。如果(compChoice.equalsIgnoreCase(playChoice))
总是平局。。。您可以添加一个if并删除六个case
,因此您可以通过删除许多case语句来改进此代码,例如case“S/P/R”
@Peter Lawrey我在哪里处理这些语句?@grexter89我会添加一条注释,说明您假设comp
将是大写,或者只是将其转换为大写。我的观点是,两个玩家应该被同等对待,你应该解释任何差异,因为这可能是一个bug的来源,否则。@Ben OP想要更少的空间而不是更多的空间。
public String determineWinner() {
Map<String, String> shortcutMap = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
Map<String, Map<String, Integer>> winMap = new TreeMap<String, Map<String, Integer>>(String.CASE_INSENSITIVE_ORDER);
shortcutMap.put("R", "Rock");
shortcutMap.put("P", "Paper");
shortcutMap.put("S", "Scissors");
winMap.put("R", new TreeMap<String, Integer>(String.CASE_INSENSITIVE_ORDER));
winMap.put("P", new TreeMap<String, Integer>(String.CASE_INSENSITIVE_ORDER));
winMap.put("S", new TreeMap<String, Integer>(String.CASE_INSENSITIVE_ORDER));
winMap.get("R").put("R", 0); // Rock draw against Rock,
winMap.get("R").put("P", -1); // Rock loose against Paper,
winMap.get("R").put("S", 1); // Rock win against Scissors,
winMap.get("P").put("R", 1); // Paper win against Rock,
winMap.get("P").put("P", 0); // Paper draw against Paper,
winMap.get("P").put("S", -1); // Paper loose against Scissors,
winMap.get("S").put("R", -1); // Scissors loose against Rock,
winMap.get("S").put("P", 1); // Scissors win against Paper,
winMap.get("S").put("S", 0); // Scissors draw against Scissors,
String winner = "yolo"; // if fail
Integer result = winMap.get(compChoice).get(playChoice);
if (result > 0) {
winner = "computer because " + shortcutMap.get(compChoice) + " beats " + shortcutMap.get(playChoice) + ".";
}
else if (result < 0) {
winner = "player because " + shortcutMap.get(playChoice) + " beats " + shortcutMap.get(compChoice) + ".";
}
else {
winner = "nobody. There was a tie because you guessed the same thing.";
}
return winner;
}