创建拍卖列表(java)
我试图为拍卖创建一个代码,这样当拍卖开始时,一只狗就会得到一个号码。创建的第一次拍卖将获得1号,下一次获得2号,依此类推 问题是,拍卖代码没有列出拍卖的狗。相反,它是通过注册列出的 例如: 注册狗创建拍卖列表(java),java,eclipse,Java,Eclipse,我试图为拍卖创建一个代码,这样当拍卖开始时,一只狗就会得到一个号码。创建的第一次拍卖将获得1号,下一次获得2号,依此类推 问题是,拍卖代码没有列出拍卖的狗。相反,它是通过注册列出的 例如: 注册狗 鲍伊 安娜 玛雅人 (拍卖过程) 命令:开始拍卖 狗名:玛雅 输出:Maya已进入拍卖#2 命令:开始拍卖 狗名:鲍伊 输出:Bowie已投入拍卖#0 这是我的代码: private void startAuction() { boolean current = false;
- 鲍伊
- 安娜
- 玛雅人
命令:开始拍卖 狗名:鲍伊 输出:Bowie已投入拍卖#0
这是我的代码:
private void startAuction() {
boolean current = false;
do {
System.out.println("Dog name: ");
String dogName = scan.nextLine().toLowerCase().trim();
if (dogName.isEmpty()) {
System.out.println("Error: Name can't be empty.");
continue;
}
for (int i = 0; i < dogs.size(); i++) {
if (dogName.equals(dogs.get(i).getName())) {
auction.add(new Auction(dogName));
System.out.printf(dogName + " has been put up for auction in auction #%d", i);
System.out.println();
current = true;
return;
}
}
if (current == false) {
System.out.println("Error: no such dog in the register");
}
} while(true);
private void startaaction(){
布尔电流=假;
做{
System.out.println(“狗名:”);
字符串dogName=scan.nextLine().toLowerCase().trim();
if(dogName.isEmpty()){
System.out.println(“错误:名称不能为空。”);
继续;
}
对于(int i=0;i
我是一个初学者,有点困惑。你知道如何解决这个问题吗?这里的问题是,在获得要拍卖的狗的名字后,你在列表中搜索该狗,打印该狗在列表中的位置的索引。要解决这个问题,你需要做的是使用另一个计数器变量来计算被拍卖狗的数量,递增I每次都是ng。代码如下所示:
private void startAuction() {
boolean current = false;
int auctionCount = 1;//Declare the current auction we are on
do {
System.out.println("Dog name: ");
String dogName = scan.nextLine().toLowerCase().trim();
if (dogName.isEmpty()) {
System.out.println("Error: Name can't be empty.");
continue;
}
for (int i = 0; i < dogs.size(); i++) {
if (dogName.equals(dogs.get(i).getName())) {
auction.add(new Auction(dogName));
//Use the auction count here so that it starts at 1 and increases
System.out.printf(dogName + " has been put up for auction in auction #%d", auctionCount);from there
System.out.println();
auctionCount++;//Make sure the next auction has a number that is one larger
current = true;
return;
}
}
if (current == false) {
System.out.println("Error: no such dog in the register");
}
} while(true);
}
private void startaaction(){
布尔电流=假;
int auctionCount=1;//声明我们正在进行的当前拍卖
做{
System.out.println(“狗名:”);
字符串dogName=scan.nextLine().toLowerCase().trim();
if(dogName.isEmpty()){
System.out.println(“错误:名称不能为空。”);
继续;
}
对于(int i=0;i
这里的问题是,在获得要拍卖的狗的名称后,您在列表中搜索该狗,打印该狗在列表中的位置索引。要解决这个问题,您需要做的是使用另一个计数器变量来计算被拍卖狗的数量,每次递增。代码如下所示:
private void startAuction() {
boolean current = false;
int auctionCount = 1;//Declare the current auction we are on
do {
System.out.println("Dog name: ");
String dogName = scan.nextLine().toLowerCase().trim();
if (dogName.isEmpty()) {
System.out.println("Error: Name can't be empty.");
continue;
}
for (int i = 0; i < dogs.size(); i++) {
if (dogName.equals(dogs.get(i).getName())) {
auction.add(new Auction(dogName));
//Use the auction count here so that it starts at 1 and increases
System.out.printf(dogName + " has been put up for auction in auction #%d", auctionCount);from there
System.out.println();
auctionCount++;//Make sure the next auction has a number that is one larger
current = true;
return;
}
}
if (current == false) {
System.out.println("Error: no such dog in the register");
}
} while(true);
}
private void startaaction(){
布尔电流=假;
int auctionCount=1;//声明我们正在进行的当前拍卖
做{
System.out.println(“狗名:”);
字符串dogName=scan.nextLine().toLowerCase().trim();
if(dogName.isEmpty()){
System.out.println(“错误:名称不能为空。”);
继续;
}
对于(int i=0;i
为什么不使用拍卖列表的大小?(假设它是一个列表,并且没有拍卖被删除),但现在auctionCount停留在1。这就好像我在拍卖中添加了另一只狗后整个列表都重置了一样:/when不使用拍卖列表的大小?(假设它是一个列表,并且没有拍卖被删除)但现在auctionCount停留在1。这就好像当我在拍卖中添加另一只狗时,整个列表就会重置:/