使用输入循环创建动态对象

时间:2012-06-01 15:31:06

标签: java arrays loops

我已成功完成,并且程序没有任何问题,一切正常,但它没有显示本书的最高和最低价格。但是,我不希望这样的程序。

目前,该程序由预定义的数组组成,该数组由书名组成,即每当用户输入书籍的输入时,该程序显示书籍的书名和价格。所以,我正在考虑一些不同的东西,即没有预先存储数组中的书名和价格。

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

任何人都可以帮我吗?请参阅下面的编码:

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 个答案:

答案 0 :(得分:2)

我完全有可能误解了你的要求,在这种情况下,忽略了下面的一切。

目前,您正在将当前迭代的书籍与下一本书进行比较,并在for循环中对我进行两次实施。试试这个:

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]);

当然,这假设您的图书清单不是空的。

答案 1 :(得分:2)

因为您正在使用动态输入,所以最终可以使用任意数量的书籍,这就是您不应再使用数组的原因。它们的大小无法轻易更改,因此我建议您改为ArrayList

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

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 + ")");

答案 2 :(得分:1)

这可能不是你的问题(我还没有真正理解),但这个评论太长了。您可以先将所有ifs替换为:

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

答案 3 :(得分: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);

}