【发布时间】:2015-01-01 17:34:26
【问题描述】:
我正在尝试使用 Java,但根本没有接受过正式培训。我从某个地方的一门课程中找到了一个作业,并认为我会尝试一下。到目前为止一切都很顺利,除了我需要让用户进行输入,该输入是从给定列表中选择的。我在处理 String 值方面没有任何问题,但他们也可以选择从 0-39 中选择一个数字,我希望将其存储为 int。
任何帮助将不胜感激。
import java.util.Scanner;
public class RouletteGame {
static Player p1;
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
int num = (int)(Math.random()*37);
int winnings = 0;
int x = 0;
int counter = 1;
int p = 1;
String name;
String choice;
int iAmount = 100;
int betNum;
System.out.println("Welcome to Cache Creek style Rouylette..bet an amount and type");
System.out.println(" if you select the correct colour, you win double your bet");
System.out.println(" if you select the correct odd/even, you win double your bet");
System.out.println(" if you select the correct number, you win 40 times your bet");
System.out.println(" otherwise you lose your bet");
System.out.println("If you lose all your money, the game is over");
System.out.println();
System.out.print("How much do you want to bet? $");
int bet=scan.nextInt();
while (x==0){
Scanner scann = new Scanner(System.in);
System.out.println("What is your bet? (odd/even/red/black/exact number)");
choice=scann.nextLine();
}
if (choice.equals("odd")){
System.out.println("You have selected odd.");
x=1;
}
else if (choice.equals("even")){
System.out.println("You have selected even.");
x=1;
}
else if (choice.equals("red")){
System.out.println("You have selected red.");
x=1;
}
else if (choice.equals("black")){
System.out.println("You have selected black.");
x=1;
}
else if (choice.equals(int)){
betNum = Integer.parseInt(choice);
System.out.println("You have selected " +betNum);
x=1;
}
else{
System.out.println("Invalid value: Must be odd, even, red, black or a number between 0 and 39");
x=0;
}
}
}
}
【问题讨论】:
-
使用 Integer.parseInt(String_input);将 String 转换为 int
-
@KrsnaChaitanya 他已经这样做了。问题出在
else if (choice.equals(int)) {这一行 -
是的,vefthym,我知道那是错误的..只是不知道该放什么
-
我已经对此发表了评论。点击此链接查看答案:stackoverflow.com/questions/5439529/…