0

我已经成功完成,程序没有问题,一切正常,除了它没有显示这本书的最高和最低价格。但是,我不想要这样的程序。

目前,该程序由包含书名的预定义数组组成,也就是说,每当用户输入一本书的输入时,程序都会显示书名和书的价格。所以,我在想一些不同的东西,那就是没有在数组中预先存储书名和价格。

无论用户键入的书名是什么,该书名都将与价格一起存储为数组。循环3次,即在输入1次书名和价格后,会再次提示用户输入书名,最后会显示总书本和价格以及最高最低价格。书,用户购买。

谁能帮我这个?请在下面查看我的编码:

import java.util.Scanner;
public class BookStore {
    public static void main(String args[]){
        Scanner input = new Scanner(System.in);
        String[] books = {"Introduction To Java","Artificial Intlegence","Web Programming","Introduction To Database","English Speech","Introduction To C#"};

        double[] prices ={100,50,25,45,60,90}; 

        System.out.println("Welcome to SAJID's Book Shop");
        System.out.println();
        System.out.println("Please select the the book and write the number of the book :");
        System.out.println();
        System.out.println("1.Introduction To Java");
        System.out.println("2.Artificial Intlegence");
        System.out.println("3.Web Programming");
        System.out.println("4.Introduction To Database");
        System.out.println("5.English Speech");
        System.out.println("6.Introduction To C#");
        System.out.println();

        System.out.println("Total Number Of Books "+books.length);
        int totalBook;
        totalBook = books.length;

        double totalPrice=0;    
        int i=0;            
        while(i<=5){
            totalPrice+=prices[i];
            i++;
        }

        System.out.println("Total Price:$" + totalPrice);

        int bookTitle;
        System.out.print("Enter book Number: ");
        bookTitle = input.nextInt();

        if(bookTitle == 0){
            System.out.println("Book name: "+books[0]);
            System.out.print("Book price:$"+prices[0]);
            }

        if(bookTitle == 1){

            System.out.println("Book name: "+books[1]);
            System.out.print("Book price:$"+prices[1]);
        }
        if(bookTitle == 2){

            System.out.println("Book name: "+ books[2]);
            System.out.print("Book price:$"+prices[2]);
        }
        if(bookTitle == 3){

            System.out.println("Book name: "+books[3]);
            System.out.print("Book price:$"+prices[3]);
        }
        if(bookTitle == 4){

            System.out.println("Book name: "+books[4]);
            System.out.print("Book price:$"+prices[4]);
        }
        if(bookTitle == 5){

            System.out.println("Book name: "+books[5]);
            System.out.print("Book price:$"+prices[5]);
        }

        /*double min=0;
        for(i=0;i<books.length-1;i++){

            if(books[i] =< books[i++]){
                min=books[i];
                minBook = i;
            }*/
        }

        //System.out.print("Cheapest book: " + min);
    }       
 }
4

4 回答 4

2

因为您使用的是动态输入,所以您最终可以得到任意数量的书籍,这就是您不应再使用数组的原因。它们的大小不能轻易改变,所以我建议你改为ArrayList

此外,由于您的书籍和价格数组密切相关,您可能希望创建自己的 Book 对象来存储此信息。例如:

public class Book {
    String title;
    double price;
    public Book(String title, double price) {
        this.title = title;
        this.price = price;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }
}

为了使整个事情动态化,你基本上需要重写你的大部分代码,我已经在下面完成了。我不确定我是否在这里做了家庭作业,但如果是,我强烈建议您通读整个代码以了解它的作用。

Scanner input = new Scanner(System.in);
List<Book> books = new ArrayList<Book>();
boolean done = false;

System.out.println("Welcome to SAJID's Book Shop\n");
// Now the user can input some books.
String title;
double price;
while (!done) {
    System.out.println("Enter a book title (nothing to stop): ");
    title = input.nextLine();
    if (title.isEmpty()) {
        // User didn't enter a title, stop this loop.
        done = true;
    }
    else {
        System.out.println("Enter a price: ");
        price = input.nextDouble();
        // Because nextDouble() only reads the next number and doesn't finish the entire line, we have to manually go to the next line.
        input.nextLine(); 
        // Create a new book and add it to our shop.
        Book newBook = new Book(title, price);
        books.add(newBook);
    }
}

System.out.println("The following books are for sale:");
for (int i = 0; i < books.size(); i++) {
    // Remember that the first book in our list is number 0, not 1, so we add 1 to i.
    System.out.println((i + 1) + ". - " + books.get(i).title);
}
System.out.println("Total Number Of Books "+ books.size());

// Now buy some books.
List<Book> purchasedBooks = new ArrayList<Book>();
done = false;
String line;
int bookNumber;
while (!done) {
    System.out.println("Enter the number of a book you'd like to buy (0 to stop): ");
    bookNumber = input.nextInt();
    input.nextLine();
    if (bookNumber == 0) {
        // We stop if the user enters 0.
        done = true;
    }
    else {
        // Remember that the first book in our list is number 0, not 1.
        purchasedBooks.add(books.get(bookNumber - 1));
    }
}

