我已经对Java感兴趣了大约2个月,因为我已经在线完成了一些课程,我正在尝试为Rock Paper Scissors创建一个游戏。我希望游戏能够使用public static void toastAnywhere(final String text) {
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
public void run() {
Toast.makeText(SuperApplication.getInstance().getApplicationContext(), text,
Toast.LENGTH_LONG).show();
}
});
}
循环运行5次,尽管我在循环时遇到问题;它一直宣称赢家而不是重复游戏?
for
答案 0 :(得分:0)
您的switch语句超出了循环执行的代码,将其移动到行player2Input = userInput2.charAt(0);
后面的点,它必须保留在循环括号内。
答案 1 :(得分:0)
switch语句超出范围,这意味着程序不知道在哪里查找它,并且对于所有意图和目的,它对程序和循环是不可见的。如果将for循环后的括号移到switch语句之后,它应该运行得很好。
答案 2 :(得分:0)
您编写的switch语句仅评估播放器的最后一个输入,因此需要在for循环中移动。
此外,在声明变量时初始化变量总是好的。
import java.util.Scanner;
public class Part2
{
public static void main (String[]args)
{
Scanner scan = new Scanner(System.in);
String userInput1 = "";
char player1Input = ' ';
String userInput2 = "";
char player2Input = ' ';
for(int i = 0; i < 5; i++) //Loop for game
{
System.out.println("Player 1: Please Enter e.g R for Rock:");
System.out.println("R.Rock");
System.out.println("P.Paper");
System.out.println("S.Scissors");
userInput1 = scan.next();
player1Input = userInput1.charAt(0);
System.out.println("Player 2: Please Enter e.g S for Scissors");
System.out.println("R.Rock");
System.out.println("P.Paper");
System.out.println("S.Scissors");
userInput2 = scan.next();
player2Input = userInput2.charAt(0);
switch(player1Input)
{
case 'R': System.out.println ("Player 1:Rock");
switch(player2Input)
{
case'R' :
System.out.println("Player 2:Rock");
System.out.println("The Game Result is Draw");
break;
case'P' :
System.out.println("Player 2:Paper");
System.out.println("The Game Result is: Player 2 Wins");
break;
case'S' :
System.out.println("Player 2:Scissors");
System.out.println("The Game Result is: Player 1 Wins");
break;
default:
System.out.println("Invalid Option");
break;
}
break;
case 'P' : System.out.println ("Player 1:Paper");
switch(player2Input)
{
case'R' :
System.out.println("Player 2:Rock");
System.out.println("The game result is: Player 1 Wins");
break;
case'P' :
System.out.println("Player 2:Paper");
System.out.println("The game result is: Draw");
break;
case'S' :
System.out.println("Player 2:Scissors");
System.out.println("The Game Result is: Player 2 Wins2");
break;
default:
System.out.println("Invalid Option");
break;
}
break;
case 'S' :
System.out.println ("Player 1:Scissors");
switch(player2Input)
{
case'R' :
System.out.println("Player 2:Rock");
System.out.println("The Game Result is: Player 2 Wins");
break;
case'P' :
System.out.println("Player 2:Paper");
System.out.println("The game result is: Player 1 Wins");
break;
case'S' :
System.out.println("Player 2:Scissors");
System.out.println ("The Game Result is: Draw");
break;
default:
System.out.println("Invalid Option");
break;
}
break;
}
}
}
}