我试图提示用户他/她是否想再玩一次。我在编写将提示用户输入的代码时遇到问题。我从学期开始看我早期的编码,但那些没有使用多种方法。所以,当我尝试将其格式化为之前的格式但是它不起作用所以我把它拿出来了。任何人都可以帮我这个吗?
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package hwchallenge5;
import java.util.Random;
import java.util.Scanner;
/**
* @author Roberto Martinez
*/
public class HwChallenge5 {
/**
* Method will return null if an invalid choice is given.
* 1=rock, 2=paper, or 3=scissors.
*
* @param number
* @return string type
*/
public static String getChoice(int number) {
String choice;
switch (number) {
case 1:
choice = "rock";
break;
case 2:
choice = "paper";
break;
case 3:
choice = "scissors";
break;
default:
choice = null;
}
return choice;
}
/**
* Method should generate a random number and then return the
* computers choice.
*
* @return The computer's choice of "rock", "paper", or "scissors".
*/
public static String computerChoice() {
// Create a Random object.
Random rand = new Random();
// Generate a random number in the range of
// 1 through 3.
int num = rand.nextInt(3) + 1;
// Return the computer's choice.
return getChoice(num);
}
/**
* Method should return the user's choice.
*
* @param keyboard
* @return The user's choice of "rock", "paper", or "scissors".
*/
public static String userChoice(Scanner keyboard) {
// Ask the user for input
System.out.print("Enter 1 - rock, 2 - paper, or 3 - scissors: ");
int userChoice = keyboard.nextInt();
String play = getChoice(userChoice);
// Validate the choice.
while (play == null) {
System.out.print("Enter 1 - rock, 2 - paper, or 3 - scissors: ");
userChoice = keyboard.nextInt();
play = getChoice(userChoice);
}
// Return the user's choice.
return play;
}
/**
* The determineWinner method returns the output based on parameters
*
* @param computerChoice The computer's choice.
* @param userChoice The user's choice.
*/
public static String determineWinner(String computerChoice, String userChoice) {
String output;
output = "The computer's choice was " + computerChoice + ".\n";
output += "The user's choice was " + userChoice + ".\n\n";
// check logic
if (userChoice.equalsIgnoreCase("rock")) {
if (computerChoice.equalsIgnoreCase("scissors"))
output += "Rock beats scissors.\nThe user wins!";
else if (computerChoice.equalsIgnoreCase("paper"))
output += "Paper beats rock.\nThe computer wins!";
else
output += "The game is tied!\nPlay again...";
} else if (userChoice.equalsIgnoreCase("paper")) {
if (computerChoice.equalsIgnoreCase("scissors"))
output += "Scissors beats paper.\nThe computer wins!";
else if (computerChoice.equalsIgnoreCase("rock"))
output += "Paper beats rock.\nThe user wins!";
else
output += "The game is tied!\nPlay again...";
} else if (userChoice.equalsIgnoreCase("scissors")) {
if (computerChoice.equalsIgnoreCase("rock"))
output += "Rock beats scissors.\nThe computer wins!";
else if (computerChoice.equalsIgnoreCase("paper"))
output += "Scissors beats paper.\nThe user wins!";
else
output += "The game is tied!";
}
return output;
}
public static void main(String[] args) {
String computer = null;
String user = null;
Scanner keyboard = new Scanner(System.in);
char playAgain=0;
// Play the game as long as there is a tie.
do {
while (user.equalsIgnoreCase(computer));
// Get the computer's choice.
computer = computerChoice();
// Get the user's choice.
user = userChoice(keyboard);
// Determine the winner.
String output = determineWinner(computer, user);
System.out.println(output);
// Ask the user if he wants to play again
System.out.println("Do you want to play again?");
// Prompt the user for a yes or no
System.out.print("Enter Y for yes or N for no: ");
String input = keyboard.nextLine();
// Indentifys the first letter of of user's answer
playAgain = input.charAt(0);
// Allows the user to input upper or lower case Char as a respond
} while(playAgain =='Y' || playAgain== 'y');
}
}
答案 0 :(得分:0)
你非常接近,首先,你希望将你的while (user.equalsIgnoreCase(computer));
移到你拥有while(playAgain =='Y' || playAgain== 'y');
的地方,否则先将NullPointerException
投掷public static boolean PlayAgain(Scanner keyboard) {
//Ask user if they want to play again
//return true if yes else return false
}
并阻止你的程序来自编译(你可以完全删除它,如果他们希望他们能够在没有比赛的情况下退出而退出)。
然后,您可以创建一个功能来检查用户是否想再次播放,例如:
do {...} while()
然后您可以将此功能与do {
//current code
//get user choice
//get computer choice
//determine winner
} while (user.equalsIgnoreCase(computer) || PlayAgain(keyboard));
结合使用,如下所示:
true
如果第一个条件是class UserSerializer(serializers.ModelSerializer):
# source is required to show where this field is coming from
phone_number = serializers.CharField(source='userprofile.phone_number')
class Meta:
model = User
# Add phone_number to the fields
fields = ('id', 'username', 'first_name', 'last_name', 'password', 'phone_number', )
read_only_fields = ('phone_number', )
extra_kwargs = {'password': {'write_only': True}}
,则永远不会检查第二个条件,这意味着除非赢或输,否则函数不会被调用。