我正在尝试制作一个简单的赔率和赔率游戏。该程序可以完美运行直到“ 1”。问题是:它没有直接将“ finger”和“ computer”相乘,而是直接给出了“ computer”的值,尽管我在其余代码中没有遇到任何错误,但是我也没有任何输出。我将“名称”,“ oe”和“手指”设为静态,以便可以在循环外使用它们。
import java.util.Scanner;
import java.util.Random;
public class OddsAndEvens {
static String name;
static String oe;
static int finger;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Let’s play a game called “Odds and Evens”");
System.out.println("What is your name?");
String name = input.next();
while (true) {
System.out.println("Hello " + name + " which one do you choose? (O)dds or (E)vens?");
String oe = input.next();
if (oe.equalsIgnoreCase("e")) {
System.out.println(name + " has picked evens! The computer will be odds.");
break;
}
if (oe.equalsIgnoreCase("o")) {
System.out.println(name + " has picked odds! The computer will be evens.");
break;
} else {
System.out.println("You have typed an invalid answer, lets try again");
}
}
while (true) {
System.out.print("How many \"fingers\" do you put out? ");
int finger = input.nextInt();
if (finger >= 0 && finger <= 5) {
break;
} else {
System.out.println("Please write a number between 0 and 5");
}
}
Random rand = new Random();
int computer = rand.nextInt(5) + 0;
System.out.println("Computer plays number " + computer);
System.out.println("----------------------------------------------");
int sum = finger + computer; //1<------------------------------
if (sum % 2 == 0) {
System.out.println(sum + " is even!");
if (oe.equalsIgnoreCase("e") && (sum % 2 == 0)) {
System.out.println(name + " wins!");
} else {
System.out.println("Computer wins!");
}
if (sum % 2 != 0) {
System.out.println(sum + " is odd!");
if (oe.equalsIgnoreCase("o") && (sum % 2 != 0)) {
System.out.println(name + " wins!");
} else {
System.out.println("Computer wins!");
}
}
}
}
}
答案 0 :(得分:0)
在第二个while循环中,通过说finger
来初始化仅在while循环内有效的新局部变量int finger =
,而不是说finger =
来调用全局静态变量finger
结果是,全局变量finger
尚未初始化,并且sum
没有要添加到变量computer
的内容。
答案 1 :(得分:0)
当您在类中使用静态值时,您不应该在while指令中第二次初始化静态值,因为它们是局部指令-因此,一旦您写入局部值和局部值,便要使用类变量。 第二件事是划分您的if。如果检查和是否为奇数,那应该是另一部分,如果它是奇数。它也可以通过if ... else来完成。 您的代码:
package stackoverflow;
import java.util.Scanner; 导入java.util.Random;
公共类OddsAndEvens {
static String name;
static String oe;
static int finger;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Let’s play a game called “Odds and Evens”");
System.out.println("What is your name?");
name = input.next();
while (true) {
System.out.println("Hello " + name + " which one do you choose? (O)dds or (E)vens?");
oe = input.next();
if (oe.equalsIgnoreCase("e")) {
System.out.println(name + " has picked evens! The computer will be odds.");
break;
}
if (oe.equalsIgnoreCase("o")) {
System.out.println(name + " has picked odds! The computer will be evens.");
break;
} else {
System.out.println("You have typed an invalid answer, lets try again");
}
}
while (true) {
System.out.print("How many \"fingers\" do you put out? ");
finger = input.nextInt();
if (finger >= 0 && finger <= 5) {
break;
} else {
System.out.println("Please write a number between 0 and 5");
}
}
Random rand = new Random();
int computer = rand.nextInt(5) + 0;
System.out.println("Computer plays number " + computer);
System.out.println("----------------------------------------------");
int sum = finger + computer; //1<------------------------------
if (sum % 2 == 0) {
System.out.println(sum + " is even!");
if (oe.equalsIgnoreCase("e") && (sum % 2 == 0)) {
System.out.println(name + " wins!");
} else {
System.out.println("Computer wins!");
}
}
if (sum % 2 != 0) {
System.out.println(sum + " is odd!");
if (oe.equalsIgnoreCase("o") && (sum % 2 != 0)) {
System.out.println(name + " wins!");
} else {
System.out.println("Computer wins!");
}
}
}
答案 2 :(得分:0)
您遇到的问题是,您要两次声明变量“名称”,“ oe”和“手指”。您应该只写int finger = input.nextInt();
而不是finger = input.nextInt();
,因为在您的代码中,您是在循环内声明一个新的变量calls finger,并在循环内使用它,当您的程序移至循环外时,它将使用静态可变手指巫婆将等于零,这就是为什么您总是得到计算机的价值。
由于相同的原因您没有得到任何输出,您要更新的“ oe”与您在if语句中测试的“ oe”不同。
您是if语句也混在一起,第二个if语句在第一个if语句之内。
您的新代码应该是:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Let’s play a game called “Odds and Evens”");
System.out.println("What is your name?");
name = input.next();
while (true) {
System.out.println("Hello " + name + " which one do you choose? (O)dds or (E)vens?");
oe = input.next();
if (oe.equalsIgnoreCase("e")) {
System.out.println(name + " has picked evens! The computer will be odds.");
break;
}
if (oe.equalsIgnoreCase("o")) {
System.out.println(name + " has picked odds! The computer will be evens.");
break;
} else {
System.out.println("You have typed an invalid answer, lets try again");
}
}
while (true) {
System.out.print("How many \"fingers\" do you put out? ");
finger = input.nextInt();
if (finger >= 0 && finger <= 5) {
break;
} else {
System.out.println("Please write a number between 0 and 5");
}
}
Random rand = new Random();
int computer = rand.nextInt(5) + 0;
System.out.println("Computer plays number " + computer);
System.out.println("----------------------------------------------");
int sum = finger + computer; //1<------------------------------
if (sum % 2 == 0) {
System.out.println(sum + " is even!");
if (oe.equalsIgnoreCase("e") && (sum % 2 == 0)) {
System.out.println(name + " wins!");
} else {
System.out.println("Computer wins!");
}
}
else {
System.out.println(sum + " is odd!");
if (oe.equalsIgnoreCase("o") && (sum % 2 != 0)) {
System.out.println(name + " wins!");
} else {
System.out.println("Computer wins!");
}
}
}
}