我正在尝试为我的学校项目制作赌场计划。除了在控制台的第一,第三,第五(奇数)行总是跳过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,我的变量下注将保持相同的每个循环,因为它在此之前已经启动。
修改:备用main()
方法
Edit2:添加更多样本运行
答案 0 :(得分:0)
如果您希望您的投注变量保持不变,您可以将while循环更改为:
while(true){
//get the bet here
if(bet == 0){
break;
}
//Do the rest of your stuff here.
}
这将改变每次迭代的下注,但如果它为零则仍然停止运行。
答案 1 :(得分:0)
您的第二个主要内容不起作用,因为您没有重新分配bet
变量。
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");
}
由于您拨打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");
}