为什么我的循环在我的投币游戏中不能正常工作?
Why my loop doesnt work properly in my coin toss game?
我已经开始攻读计算机科学相关领域的学士学位,所以我应该编码,一切对我来说都是全新的,但我想我们都是从头开始的。
我很难使我的代码按应有的方式运行。我必须编写一个翻转硬币游戏....创建随机数(even/odd),使用用户输入然后用户应该玩他想要的时间,因此我创建了一个 while-loop 但它没有'似乎不起作用 属性。我已经尝试将我的代码放入其中,但它也没有用。我的 IDE 还告诉我,我从不使用分配给 scanner.nextInt() 的值,即 UserEingabe。我很确定对于你们中的许多人来说,这是很容易解决的问题,但我有点挣扎。在此先感谢您的帮助。
代码:
主要 class
class CoinObject {
public static void main(String[] args) {
Coin coinObject = new Coin();
coinObject.throwCoin();
}
}
第二个class:
import java.util.Scanner;
public class Coin {
public void throwCoin(){
Scanner scanner = new Scanner(System.in);
System.out.println("Erraten sie, ob Kopf oder Zahl oben liegt:");
System.out.println("Kopf=0");
System.out.println("Zahl=1");
int UserEingabe = scanner.nextInt();
int randomNumber = (int) Math.random();
String yes = "yes";
String no = "no";
int spiele = 1;
int victories = 1;
String play = scanner.next();
// if the input = the random #, cool!, otherwise false :)
if (UserEingabe == randomNumber){
System.out.println("Sie haben richtig geraten");
System.out.println("Moechten Sie weiter spielen (yes/no)");
play = scanner.next();
} else {
System.out.println("Sie haben falsch geraten");
System.out.println("Moechten Sie weiter spielen (yes/no)");
play = scanner.next();
} if (UserEingabe != 0 || UserEingabe != 1){
System.out.println("falsche Eingabe, versuchen Sie wieder");
UserEingabe = scanner.nextInt();
}
// the loop will be repeat it as long as the player wants to play
while (play != no){
UserEingabe = scanner.nextInt();
if (play == yes){
System.out.println("Sie haben " + spiele + "Spiele gespielt und " + victories + "Spiele gewonnen");
victories=victories +1;
spiele = spiele+1;
}
}
}
}
据我所知,重要的东西(三个 if)不在你的 while 循环中,所以它们只执行一次,然后就再也不会执行了。我想他们也应该在 while 循环中。我的建议:
do {
if (UserEingabe == randomNumber){
System.out.println("Sie haben richtig geraten");
System.out.println("Moechten Sie weiter spielen (yes/no)");
play = scanner.next();
} else {
System.out.println("Sie haben falsch geraten");
System.out.println("Moechten Sie weiter spielen (yes/no)");
play = scanner.next();
} if (UserEingabe != 0 || UserEingabe != 1){
System.out.println("falsche Eingabe, versuchen Sie wieder");
UserEingabe = scanner.nextInt();
}
// the loop will be repeat it as long as the player wants to play
UserEingabe = scanner.nextInt();
if (play == yes){
System.out.println("Sie haben " + spiele + "Spiele gespielt und " + victories + "Spiele gewonnen");
victories=victories +1;
spiele = spiele+1;
}
} while (play != no);
我认为您应该使用 do while 而不是 while,因为括号 {} 内的所有内容都将至少执行一次(然后取决于您在 while 内编写的内容最终执行多次)。
CoinObject(主要方法)
public class CoinObject {
public static void main (String[] args) {
Coin coinObject = new Coin();
coinObject.initializeGame();
}
}
硬币(游戏逻辑)
import java.util.Random;
import java.util.Scanner;
public class Coin {
// In this block we initialize the global varibales, that can be used and modified by all the methods in the class
Scanner scanner = new Scanner(System.in);
private int games = 0;
private int victories = 0;
private int rightInputGuess = 0;
private int wrongInputGuess = 0;
private int[] items = new int[]{0,1};
private Random rand = new Random();
// This method creates the gameheader, meaning it creates a random number and passes it to the game method
public void initializeGame() {
System.out.println("Erraten sie, ob Kopf oder Zahl oben liegt:");
System.out.println("Kopf=0");
System.out.println("Zahl=1");
int randomNumber = rand.nextInt(items.length);
if (randomNumber == 1) {
rightInputGuess = 1;
wrongInputGuess = 0;
} else if (randomNumber == 0) {
rightInputGuess = 0;
wrongInputGuess = 1;
}
playGame(randomNumber);
}
// This method is the actual game logic
// It takes the generates randomNumber as parameter.
// if the user types something else as 0 or 1 he will be asked to try to guess the number again.
public void playGame(int randomNumber) {
int userInput = scanner.nextInt();
String play;
if (userInput == rightInputGuess){
System.out.println("Sie haben richtig geraten");
System.out.println("Moechten Sie weiter spielen (yes/no)");
play = scanner.next();
if(play.equals("yes")) {
victories=victories +1;
games = games+1;
initializeGame();
}
else if (play.equals("no")){
victories=victories +1;
games = games+1;
System.out.println("Sie haben " + games + " Spiele gespielt und " + victories + " Spiele gewonnen");
}
} else if (userInput == wrongInputGuess){
System.out.println("Sie haben falsch geraten");
System.out.println("Moechten Sie weiter spielen (yes/no)");
play = scanner.next();
if(play.equals("yes")) {
games = games+1;
initializeGame();
}
else if (play.equals("no")){
games = games+1;
System.out.println("Sie haben " + games + " Spiele gespielt und " + victories + " Spiele gewonnen");
}
} else if (userInput != 0 || userInput != 1){
System.out.println("falsche Eingabe, versuchen Sie wieder");
// The playGame method is called with the current randomNumber.
// If the user types something else as 0 or 1 he gets the chance to type a valid guess
playGame(randomNumber);
}
}
}
这符合您的游戏要求。如果用户尝试一些不是“0”或“1”的无效输入,他将有机会输入另一个输入并猜测当前的随机数。
我已经开始攻读计算机科学相关领域的学士学位,所以我应该编码,一切对我来说都是全新的,但我想我们都是从头开始的。
我很难使我的代码按应有的方式运行。我必须编写一个翻转硬币游戏....创建随机数(even/odd),使用用户输入然后用户应该玩他想要的时间,因此我创建了一个 while-loop 但它没有'似乎不起作用 属性。我已经尝试将我的代码放入其中,但它也没有用。我的 IDE 还告诉我,我从不使用分配给 scanner.nextInt() 的值,即 UserEingabe。我很确定对于你们中的许多人来说,这是很容易解决的问题,但我有点挣扎。在此先感谢您的帮助。
代码: 主要 class
class CoinObject {
public static void main(String[] args) {
Coin coinObject = new Coin();
coinObject.throwCoin();
}
}
第二个class:
import java.util.Scanner;
public class Coin {
public void throwCoin(){
Scanner scanner = new Scanner(System.in);
System.out.println("Erraten sie, ob Kopf oder Zahl oben liegt:");
System.out.println("Kopf=0");
System.out.println("Zahl=1");
int UserEingabe = scanner.nextInt();
int randomNumber = (int) Math.random();
String yes = "yes";
String no = "no";
int spiele = 1;
int victories = 1;
String play = scanner.next();
// if the input = the random #, cool!, otherwise false :)
if (UserEingabe == randomNumber){
System.out.println("Sie haben richtig geraten");
System.out.println("Moechten Sie weiter spielen (yes/no)");
play = scanner.next();
} else {
System.out.println("Sie haben falsch geraten");
System.out.println("Moechten Sie weiter spielen (yes/no)");
play = scanner.next();
} if (UserEingabe != 0 || UserEingabe != 1){
System.out.println("falsche Eingabe, versuchen Sie wieder");
UserEingabe = scanner.nextInt();
}
// the loop will be repeat it as long as the player wants to play
while (play != no){
UserEingabe = scanner.nextInt();
if (play == yes){
System.out.println("Sie haben " + spiele + "Spiele gespielt und " + victories + "Spiele gewonnen");
victories=victories +1;
spiele = spiele+1;
}
}
}
}
据我所知,重要的东西(三个 if)不在你的 while 循环中,所以它们只执行一次,然后就再也不会执行了。我想他们也应该在 while 循环中。我的建议:
do {
if (UserEingabe == randomNumber){
System.out.println("Sie haben richtig geraten");
System.out.println("Moechten Sie weiter spielen (yes/no)");
play = scanner.next();
} else {
System.out.println("Sie haben falsch geraten");
System.out.println("Moechten Sie weiter spielen (yes/no)");
play = scanner.next();
} if (UserEingabe != 0 || UserEingabe != 1){
System.out.println("falsche Eingabe, versuchen Sie wieder");
UserEingabe = scanner.nextInt();
}
// the loop will be repeat it as long as the player wants to play
UserEingabe = scanner.nextInt();
if (play == yes){
System.out.println("Sie haben " + spiele + "Spiele gespielt und " + victories + "Spiele gewonnen");
victories=victories +1;
spiele = spiele+1;
}
} while (play != no);
我认为您应该使用 do while 而不是 while,因为括号 {} 内的所有内容都将至少执行一次(然后取决于您在 while 内编写的内容最终执行多次)。
CoinObject(主要方法)
public class CoinObject {
public static void main (String[] args) {
Coin coinObject = new Coin();
coinObject.initializeGame();
}
}
硬币(游戏逻辑)
import java.util.Random;
import java.util.Scanner;
public class Coin {
// In this block we initialize the global varibales, that can be used and modified by all the methods in the class
Scanner scanner = new Scanner(System.in);
private int games = 0;
private int victories = 0;
private int rightInputGuess = 0;
private int wrongInputGuess = 0;
private int[] items = new int[]{0,1};
private Random rand = new Random();
// This method creates the gameheader, meaning it creates a random number and passes it to the game method
public void initializeGame() {
System.out.println("Erraten sie, ob Kopf oder Zahl oben liegt:");
System.out.println("Kopf=0");
System.out.println("Zahl=1");
int randomNumber = rand.nextInt(items.length);
if (randomNumber == 1) {
rightInputGuess = 1;
wrongInputGuess = 0;
} else if (randomNumber == 0) {
rightInputGuess = 0;
wrongInputGuess = 1;
}
playGame(randomNumber);
}
// This method is the actual game logic
// It takes the generates randomNumber as parameter.
// if the user types something else as 0 or 1 he will be asked to try to guess the number again.
public void playGame(int randomNumber) {
int userInput = scanner.nextInt();
String play;
if (userInput == rightInputGuess){
System.out.println("Sie haben richtig geraten");
System.out.println("Moechten Sie weiter spielen (yes/no)");
play = scanner.next();
if(play.equals("yes")) {
victories=victories +1;
games = games+1;
initializeGame();
}
else if (play.equals("no")){
victories=victories +1;
games = games+1;
System.out.println("Sie haben " + games + " Spiele gespielt und " + victories + " Spiele gewonnen");
}
} else if (userInput == wrongInputGuess){
System.out.println("Sie haben falsch geraten");
System.out.println("Moechten Sie weiter spielen (yes/no)");
play = scanner.next();
if(play.equals("yes")) {
games = games+1;
initializeGame();
}
else if (play.equals("no")){
games = games+1;
System.out.println("Sie haben " + games + " Spiele gespielt und " + victories + " Spiele gewonnen");
}
} else if (userInput != 0 || userInput != 1){
System.out.println("falsche Eingabe, versuchen Sie wieder");
// The playGame method is called with the current randomNumber.
// If the user types something else as 0 or 1 he gets the chance to type a valid guess
playGame(randomNumber);
}
}
}
这符合您的游戏要求。如果用户尝试一些不是“0”或“1”的无效输入,他将有机会输入另一个输入并猜测当前的随机数。