gjmding 2018-11-28 05:09 采纳率: 50%

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

}

• 写回答

1条回答默认 最新

• threenewbee 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

本回答被题主选为最佳回答 , 对您是否有帮助呢?
评论

悬赏问题

• ¥15 微信公众号如何开发网页
• ¥15 h3.6m 人类行为预测论文复现
• ¥50 wordpress项目注册报失败刷新后其实是成功状态，请求排查原因
• ¥20 linxu服务器僵尸进程不释放，代码如何修改？
• ¥15 pycharm激活不成功
• ¥40 如果update 一个列名为参数的value
• ¥15 基于51单片机的水位检测系统设计中LCD1602一直不显示
• ¥15 OCS2安装出现问题，请大家给点意见
• ¥15 ros小车启动launch文件报错
• ¥15 vs2015到期想登陆但是登陆不上