System.out.println("You're buying these books:");
double totalPrice = 0;
for (int i = 0; i < purchasedBooks.size(); i++) {
    // Remember that the first book in our list is number 0, not 1, so we add 1 to i.
    System.out.println((i + 1) + ". - " + purchasedBooks.get(i).price + " - " + purchasedBooks.get(i).title);
    totalPrice += purchasedBooks.get(i).price;
}
System.out.println("Total Number Of Books Bought: " + purchasedBooks.size());
System.out.println("Total Price: " + totalPrice);

// Find the cheapest and the most expensive book.
Book minBook = purchasedBooks.get(0);
Book maxBook = purchasedBooks.get(0);
// Iterate over our books using a foreach loop.
for (Book book : purchasedBooks) {
    if (book.price < minBook.price) {
        minBook = book;
    }
    else if (book.price > maxBook.price) {
        maxBook = book;
    }
}
System.out.println("Cheapest Book: " + minBook.title + " (" + minBook.price + ")");
System.out.println("Most Expensive Book: " + maxBook.title + " (" + maxBook.price + ")");
于 2012-06-01T17:12:19.327 回答
2

我完全有可能误解了您的要求,在这种情况下,请忽略以下所有内容。

目前,您正在将当前迭代的书与下一本书进行比较,并在 for 循环中添加 i 两次。尝试这个:

double min = prices[i];
int minBook = 0;
for(int i = 1; i < books.length; i++) {
    if(prices[i] < min) {
        min = prices[i];
        minBook = i;
    }
}
System.out.print("Cheapest book: " + books[minBook]);

您可以对最大值执行类似的操作:

double max = prices[i];
int maxBook = 0;
for(int i = 1; i < books.length; i++) {
    if(prices[i] > max) {
        max = prices[i];
        maxBook = i;
    }
}

System.out.print("Cheapest book: " + books[maxBook]);

当然,这假设您的书籍列表不为空。

于 2012-06-01T15:41:03.617 回答
1

我发布的有点晚,但你去吧。我摆脱了所有的 if 语句并使用了一个开关,对您的价格数组进行了排序并更改了一些循环。编辑哎呀!我搞砸了。你想要最便宜的书,而不是我所做的最便宜的书价。

        Scanner input = new Scanner(System.in);
    String[] books = {"Introduction To Java","Artificial Intlegence","Web Programming","Introduction To Database","English Speech","Introduction To C#"};

    double[] prices ={100,50,25,45,60,90};

    System.out.println("Welcome to SAJID's Book Shop");
    System.out.println();
    System.out.println("Please select the the book and write the number of the book :");
    System.out.println();
    System.out.println("1.Introduction To Java");
    System.out.println("2.Artificial Intlegence");
    System.out.println("3.Web Programming");
    System.out.println("4.Introduction To Database");
    System.out.println("5.English Speech");
    System.out.println("6.Introduction To C#");
    System.out.println();

    System.out.println("Total Number Of Books "+books.length); 

    double totalPrice=0;
    for(int i = 0; i<=5; i++){
        totalPrice+=prices[i];
    }

    System.out.println("Total Price:$" + totalPrice);

    System.out.print("Enter book Number: ");

    int bookTitle = input.nextInt();
    switch(bookTitle){
      case 1:
        System.out.println("Book name: "+books[0]);
        System.out.println("Book price:$"+prices[0]);
        break;
      case 2:
        System.out.println("Book name: "+books[1]);
        System.out.println("Book price:$"+prices[1]);
        break;
      case 3:
        System.out.println("Book name: "+ books[2]);
        System.out.println("Book price:$"+prices[2]);
        break;
      case 4:
        System.out.println("Book name: "+books[3]);
        System.out.println("Book price:$"+prices[3]);
        break;
      case 5:
        System.out.println("Book name: "+books[4]);
        System.out.println("Book price:$"+prices[4]);
        break;
      default:// since there are only 5 books use #6 as default
        System.out.println("Book name: "+books[5]);
        System.out.println("Book price:$"+prices[5]);
        break;
  }

    // assuming the prices array holds the price of each individual book
    // sort the prices array
    java.util.Arrays.sort(prices);
    System.out.println("Cheapest book: " + prices[0]);
    System.exit(0);

}

于 2012-06-01T16:12:10.757 回答
1

这可能不是您的问题(我还没有真正理解),但这对于评论来说太长了。您可以通过以下方式替换所有 ifs 开始:

if(bookTitle >= 0 && bookTitle <= 5){
    System.out.println("Book name: "+books[bookTitle]);
    System.out.print("Book price:$"+prices[bookTitle]);
}
于 2012-06-01T15:36:22.417 回答