使用输入循环动态创建对象 [英] Dynamic object creation using input loops

查看:60
本文介绍了使用输入循环动态创建对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have successfully completed, and no problems with the program, everything works just fine, except it does not show the the maximum and minimum price of the book. But, I do not want the program like this.

当前,该程序由包含书名的预定义数组组成,也就是说,每当用户输入书的输入时,该程序就会显示书名和书价.因此,我在想一些不同的东西,即无需预先存储阵列中的书名和价格.

Currently the program consists of pre-defined array that consists of book name, that is, whenever a user enters input for a book, the program shows the book name and price of the book. So, I am thinking of something different, that is without pre-storing the book names and prices in the array.

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

Whatever name the user key-in for the book, that book name will be stored as an array along with the price. And the loop will be three times, that is after 1 input of book name and price, the user will again be prompted to enter book name, and last the the total number books and price will be displayed along with the maximum and minimum prices of the book, the user purchased.

有人可以帮我吗?请在下面查看我的编码:

Can anyone help me with this? Please see my coding below:

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

推荐答案

这可能不是您的问题(我尚未真正理解),但是对于评论来说太长了.您可以先将所有if替换为:

That probably does not your question (which I have not really understood) but this is too long for a comment. You could start by replacing all your ifs by:

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

这篇关于使用输入循环动态创建对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