我正在制作一个Craps java程序而且我在添加&#34时遇到了一些问题;你想再玩一次吗?#34;最后的声明。如果你能帮助我,我将不胜感激!此外,我必须计算用户赢/输多少游戏的计数器不能正常工作。如果你看到问题,请告诉我!
import java.util.Scanner;
public class Lab5 {
static int dice2;
public static void main(String[] args) {
//variables
int dice1;
int dice2;
int numWins = 0;
int numLosses = 0;
//Call the welcome method
welcome();
//fetch random numbers
/*
* **************************************************************
*welcome method
*welcome user
*no parameters
*no return
****************************************************************
*/
}
public static void welcome() {
System.out.println("Welcome to a Lucky (for me) Dice Game! \nFEELING LUCKY?!? Hope you brought lots of CASH!");{
}
int die1 = (int) (Math.random()*6 + 1);
int die2 = (int) (Math.random()*6 + 1);
int dice = die1 + die2;
System.out.println("Roll: total = " + dice);
int numWins = 0;
int numLosses = 0;
if (dice == 7 || dice == 11){
System.out.println("Woah!!! With a: "+dice+ " You WIN!!!!!!!!");
numWins++;
}
else if (dice == 2 || dice == 3 || dice == 12){
System.out.println("Sorry, with a "+dice+" You lose:(");
numLosses++;
}
while (dice != 0){
int die3 = (int) (Math.random()*6 + 1);
int die4 = (int) (Math.random()*6 + 1);
int dice2 = die3 + die4;
System.out.println("Roll: total = "+dice2);
if (dice2 == 2|| dice2 == 3 || dice2 == 12){
System.out.println("Sorry, with a "+dice2+" You lose:(");
numLosses++;
dice = 0;
}
else if (dice2 == 7 || dice2 == 11){
System.out.println("Woah!!! With a: "+dice2+ " You WIN!!!!!!!!");
numWins++;
dice = 0;
}
{
System.out.println("So far you have won " + numWins +
" times and lost " + numLosses + " times, ");
{
}
}
}
}}
这是我运行时的输出:
Welcome to a Lucky (for me) Dice Game!
FEELING LUCKY?!? Hope you brought lots of CASH!
Roll: total = 2
Sorry, with a 2 You lose:(
Roll: total = 8
So far you have won 0 times and lost 1 times,
Roll: total = 10
So far you have won 0 times and lost 1 times,
Roll: total = 8
So far you have won 0 times and lost 1 times,
Roll: total = 3
Sorry, with a 3 You lose:(
So far you have won 0 times and lost 2 times,
只有胜负后才能说出反击。我该如何解决这个问题?
答案 0 :(得分:0)
要重复某些内容,请使用循环,例如while循环或do-while循环,如果您不确定循环的manny时间。要获取用户输入,请使用已导入的Scanner对象。 do-while循环结构将类似于......
do {
// code to do inside of the loop
} while (somethingIsTrue);
您需要使用一些sentinel变量来更改循环,然后在while布尔检查内部进行测试。它可以是一个String,在这种情况下,你可以在while的布尔检查中使用String equals(...)
或equalsIgnoreCase(...)
方法。
因此,请考虑使用System.out.print(...)
在do代码块的末尾提示输入,使用Scanner获取输入,然后在while布尔测试中测试该输入。
答案 1 :(得分:-1)
import java.util.NoSuchElementException;
import java.util.Scanner;
public class Lab5
{
static int dice2;
public static void main(String[] args)
{
//variables
int dice1;
int dice2;
int numWins = 0;
int numLosses = 0;
//Call the welcome method
//welcome();
//fetch random numbers
/*
* **************************************************************
*welcome method
*welcome user
*no parameters
*no return
****************************************************************
*/
while(true)
{
welcome();
}
}
public static void welcome()
{
System.out.println("Welcome to a Lucky (for me) Dice Game! \nFEELING LUCKY?!? Hope you brought lots of CASH!");
int die1 = (int) (Math.random()*6 + 1);
int die2 = (int) (Math.random()*6 + 1);
int dice = die1 + die2;
System.out.println("Roll: total = " + dice);
int numWins = 0;
int numLosses = 0;
if (dice == 7 || dice == 11)
{
System.out.println("Woah!!! With a: "+dice+ " You WIN!!!!!!!!");
numWins++;
}
else if (dice == 2 || dice == 3 || dice == 12)
{
System.out.println("Sorry, with a "+dice+" You lose:(");
numLosses++;
}
while (dice != 0)
{
int die3 = (int) (Math.random()*6 + 1);
int die4 = (int) (Math.random()*6 + 1);
int dice2 = die3 + die4;
System.out.println("Roll: total = "+dice2);
if (dice2 == 2|| dice2 == 3 || dice2 == 12)
{
System.out.println("Sorry, with a "+dice2+" You lose:(");
numLosses++;
dice = 0;
}
else if (dice2 == 7 || dice2 == 11)
{
System.out.println("Woah!!! With a: "+dice2+ " You WIN!!!!!!!!");
numWins++;
dice = 0;
}
{
System.out.println("So far you have won " + numWins +
" times and lost " + numLosses + " times, ");
{
}
}
}
System.out.printf("\n\nWould you like to play again? ");
Scanner scanner = new Scanner(System.in);
String uinput = scanner.nextLine();
if(uinput.isEmpty() ||
uinput.equals("n") || uinput.equals("no"))
{
scanner.close();
System.out.println("Goodbye.");
return;
}
System.out.println();
}}