gpt4 book ai didi

java - 如何返回我的 getWin 并不仅仅是 true 或 false

转载 作者:行者123 更新时间:2023-12-01 23:54:07 26 4
gpt4 key购买 nike

我试图让我的程序采用多个不同的返回语句,这样我就可以有一个胜利分割和失败,而不仅仅是胜利或失败,而且似乎不知道如何做到这一点。我的程序是为了让你赢或者让你的赌注加倍而设计的。如果打平了,你就可以拿回你的钱。如果你输了,你就输了你的赌注。我之前曾通过使用 boolean 值并返回 true 或 false 来获得胜利和失败,但需要一种方法来添加进行分割的方法。

我已经尝试了网上看到的多种方法,但都没有效果,我们将不胜感激。

//Import Random Number Generator
import java.util.Random;

class BlackJackPlayer{
//Keep the data secure by using private
private String hand;
private int sum;
private int numAces;
private static Random gen = new Random();
private String Win;
private String Lose;
private String Split;

private final int ACE = 1;
private final int JACK = 11;
private final int QUEEN = 12;
private final int KING = 13;

//constructor
public BlackJackPlayer(){
hand = "";
sum = 0;
numAces = 0;
}

//Getter for hand variable
public String getHand(){
return hand;
}

public String setHand(){
hand = " ";
return hand;
}

//Getter for sum variable
public int getSum(){
return sum;
}

public void hit(){
//local variable
int currentCard = gen.nextInt(13) + 1;

if(currentCard > ACE && currentCard < JACK){
sum += currentCard;
hand += currentCard + " ";
}
else if(currentCard == ACE){
sum += 11;
numAces++;
hand += "A ";
}

else if(currentCard == QUEEN){
sum += 10;
hand += "Q ";
}

else if(currentCard == QUEEN){
sum += 10;
hand += "Q ";
}

else if(currentCard == KING){
sum += 10;
hand += "K ";
}//Ends Else If

//Is Ace 1 or 11
if(sum > 21 && numAces > 0){
numAces--;
sum -= 10;
}

}//ENDS HIT

public String getWin(BlackJackPlayer other) {
if(sum > 21){
Win = "Win";
}
else if(sum < other.getSum()){
Lose = "Lose";
}
else if(sum == other.getSum()){
Split = "Split";

}
return Win;
}


}//end main



import java.util.Scanner;
class BlackJackGame{
public static void main(String args[]){
BlackJackPlayer you = new BlackJackPlayer();
BlackJackPlayer enemy = new BlackJackPlayer();
int chips = 100;
int bet;
int winnings;
int multiply = 2;
String qORc;
Scanner in = new Scanner(System.in);

String choice;
do{
you.setHand();
enemy.setHand();
you.hit();
enemy.hit();
enemy.hit();
System.out.println("Chips: " + chips);
System.out.println("How much do you want to bet");
bet = in.nextInt();
chips -= bet;
System.out.println("You bet: " + bet + ", You now have " + chips + " chips");
do{
System.out.println("Your Cards: " + you.getHand());
System.out.println("(H)it or (S)tand");
choice = in.next();
if(choice.equalsIgnoreCase("h")){
you.hit();
}
}while(choice.equalsIgnoreCase("h"));

while(enemy.getSum() < 16){
enemy.hit();
}
System.out.println(you.getWin());
if(you.getWin()){
System.out.println("You Win");
System.out.println(enemy.getHand());
winnings = bet * multiply;
chips += winnings;
System.out.println("You now have: " + chips + " chips!");
}
else{
System.out.println("You Lose");
System.out.println(enemy.getHand());
System.out.println("You now have: " + chips + " chips.");
}
System.out.println("(C)ontinue or (Q)uit");
qORc = in.next();
}while(chips > 0 && qORc.equalsIgnoreCase("c"));

}//end main

}//end class

我希望能够得到不同的返回语句,这样我实际上可以将胜利方面设置为失败方面,但如果两个玩家平局,也可以设置 split 方面。

最佳答案

您需要返回三个结果,分别是:获胜、失败和平局,对吗?

在这种情况下,您必须使用三个不同的变量,例如,

public String Win = "Win",Lose = "Lose",Split = "Split"; 

并使用其他变量来存储结果,例如result,并在getWin()中返回该结果。参见下面的代码:

import java.util.Random;
import java.util.Scanner;
class BlackJackPlayer{
//Keep the data secure by using private
private String hand;
private int sum;
private int numAces;
private static Random gen = new Random();
public String Win = "Win",Lose = "Lose",Split = "Split";
private String result = "";
private final int ACE = 1;
private final int JACK = 11;
private final int QUEEN = 12;
private final int KING = 13;

//constructor
public BlackJackPlayer(){
hand = "";
sum = 0;
numAces = 0;
}

//Getter for hand variable
public String getHand(){
return hand;
}

public String setHand(){
hand = " ";
return hand;
}

//Getter for sum variable
public int getSum(){
return sum;
}

public void hit(){
//local variable
int currentCard = gen.nextInt(13) + 1;

if(currentCard > ACE && currentCard < JACK){
sum += currentCard;
hand += currentCard + " ";
}
else if(currentCard == ACE){
sum += 11;
numAces++;
hand += "A ";
}

else if(currentCard == QUEEN){
sum += 10;
hand += "Q ";
}

else if(currentCard == QUEEN){
sum += 10;
hand += "Q ";
}

else if(currentCard == KING){
sum += 10;
hand += "K ";
}//Ends Else If

//Is Ace 1 or 11
if(sum > 21 && numAces > 0){
numAces--;
sum -= 10;
}

}//ENDS HIT

public String getWin(BlackJackPlayer other) {
if(sum > 21){
result = Win;
}
else if(sum < other.getSum()){
result = Lose;
}
else if(sum == other.getSum()){
result = Split;
}
return result;
}
}//end main

class BlackJackGame{
public static void main(String args[]){
BlackJackPlayer you = new BlackJackPlayer();
BlackJackPlayer enemy = new BlackJackPlayer();
int chips = 100,bet,winnings,multiply = 2;
String qORc;
Scanner in = new Scanner(System.in);

String choice;
do{
you.setHand();
enemy.setHand();
you.hit();
enemy.hit();
enemy.hit();
System.out.println("Chips: " + chips);
System.out.println("How much do you want to bet");
bet = in.nextInt();
chips -= bet;
System.out.println("You bet: " + bet + ", You now have " + chips + " chips");
do{
System.out.println("Your Cards: " + you.getHand());
System.out.println("(H)it or (S)tand");
choice = in.next();
if(choice.equalsIgnoreCase("h")){
you.hit();
}
}while(choice.equalsIgnoreCase("h"));

while(enemy.getSum() < 16){
enemy.hit();
}
String result = you.getWin(enemy);
System.out.println(result);
if(result == you.Win){
System.out.println("You Win");
System.out.println(enemy.getHand());
winnings = bet * multiply;
chips += winnings;
System.out.println("You now have: " + chips + " chips!");
}
else if (result == you.Lose){
System.out.println("You Lose");
System.out.println(enemy.getHand());
System.out.println("You now have: " + chips + " chips.");
}else{
System.out.println("You Split");
}
System.out.println("(C)ontinue or (Q)uit");
qORc = in.next();
}while(chips > 0 && qORc.equalsIgnoreCase("c"));

}//end main

}

关于java - 如何返回我的 getWin 并不仅仅是 true 或 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58211786/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com