Java “程序保持打印”;“继续玩”;而不是阅读代码
每次我运行我的程序,如果没有达到点限制,它应该要求用户继续播放,如果他们选择是,那么它应该返回循环并再次运行代码,但它没有这样做。当我输入“是”时,它只打印我当前拥有的点数,而不是返回循环Java “程序保持打印”;“继续玩”;而不是阅读代码,java,loops,Java,Loops,每次我运行我的程序,如果没有达到点限制,它应该要求用户继续播放,如果他们选择是,那么它应该返回循环并再次运行代码,但它没有这样做。当我输入“是”时,它只打印我当前拥有的点数,而不是返回循环 import java.util.*; public class Blackjack { private int points; private int limit; private Scanner scan; public Blackjack() { sc
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();
}
}
import java.util.*;
公营21点{
私人积分;
私有整数限制;
私人扫描仪扫描;
公共21点(){
扫描=新扫描仪(System.in);
}
/*用途:打印当前点和限制。
输入:点数和限制
输出:点数和限制
*/
公共空间显示点(){
System.out.println(“您的积分:“+积分+”/“+限制”);
}
/*目的:向用户询问游戏限制。
输入:游戏限制
输出:输入限制
*/
公共无效StartName(){
System.out.println(“输入点限制:”);
limit=scan.nextInt();
显示点();
}
/*目的:通过掷骰子获得掷骰值
输入:无
输出:随机骰子数
*/
公共int getRoll(){
随机r=新随机();
int roll=r.nextInt(6)+1;
System.out.println(“您滚动:“+滚动”);
回程辊;
}
/*目的:看玩家是否想继续玩游戏,
输入:是或否
输出:用户的响应。
*/
公共布尔askUser(布尔首次){
if(firstTime==true)
System.out.println(“开始播放?”);
否则{
System.out.println(“继续播放?”);
}
scan.next();
第一次回来;
}
/*目的:显示游戏中的分数结果。
输入:无输入。
输出:游戏中的点数。
*/
public void displayResult(){
如果(点数==限制)
System.out.println(“21点!”);
否则如果(点数>限制)
System.out.println(“半身像!”);
否则如果(点数<限制)
System.out.println(“站在“+点+点!”);
}
/*目的:玩所有的方法
输入:无。
输出:游戏结果。
*/
公共游戏{
布尔gameOver=false;
startGame();
askUser(真);
字符串响应=”;
int roll=getRoll();
while(response.equals(“yes”)&&gameOver==false)
getRoll();
点数=点数+掷骰;
显示点();
如果(点>=限制)
gameOver=true;
否则{
askUser(假);
}
displayResult();
}
公共静态void main(字符串[]args){
21点练习=新21点();
练习。玩();
}
}
用户键入时未收到响应
我认为您可以更改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);
;
;
}
用户键入时未收到响应 我认为您可以更改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);
;
;
}
我对你的代码做了一些小改动。试试这个:
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();
}
}
包栈;
导入java.util.*;
公营21点{
私人积分;
私有整数限制;
私人扫描仪扫描;
私有布尔值firstime=true;//新建
布尔gameOver;//新建
私有字符串应答;//新建
公共21点(){
扫描=新扫描仪(System.in);
}
/*用途:打印当前点和限制。
输入:点数和限制
输出:点数和限制
*/
公共空间显示点(){
System.out.println(“您的积分:“+积分+”/“+限制”);
}
/*目的:向用户询问游戏限制。
输入:游戏限制
输出:输入限制
*/
public void startName(){//更改
if(firstime==true){
System.out.println(“开始播放?”);
answer=scan.next();
}
开关(应答){
案例“是”:
System.out.println(“输入点限制:”);
limit=scan.nextInt();
int roll=getRoll();
点数=点数+掷骰;
displayResult();
打破
案例“否”:
再见();
}
}
//新方法
公众假期{
System.out.println(“再见!”);
scan.close();
}
/*目的:通过掷骰子获得掷骰值
输入:无
输出:随机骰子数
*/
公共int getRoll(){
随机r=新随机();
int roll=r.nextInt(6)+1;
System.out.println(“您滚动:“+滚动”);
回程辊;
}
/*目的:显示游戏中的分数结果。
输入:无输入。
输出:游戏中的点数。
*/
public void displayResult(){//更改
如果(点数==限制){
gameOver=true;
System.out.println(“21点!”);
显示点();
System.out.println(“继续播放?”);
answer=scan.next();
}
否则如果(点数>限制){
gameOver=true;
System.out.println(“半身像!”);
显示点();
System.out.println(“继续播放?”);
answer=scan.next();
}
否则如果(点数<限制){
gameOver=false;
System.out.println(“站在“+点+点!”);
}
}
/*目的:玩所有的方法
输入:无。
输出:游戏结果。
*/
public void play(){//更改
startGame();
ENDWHILE:while(gameOver==true | | gameOver==false){
如果(回答等于(“是”)){
第一次=错误;
while(gameOver==true){
分值=0;
startGame();
}
而(gameOver==false){
startGame();
}
}否则{