gpt4 book ai didi

java - 循环正在跳过语句(赌场程序)

转载 作者:太空宇宙 更新时间:2023-11-04 13:14:12 24 4
gpt4 key购买 nike

我正在尝试为我的学校项目制作一个赌场程序。一切似乎都工作得很好,除了 main() 方法内的 do-while 循环语句总是在控制台的第一、第三、第五(奇数)行被跳过。

计划:

import java.util.Scanner;
import java.text.*;
import java.util.*;

public class Casino
{
public static Scanner input;
static final String SEPARATOR = "\n";

public static void main (String[] args) throws Exception
{
int winnings;

while (getBet() != 0)
{
TripleString thePull = pull();
getPayMultiplier(thePull);
winnings = getPayMultiplier(thePull) * getBet();
display(thePull, winnings);
}
System.out.println("Thanks");
}

//gets bet, stores in static class variables
public static int getBet()
{
final double MAX_BET = 50;
String prompt, strUserResponse;
int intResponse;
input = new Scanner(System.in);

do
{
prompt = "How much would you like to bet ( Min $1 - Max $50 ) "
+ "or press '0' to quit?";
System.out.print(prompt);
strUserResponse = input.nextLine();
intResponse = Integer.parseInt(strUserResponse);
}
while( intResponse < 0 || intResponse > MAX_BET );

return intResponse;
}

public static String randString()
{
int bar = 38;
int cherries = 78;
int space = 85;
int seven = 100;

String randomString = "";
int randomNum = 1 + (int)(Math.random() * 100);

if (randomNum <= bar)
randomString = "BAR";
else if (randomNum <= cherries)
randomString = "cherries";
else if (randomNum <= space)
randomString = "space";
else if (randomNum <= seven)
randomString = "7";
return randomString;
}

public static TripleString pull()
{
TripleString pullString = new TripleString();

String str1 = randString();
pullString.setString1(str1);

String str2 = randString();
pullString.setString2(str2);

String str3 = randString();
pullString.setString3(str3);

return pullString;
}

public static int getPayMultiplier (TripleString thePull)
{

if (thePull.getString1() == "cherries" &&
thePull.getString2() != "cherries" )
return 5;
else if (thePull.getString1() == "cherries" &&
thePull.getString2() == "cherries" &&
thePull.getString3() != "cherries")
return 15;
else if (thePull.getString1() == "cherries" &&
thePull.getString2() == "cherries" &&
thePull.getString3() == "cherries")
return 30;
else if (thePull.getString1() == "BAR" &&
thePull.getString2() == "BAR" &&
thePull.getString3() == "BAR")
return 50;
else if (thePull.getString1() == "7" &&
thePull.getString2() == "7" &&
thePull.getString3() == "7")
return 100;
else
return 0;
}

public static void display (TripleString thePull, int winnings)
{
System.out.println(SEPARATOR + ">>>Brrrrrr! Your Pull Is . . .<<<"
+ SEPARATOR + thePull.toString());
if ( winnings == 0)
System.out.println("Sorry you lose. . . GOOD LUCK NEXT TIME!"
+ SEPARATOR);
else
System.out.println("Congaratulations, you win =" + " $" + winnings
+ " !" + SEPARATOR + "YEAY !!! :):):)" + SEPARATOR);
}
}

