求助一道Java 作业题,用eclipse编写的,很急多谢
要求是写一个程序读入产品描述,输出产品数据例如以下outputDescriptionHereisasampletranscriptoftheoutputofthispro...
要求是写一个程序 读入产品描述,输出产品数据
例如以下output
Description
Here is a sample transcript of the output of this program:
Enter database filename: lab1_input.txt
Product Summary Report
------------------------------------------------------------
Title: The Shawshank Redemption (***)
Product Type: DVD
Price: 19.95
Title: The Dark Knight (***)
Product Type: DVD
Price: 19.95
Title: Casablanca (****)
Product Type: DVD
Price: 9.95
Title: The Girl With The Dragon Tattoo (***)
Product Type: Book
Price: 14.95
Title: Vertigo (**)
Product Type: DVD
Price: 9.95
Title: A Game of Thrones (**)
Product Type: Book
Price: 8.95
Total products in database: 6
Highest average score: 4
Highest average ranked product: Casablanca
Lowest average score: 2
Lowest average ranked product: Vertigo
具体要求是
1 让用户输入database的文件名
2 input 产品信息然后储存为6个array,一个是string(名字),一个是string(类型),一个是double(价格),后3个是int (分数)
3算出每个产品的平均分数
4 输出结果时每个产品名字后跟着的星星就是他的平均分数
5 输出总产品数(在input第一行), 最高平均分(以及产品名),最低平均分(以及产品名)
以下是input文件的格式
<number of products>
<product name>
<product type>
<product price>
<user 1 score>
<user 2 score>
<user 3 score>
<product name>
<product type>
<product price>
<user 1 score>
<user 2 score>
<user 3 score>
…
如有高手帮忙 感激不尽! 展开
例如以下output
Description
Here is a sample transcript of the output of this program:
Enter database filename: lab1_input.txt
Product Summary Report
------------------------------------------------------------
Title: The Shawshank Redemption (***)
Product Type: DVD
Price: 19.95
Title: The Dark Knight (***)
Product Type: DVD
Price: 19.95
Title: Casablanca (****)
Product Type: DVD
Price: 9.95
Title: The Girl With The Dragon Tattoo (***)
Product Type: Book
Price: 14.95
Title: Vertigo (**)
Product Type: DVD
Price: 9.95
Title: A Game of Thrones (**)
Product Type: Book
Price: 8.95
Total products in database: 6
Highest average score: 4
Highest average ranked product: Casablanca
Lowest average score: 2
Lowest average ranked product: Vertigo
具体要求是
1 让用户输入database的文件名
2 input 产品信息然后储存为6个array,一个是string(名字),一个是string(类型),一个是double(价格),后3个是int (分数)
3算出每个产品的平均分数
4 输出结果时每个产品名字后跟着的星星就是他的平均分数
5 输出总产品数(在input第一行), 最高平均分(以及产品名),最低平均分(以及产品名)
以下是input文件的格式
<number of products>
<product name>
<product type>
<product price>
<user 1 score>
<user 2 score>
<user 3 score>
<product name>
<product type>
<product price>
<user 1 score>
<user 2 score>
<user 3 score>
…
如有高手帮忙 感激不尽! 展开
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<count;i++){
fw.write("Title:"+ name[i]+"(");
for(int j =0;j<avescore[i];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个分数是什么意思
我就理解为 输入三次分数取平均数。
最后那个 最大 最小 的没写。你自己写个判断就好
希望采取
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<count;i++){
fw.write("Title:"+ name[i]+"(");
for(int j =0;j<avescore[i];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个分数是什么意思
我就理解为 输入三次分数取平均数。
最后那个 最大 最小 的没写。你自己写个判断就好
希望采取
追问
恩,他有个input的列子
6
The Shawshank Redemption
DVD
19.95
3
3
3
The Dark Knight
DVD
19.95
5
1
5
Casablanca
DVD
9.95
5
5
2
The Girl With The Dragon Tattoo
Book
14.95
5
1
3
Vertigo
DVD
9.95
1
5
1
A Game of Thrones
Book
8.95
2
5
1
他输出名字旁边的“***” 是指平均分数
就是3个score的平均值
多谢你的回复,我正在研究,很有启发!
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询