我试图解决这个问题。我创建了一个菜单和一个Scanner来获取变量。现在我希望每个对象都存储在一个包含自己信息的数组中。但是我的代码没有按照我想要的方式工作。
package com.Adams;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner vehicle = new Scanner(System.in);
System.out.println("Enter your auctionId ");
String auctionId = vehicle.nextLine() ;
System.out.println("Enter the vehicule description");
String vehicleDescription = vehicle.nextLine();
System.out.println("What is the reserve price");
int resevePrice = vehicle.nextInt();
VehiculeAuction newVehicle = new VehiculeAuction(auctionId, vehicleDescription, resevePrice );
// Define an array of VehicleAuction references named auctions,
// which can be used to store up to 20 VehicleAuction objects.
VehiculeAuction auctions[] = new VehiculeAuction[20];
for (int i = 0; i < auctions.length; i ++) {
auctions[i] = new VehiculeAuction("", "", 0);
System.out.println(auctions[i]);
}
int auctionCount = 0;
do {
auctionCount++;
System.out.printf("***** Auction System Menu *****");
System.out.printf("%n" + "A. " + "Add New Vehicle Auction");
System.out.printf("%n" + "B. " + "Display All Vehicle Auctions");
System.out.printf("%n" + "C. " + "Submit Bid for Vehicle");
System.out.printf("%n" + "D. " + "List Ended Vehicle Auctions");
System.out.printf("%n" + "X. " + "Exit the program");
System.out.printf("%n" + "Enter your selection: ");
Scanner console = new Scanner(System.in);
String selection = console.nextLine();
switch (selection.toUpperCase()) {
case "A": {
System.out.println("testA");
break;
}
case "B": {
System.out.println("testB");
//Display all vehicle auctions
break;
}
case "C": {
System.out.println("testC");
//Submit Bid for Vehicle
break;
}
case "D": {
System.out.println("testD");
//List ended vehicle auctions
break;
}
case "X": {
System.exit(0);
}
default:
System.out.printf("That is not a valid response. Try Again.");
}
} while (auctionCount <= 20);
System.out.println(auctionCount);
}
}
答案 0 :(得分:0)
三个观察结果:
case "A":
处理的一部分吗?auctionCount++
循环顶部的do...while
行:为什么会出现?如果auctionCount
确实是竞价计数,您只想在实际将竞价添加到您的列表时增加它,是吗?我认为这只应该作为case "A":
处理的一部分而发生。事实上,auctionCount
只是计算您要求用户输入菜单选项的次数。Scanner
上有多个System.in
:您应该只创建一个并在每次需要输入时使用它。每个Scanner
都希望它是仅一个读取输入,并且试图读取相同输入源的几个Scanner
将相互“混淆”。 除了这些问题之外,看起来你有一个相当不错的开始。