更好的算法?更好的阵列?
Better Algorithm? Better Array?
这是我的代码:
/* Name: Steven Royster
* Date: Jan. 15, 2015
* Rock, Paper, Scissors Program
* This program simulates a game of rock, paper, scissors with the user until someone has one a total of five times.
*/
import java.util.Random;
import java.util.Scanner;
public class RPS {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Let's play rock, paper, scissors! First to five wins!");
String[] choice = { "zero" , "rock" , "paper" , "scissors" };
Random rander = new Random();
Scanner input = new Scanner(System.in);
int userScore = 0, compScore = 0,
userChoice, compChoice;
while (compScore < 5 && userScore < 5)
{
compChoice = rander.nextInt(3) + 1;
System.out.println("\nEnter: 1 for ROCK | 2 for PAPER | 3 for SCISSORS.");
userChoice = input.nextInt();
if (compChoice == userChoice) // tie
{
System.out.println("I chose " + choice[compChoice] + " too, so we tied!");
}
else if ( ( compChoice == 1 && userChoice == 3 ) //computer wins
|| ( compChoice == 2 && userChoice == 1 )
|| ( compChoice == 3 && userChoice == 2) )
{
System.out.println("I win! I chose " + choice[compChoice] + ". " +
choice[compChoice] + " beats " + choice[userChoice] + "." );
compScore += 1;
}
else //human wins
{
System.out.println("You win! I chose " + choice[compChoice] + ". " +
choice[userChoice] + " beats " + choice[compChoice] + ".");
userScore += 1;
}
}//end while
if (userScore == 5)
{
System.out.println("\nCongrats! You're the winner! You got "
+ userScore + " points. I only got " + compScore + " points." );
}
}//end main
}//end class
有没有比在 else-if 语句中使用三个独立条件更好的算法我可以实现?另外,我的数组中没有 'zero' 字符串,我如何仅使用数字 1、2 和 3 检查我的数组?
只需使用
String[] choice = { "rock" , "paper" , "scissors" };
然后你可以 choice[userChoice - 1]
.
而不是 choice[userChoice]
你不需要写
if ( ( compChoice == 1 && userChoice == 3 )
|| ( compChoice == 2 && userChoice == 1 )
|| ( compChoice == 3 && userChoice == 2) )
因为和
一样
if (compChoice == 1 + (userChoice % 3))
这是我的代码:
/* Name: Steven Royster
* Date: Jan. 15, 2015
* Rock, Paper, Scissors Program
* This program simulates a game of rock, paper, scissors with the user until someone has one a total of five times.
*/
import java.util.Random;
import java.util.Scanner;
public class RPS {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Let's play rock, paper, scissors! First to five wins!");
String[] choice = { "zero" , "rock" , "paper" , "scissors" };
Random rander = new Random();
Scanner input = new Scanner(System.in);
int userScore = 0, compScore = 0,
userChoice, compChoice;
while (compScore < 5 && userScore < 5)
{
compChoice = rander.nextInt(3) + 1;
System.out.println("\nEnter: 1 for ROCK | 2 for PAPER | 3 for SCISSORS.");
userChoice = input.nextInt();
if (compChoice == userChoice) // tie
{
System.out.println("I chose " + choice[compChoice] + " too, so we tied!");
}
else if ( ( compChoice == 1 && userChoice == 3 ) //computer wins
|| ( compChoice == 2 && userChoice == 1 )
|| ( compChoice == 3 && userChoice == 2) )
{
System.out.println("I win! I chose " + choice[compChoice] + ". " +
choice[compChoice] + " beats " + choice[userChoice] + "." );
compScore += 1;
}
else //human wins
{
System.out.println("You win! I chose " + choice[compChoice] + ". " +
choice[userChoice] + " beats " + choice[compChoice] + ".");
userScore += 1;
}
}//end while
if (userScore == 5)
{
System.out.println("\nCongrats! You're the winner! You got "
+ userScore + " points. I only got " + compScore + " points." );
}
}//end main
}//end class
有没有比在 else-if 语句中使用三个独立条件更好的算法我可以实现?另外,我的数组中没有 'zero' 字符串,我如何仅使用数字 1、2 和 3 检查我的数组?
只需使用
String[] choice = { "rock" , "paper" , "scissors" };
然后你可以 choice[userChoice - 1]
.
choice[userChoice]
你不需要写
if ( ( compChoice == 1 && userChoice == 3 )
|| ( compChoice == 2 && userChoice == 1 )
|| ( compChoice == 3 && userChoice == 2) )
因为和
一样if (compChoice == 1 + (userChoice % 3))