每次运行程序时,如果未达到积分限制,则要求用户继续播放,如果他们选择是,则应返回循环并再次运行代码,但是没有这样做。当我输入“是”时,它只会打印出我当前拥有的点数,而不是返回到循环。
import java.util.*;
public class Blackjack {
private int points;
private int limit;
private Scanner scan;
public Blackjack() {
scan = new Scanner(System.in);
}
/*Purpose: To print the current points and the limit.
Input: Points and Limit
Output: Points and Limit
*/
public void displayPoints() {
System.out.println("Your points:" + points + "/" + limit);
}
/*Purpose: To ask the user for the game limit.
Input: Game limit
Output: Entered limit
*/
public void startGame() {
System.out.println("Enter point limit:");
limit = scan.nextInt();
displayPoints();
}
/*Purpose: To get the roll value from rolling a dice
Input: Nothing
Output: Random dice number
*/
public int getRoll() {
Random r = new Random();
int roll = r.nextInt(6) + 1;
System.out.println("You rolled:" + roll);
return roll;
}
/*Purpose: To see if the player wants to keep playing the game,
Input: yes or no
Output: User's response.
*/
public boolean askUser(boolean firstTime) {
if (firstTime == true)
System.out.println("Start Playing?");
else {
System.out.println("Keep playing?");
}
scan.next();
return firstTime;
}
/* Purpose: to display the result of points in the game.
Input: No input.
Output: amount of points in the game.
*/
public void displayResult() {
if (points == limit)
System.out.println("BlackJack!");
else if (points > limit)
System.out.println("Bust!");
else if (points < limit)
System.out.println("Stand at " + points + " points!");
}
/*Purpose: to play all methods
Input: none.
Output: Game results.
*/
public void play() {
boolean gameOver = false;
startGame();
askUser(true);
String response = "";
int roll = getRoll();
while (response.equals("yes") && gameOver == false)
getRoll();
points = points + roll;
displayPoints();
if (points >= limit)
gameOver = true;
else {
askUser(false);
}
displayResult();
}
public static void main(String[] args) {
Blackjack practice = new Blackjack();
practice.play();
}
}
答案 0 :(得分:0)
用户键入时没有得到响应。
我认为您可以像下面的代码一样更改askUser方法。
public String askUser(boolean firstTime) {
if (firstTime == true)
System.out.println("Start Playing?");
else {
System.out.println("Keep playing?");
}
String response = scan.next();
return response;
}
然后更改播放方法,例如:
public void play() {
boolean gameOver = false;
startGame();
String response = askUser(true);
;
;
}
答案 1 :(得分:0)
我对您的代码做了一些小的更改。试试这个:
package stack;
import java.util.*;
public class Blackjack{
private int points;
private int limit;
private Scanner scan;
private boolean firstime = true; //new
boolean gameOver; //new
private String answer;//new
public Blackjack(){
scan = new Scanner(System.in);
}
/*Purpose: To print the current points and the limit.
Input: Points and Limit
Output: Points and Limit
*/
public void displayPoints(){
System.out.println("Your points:" + points+"/"+limit);
}
/*Purpose: To ask the user for the game limit.
Input: Game limit
Output: Entered limit
*/
public void startGame(){//Changes
if(firstime == true) {
System.out.println("Start Playing?");
answer = scan.next();
}
switch(answer) {
case "yes":
System.out.println("Enter point limit:");
limit = scan.nextInt();
int roll = getRoll();
points = points + roll;
displayResult();
break;
case "no":
goodBye();
}
}
//New method
public void goodBye() {
System.out.println("Goodbye!");
scan.close();
}
/*Purpose: To get the roll value from rolling a dice
Input: Nothing
Output: Random dice number
*/
public int getRoll(){
Random r = new Random();
int roll = r.nextInt(6)+1;
System.out.println("You rolled:" + roll);
return roll;
}
/* Purpose: to display the result of points in the game.
Input: No input.
Output: amount of points in the game.
*/
public void displayResult(){//Changes
if(points == limit) {
gameOver = true;
System.out.println("BlackJack!");
displayPoints();
System.out.println("Keep playing?");
answer = scan.next();
}
else if (points > limit) {
gameOver = true;
System.out.println("Bust!");
displayPoints();
System.out.println("Keep playing?");
answer = scan.next();
}
else if (points < limit) {
gameOver = false;
System.out.println("Stand at " + points + " points!");
}
}
/*Purpose: to play all methods
Input: none.
Output: Game results.
*/
public void play(){//Changes
startGame();
ENDWHILE:while(gameOver == true || gameOver == false) {
if(answer.equals("yes")) {
firstime = false;
while(gameOver == true) {
points = 0;
startGame();
}
while(gameOver == false) {
startGame();
}
}else {
break ENDWHILE;
}
}
goodBye();
}
public static void main(String [] args){
Blackjack practice = new Blackjack();
practice.play();
}
}
答案 2 :(得分:0)
这个正在工作..您在代码中注释了2个小错误。需要在{}中包装while循环内容。并且用户输入需要在askUser函数中作为字符串返回。以下代码可根据需要运行。
package javaapplication1;
import java.util.*;
public class JavaApplication1 {
private int points;
private int limit;
private Scanner scan;
public JavaApplication1(){
scan = new Scanner(System.in);
}
public void displayPoints(){
System.out.println("Your points:" + points+"/"+limit);
}
/*Purpose: To ask the user for the game limit.
Input: Game limit
Output: Entered limit
*/
public void startGame(){
System.out.println("Enter point limit:");
limit = scan.nextInt();
displayPoints();
}
public int getRoll(){
Random r = new Random();
int roll = r.nextInt(6)+1;
System.out.println("You rolled:" + roll);
return roll;
}
public String askUser(boolean firstTime){
if(firstTime == true)
System.out.println("Start Playing?");
else {
System.out.println("Keep playing?");
}
return scan.next();
}
public void displayResult(){
if(points == limit)
System.out.println("BlackJack!");
else if (points > limit)
System.out.println("Bust!");
else if (points < limit)
System.out.println("Stand at " + points + " points!");
}
public void play(){
boolean gameOver = false;
startGame();
String resUser = askUser(true); // add a variable to catch the input //exp-"yes"
String response = "";
int roll = getRoll();
while(resUser.equals("yes")&& gameOver == false){ // you forget the { and use //the variable to check if it is yess or not
getRoll();
points = points + roll;
displayPoints();
if(points >= limit)
gameOver =true;
else{
resUser = askUser(false);//catching the new user input
}
}// you forget the }. if you are not wrapping the loop with {}, it will only //execute one line after the loop
displayResult();
}
public static void main(String [] args){
JavaApplication1 practice = new JavaApplication1();
practice.play();
}
}