Java 使用不带构造函数参数的对象
所以基本上我正在创建一个下注程序,其中Player1、Player2、Player3等都是对象,每个对象都有五个初始化字段-1,这意味着还没有下注 我要求用户输入他们的下注号码(这些号码是数组中的整数),他们最多可以有五次下注,解释了-1的五个初始化字段 这里的问题是,我似乎找不到一种方法让某些玩家只输入1或2个赌注,而另一个玩家则输入4和5。我的程序强制每个用户输入5个整数,即使他们拒绝下注5次。 我有什么办法可以让它工作吗 这是我的驾驶课:(其他部分都不相关) 这是我的另一个球员课程:Java 使用不带构造函数参数的对象,java,Java,所以基本上我正在创建一个下注程序,其中Player1、Player2、Player3等都是对象,每个对象都有五个初始化字段-1,这意味着还没有下注 我要求用户输入他们的下注号码(这些号码是数组中的整数),他们最多可以有五次下注,解释了-1的五个初始化字段 这里的问题是,我似乎找不到一种方法让某些玩家只输入1或2个赌注,而另一个玩家则输入4和5。我的程序强制每个用户输入5个整数,即使他们拒绝下注5次。 我有什么办法可以让它工作吗 这是我的驾驶课:(其他部分都不相关) 这是我的另一个球员课程: pu
public class Player {
int number1;
int number2;
int number3;
int number4;
int number5;
// constructor for each player at the table
public Player(int number1, int number2, int number3, int number4, int number5) {
this.number1 = number1;
this.number2 = number2;
this.number3 = number3;
this.number4 = number4;
this.number5 = number5;
一号,二号。。。代表五个赌注
同样要澄清的是,用户不能被要求输入某个字符串,int,char来让程序知道他/她已经输入了他们的赌注。它必须看起来像这样
Player 1: 12 13 15
Player 2: 0 5 4
有很多方法可以做到这一点:
- 如果只有一个带5个参数的构造函数,则使用1,2,3,4,5个参数的构造函数
- 更好的方法是使用生成器模式,因为以后可能需要下5次以上的赌注,所以这是一个更好的选择,而不是继续添加构造函数
- 您还可以使用变量参数。看
- Like@immibs说,您还可以拥有Like成员变量列表,然后可以添加到任意数量的变量中
用户
类
public class User {
private final String firstName; // required
private final String lastName; // required
private final int age; // optional
private final String phone; // optional
private final String address; // optional
public static class UserBuilder {
private final String firstName;
private final String lastName;
private int age;
private String phone;
private String address;
public UserBuilder(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public UserBuilder withAge(int age) {
this.age = age;
return this;
}
public UserBuilder withPhone(String phone) {
this.phone = phone;
return this;
}
public UserBuilder withAddress(String address) {
this.address = address;
return this;
}
public User build() {
// if(age < 0) { ... } return new User(this);
// not thread-safe, a second thread may modify the value of age
User user = new User(this);
if (user.getAge() < 0) {
throw new IllegalStateException("Age out of range!"); // thread-safe
}
return user;
}
}
private User(UserBuilder builder) {
this.firstName = builder.firstName;
this.lastName = builder.lastName;
this.age = builder.age;
this.phone = builder.phone;
this.address = builder.address;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public int getAge() {
return age;
}
public String getPhone() {
return phone;
}
public String getAddress() {
return address;
}
}
跟进你的评论,帕特里克。 这就是你使用的方法 或者,您可以将
keyin.nextLine()
的输出存储到一个字符串中,然后应用parseInt或对其求值以获得您想要的“关键字”
String keyedinString = keyin.nextLine();
if(keyinString.equalsIgnoreCase("done betting") {
throw new Exception();
}
你很快就会意识到这个who过程是多么重复。因此,正如其他人所建议的,考虑使用数组和任务循环。像这样
int[] player1Bets = new int[5];
for(int i=0;i<5;i++) {
try {
player1Bets[i] = Integer.parseInt(keyin.nextLine());
}
catch(NumberFormatException e) {
System.out.println("Player1 took" + i + "bets. he is done betting");
}
}
...
int[]player1Bets=newint[5];
对于(int i=0;i您了解列表了吗?(或数组)假设keyin是java.util.Scanner对象
,程序总是要求五个数字,因为您让程序要求五个数字。而不是nextInt()
执行nextLine()
并使用integer.parseInt()将字符串解析为整数
如果nextLine()
的输入仅为\n
(返回键)则假定它是无赌注的,然后转到下一个玩家。或者建立自己的约定,例如,如果用户键入“no more”或其他内容。然后在每次输入时将您的输入与之匹配。@anu我该如何使用这个interger.parseInt()?Java的新手!感谢您的帮助,实际上5次下注是允许的最大数量!但感谢您的回答是的,但我只是举了一个例子,想象一下玩家以后可以下注6次或更多。谢谢!顺便说一句,我用数组和try catch实现了上面的代码,但出于某种原因,相同的SOP不断出现虽然每个玩家我的接球不同…?SOP?请解释?System.out.println@Anu您是否更改了每个接球区块的sysout(SOP)内部字符串?听起来您没有更改,不知怎的,我发现是for循环导致了问题,我应该在player1Bets[i]中切换哪个数字?
...
Player player2 = new Player(-1, -1, -1, -1, -1);
System.out.println("Player 2: ");
try {
number1 = Integer.parseInt(keyin.nextLine());
number2 = Integer.parseInt(keyin.nextLine());
number3 = Integer.parseInt(keyin.nextLine());
number4 = Integer.parseInt(keyin.nextLine());
number5 = Integer.parseInt(keyin.nextLine());
System.out.println("");
}
//Code will go to this block when user entered something other than integers
catch(NumberFormatException e) {
System.out.println("Done accepting bets from player2");
}
...
String keyedinString = keyin.nextLine();
if(keyinString.equalsIgnoreCase("done betting") {
throw new Exception();
}
int[] player1Bets = new int[5];
for(int i=0;i<5;i++) {
try {
player1Bets[i] = Integer.parseInt(keyin.nextLine());
}
catch(NumberFormatException e) {
System.out.println("Player1 took" + i + "bets. he is done betting");
}
}
...