package les9;
import java.util.Scanner;
public class Book {
/**
* @param args
*/
private static int bookNum;
private static double price;
public Book(int bookNum,double price){
this.bookNum=bookNum;
this.price=price;
}
public static double MaxPrice(Book[] b){
double MaxPrice=Double.MIN_VALUE;
for(int i=0;i<b.length;i++){
if(b[i].bookNum*b[i].price>MaxPrice){
MaxPrice=b[i].bookNum*b[i].price;
}
}
return MaxPrice;
}
public static double MinPrice(Book[] b){
double MinPrice=Double.MAX_VALUE;
for(int i=0;i<b.length;i++){
if(b[i].bookNum*b[i].price<MinPrice){
MinPrice=b[i].bookNum*b[i].price;
}
}
return MinPrice;
}
public static void show() {
System.out.println("数量:"+bookNum+"\t单价:"+price);
}
public static void showAll(Book[] b) {
for(int i=0;i<b.length;i++) {
System.out.println("数量:"+b[i].bookNum+"\t单价:"+b[i].price);
}
}
public static void main(String[] args) {
Book b[]=new Book[2];
Scanner sc=new Scanner(System.in);
for(int i=0;i<b.length;i++){
System.out.println("请输入第"+(i+1)+"本书的单价:");
int num=sc.nextInt();
System.out.println("请输入第"+(i+1)+"本书的数量:");
double pri=sc.nextDouble();
b[i]=new Book(num,pri);
Book.show();
}
showAll(b);
System.out.println("最低价为:"+Book.MinPrice(b));
System.out.println("最高价为:"+Book.MaxPrice(b));
}
}
代码如图,主要的问题是,如何在new一个对象的前提下,对数组进行循环赋值,并且能够输出前一轮循环赋值的数据