gjmding
2018-11-28 05:09
采纳率: 50%
浏览 6.4k
已采纳

Java怎么用for循环给数组赋值?

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一个对象的前提下,对数组进行循环赋值,并且能够输出前一轮循环赋值的数据

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • blownewbee 2018-11-28 05:20
    已采纳

    private static int bookNum;
    private static double price;
    这些不能是static的
    包括 public static void show()
    你首先应该把主程序和Book类分开写

    package les9;
    
    import java.util.Scanner;
    
    class Book {
    
    /**
     * @param args
     */
    private int bookNum;
    private 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 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 class Program {
    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);
            b[i].show();
        }
        Book.showAll(b);
        System.out.println("最低价为:"+Book.MinPrice(b));
        System.out.println("最高价为:"+Book.MaxPrice(b));
    }
    }
    

    将Book.java重命名为Program.java

    点赞 评论

相关推荐 更多相似问题