class TripleString
{
//member data
private String string1, string2, string3;

//static constants
public static final double MIN_LEN = 1;
public static final double MAX_LEN = 50;
public static final String DEFAULT_STRING = "undefined";

//default constructor
TripleString ()
{
string1 = DEFAULT_STRING;
string2 = DEFAULT_STRING;
string3 = DEFAULT_STRING;
}

//parameter-taking constructor
TripleString (String str1, String str2, String str3)
{
if (! setString1(str1))
str1 = DEFAULT_STRING;
if (! setString2(str2))
str2 = DEFAULT_STRING;
if (! setString3(str3))
str3 = DEFAULT_STRING;
}

//private static helper method
private boolean validString(String str)
{
if (str == null || str.length() < MIN_LEN || str.length() > MAX_LEN)
return false;
else
return true;
}

//accessor set methods
public boolean setString1 (String stringName1)
{
if ( !validString(stringName1) )
return false;
string1 = stringName1;
return true;
}

public boolean setString2 (String stringName2)
{
if ( !validString(stringName2) )
return false;
string2 = stringName2;
return true;
}

public boolean setString3 (String stringName3)
{
if ( !validString(stringName3) )
return false;
string3 = stringName3;
return true;
}

//accessor get methods
public String getString1 () { return string1; }
public String getString2 () { return string2; }
public String getString3 () { return string3; }

public String toString ()
{
String reStr;

reStr = string1 + " "+ string2 + " " + string3;
return reStr;
}
}

这是我的运行示例:

How much would you like to bet ( Min $1 - Max $50 ) or press '0' to quit?1
How much would you like to bet ( Min $1 - Max $50 ) or press '0' to quit?6

//Brrrrr below supposed to have ">>>" "<<<" but I remove it manually in this example since it creates blockquotes

Brrrrrr! Your Pull Is . . .
cherries cherries BAR
Congaratulations, you win = $90 !
YEAY !!! :):):)

How much would you like to bet ( Min $1 - Max $50 ) or press '0' to quit?7
How much would you like to bet ( Min $1 - Max $50 ) or press '0' to quit?7

Brrrrrr! Your Pull Is . . .
7 BAR cherries
Sorry you lose. . . GOOD LUCK NEXT TIME!

我想让它看起来更像

How much would you like to bet ( Min $1 - Max $50 ) or press '0' to quit?1

Brrrrrr! Your Pull Is . . .<<<
BAR BAR BAR
Congaratulations, you win = $50 !
YEAY !!! :):):)

How much would you like to bet ( Min $1 - Max $50 ) or press '0' to quit?2

Brrrrrr! Your Pull Is . . .<<<
BAR cherries BAR
Sorry you lose. . . GOOD LUCK NEXT TIME!

How much would you like to bet ( Min $1 - Max $50 ) or press '0' to quit?0

我认为问题必须与我的 getInput() 方法中的循环有关,但我真的不确定为什么。我知道我无法在 getInput() 方法中进行循环,但我的讲师指定该方法必须循环,直到用户输入有效的 #(1-50)

我尝试将其更改为标准 while 循环或以许多其他方式修改代码,但新的方式会带来新的问题。例如,如果我将 main 方法更改为

备用main()

public static void main (String[] args) throws Exception
{
int bet = getBet(), winnings;

do
{
TripleString thePull = pull();
getPayMultiplier(thePull);
winnings = getPayMultiplier(thePull) * bet;
display(thePull, winnings);
}
while (getBet() != 0);


System.out.println("Thanks");

}

如果我将上述代码用于主程序,则每个循环的变量下注将保持相同,因为它在此之前已启动。

编辑:替代的 main() 方法Edit2:添加更多示例运行

最佳答案

您的第二个 main 不起作用,因为您没有重新分配 bet 变量。

工作简单的替代 main()

   public static void main (String[] args) throws Exception
{
int bet = getBet(), winnings;

do
{
TripleString thePull = pull();
getPayMultiplier(thePull);
winnings = getPayMultiplier(thePull) * bet;
display(thePull, winnings);
bet = getBet();
}
while ( bet != 0);

System.out.println("Thanks");
}

您的第一个 main 不起作用,因为您调用了 getBet() 两次

工作主要

 public static void main (String[] args) throws Exception
{
int winnings;
int bet;
while ((bet = getBet()) != 0)
{
TripleString thePull = pull();
getPayMultiplier(thePull);
winnings = getPayMultiplier(thePull) * bet;
display(thePull, winnings);
}
System.out.println("Thanks");
}

关于java - 循环正在跳过语句(赌场程序),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33692802/

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