我陷入模數計算以及此遊戲的條件。有什麼建議麼?java中使用模量的岩石紙剪刀
{
public static void main(String[] args)
{
int x, comp, result;
x = 0;
String user;
String computer_choice = null; //declare variables for each value
// generate computer number
comp = (int) (3 * Math.random()) + 1 ;
System.out.println (comp);
//convert# to a value
if (comp == 0)
{
computer_choice = "Rock";
}
else if (comp == 1)
{
computer_choice = "Paper";
}
else if (comp == 2)
{
computer_choice = "Scissor";
}
// print out the message
System.out.println ("Please select one of [R/P/S]: ");
Scanner keyboard = new Scanner (System.in);
user = keyboard.nextLine();
if (user.equals ("R")|| user.equals("r"))
{
user = "Rock";
x = 0; // rock
}
else if (user.equals ("P")|| user.equals("p"))
{
user = "Paper";
x = 1;//paper
}
else if (user.equals ("S")|| user.equals("s"))
{
user = "Scissor";
x = 2;
}
else
{ System.out.println ("Please enter a valid values");
}
System.out.println ("You chose: " + user);
//print out the computer choice
System.out.println ("I chose\t" + computer_choice);
result = (comp - x) % 3 ;
System.out.println (result);
if (result == 0)// if the game is tie
{
System.out.println ("A Tie!") ;
}
else if (result == 1 || result == 2)
{
System.out.println ("comp win");
}
else
{
System.out.println ("you win");
}
}
}
正確縮進您的代碼。 – Tirath 2014-10-18 05:44:53
你爲什麼使用mod?爲什麼不使用,如果其他? – Edwin 2014-10-18 05:51:43