求助一道Java 作业题,用eclipse编写的,很急多谢

2025-06-21 14:31:34
推荐回答(1个)
回答1:

为什么会有三个数组存储分数呢????
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

/**
* Created by IntelliJ IDEA.
* User: guangzhi
* Date: 11-9-30
* Time: 下午3:15
* To change this template use File | Settings | File Templates.
*/
public class InputOutput {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter your database filename:");
String filename = scanner.next();

String[] name = new String[10];
String[] type = new String[10];
double[] price = new double[10];
int[] score1 = new int[10];
int[] score2 = new int[10];
int[] score3 = new int[10];
int[] avescore = new int[10];

int count = 0;

for(int i = 0;i < 10; i++){
System.out.println("Enter your product's name;");
name[i] = scanner.next();
System.out.println("Enter your product's type:");
type[i] = scanner.next();
System.out.println("Enter your product's price:");
price[i] = scanner.nextDouble();
System.out.println("Enter your product's score1:");
score1[i] = scanner.nextInt();
System.out.println("Enter your product's score2:");
score2[i] = scanner.nextInt();
System.out.println("Enter your product's score3:");
score3[i] = scanner.nextInt();

avescore[i] = (score1[i]+score2[i]+score3[i])/3;
count = count+1;
System.out.println("Do You want to go on?y/n");
String bool = scanner.next();
if(bool.equals("y")){
continue;
}else{
break;
}
}

FileWriter fw ;
try {
fw = new FileWriter("d:/"+filename);
for(int i =0;i fw.write("Title:"+ name[i]+"(");
for(int j =0;j fw.write("*");
}
fw.write(")\r\n");
fw.write("Product Type:"+type[i]+"\r\n");
fw.write("Price:"+price[i]+"\r\n");
}
fw.write("-------------------------------------\r\n");
fw.write("Totoal product in database:"+count);
fw.close();
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}

}
}

测试过程:
Please enter your database filename:
product.txt
Enter your product's name;
1
Enter your product's type:
1
Enter your product's price:
1
Enter your product's score1:
2
Enter your product's score2:
3
Enter your product's score3:
4
Do You want to go on?y/n
y
Enter your product's name;
2
Enter your product's type:
2
Enter your product's price:
2
Enter your product's score1:
6
Enter your product's score2:
7
Enter your product's score3:
8
Do You want to go on?y/n
n

Process finished with exit code 0

测试结果:
Title:1(***)
Product Type:1
Price:1.0
Title:2(*******)
Product Type:2
Price:2.0
-------------------------------------
Totoal product in database:2

因为不知道你那个3个分数是什么意思
我就理解为 输入三次分数取平均数。

最后那个 最大 最小 的没写。你自己写个判断就好
希望采取