求高手给点思路。谢谢
/*** 要求输出格式如下:
* 北京 上海 广州 南京 武汉
* 洗衣机 700 350 800 185 0
* 彩电 150 100 300 0 500
* 空调 0 160 450 650 150
* 冰箱 250 600 0 900 300
* @author ww
*
*/
//以下是给好的代码,求高手指导完成
public class myExam2 {
private String[][] str_data1 = new String[][]{
{"上海","彩电","100"},
{"上海","洗衣机","350"},
{"上海","空调","160"},
{"上海","冰箱","600"},
{"北京","洗衣机","700"},
{"北京","彩电","150"},
{"北京","冰箱","250"},
{"广州","洗衣机","800"},
{"广州","彩电","300"},
{"广州","空调","450"},
{"南京","洗衣机","185"},
{"南京","空调","650"},
{"南京","冰箱","900"},
{"武汉","彩电","500"},
{"武汉","空调","150"},
{"武汉","冰箱","300"}
};
private String[][] str_data2 = null;
public myExam2(){
if(str_data2 != null){
for(int i=0;i<str_data2.length;i++){
for(int j =0;j<str_data2.length;j++){
System.out.print(str_data2[i][j]+ "\t");
}
System.out.println();
}
}
}
public static void main(String args[])
{
new myExam2();
}
} --------------------编程问答-------------------- 就是用双层循环把一维字符数组换个角度打印出来而已 --------------------编程问答-------------------- So
你的问题是? --------------------编程问答-------------------- 怎么感觉像是一个填空、改错题呢!
package com.com.com;
public class myExam2 {
private String[][] str_data1 = new String[][] { { "上海", "彩电", "100" },
{ "上海", "洗衣机", "350" }, { "上海", "空调", "160" },
{ "上海", "冰箱", "600" }, { "北京", "洗衣机", "700" },
{ "北京", "彩电", "150" }, { "北京", "冰箱", "250" },
{ "广州", "洗衣机", "800" }, { "广州", "彩电", "300" },
{ "广州", "空调", "450" }, { "南京", "洗衣机", "185" },
{ "南京", "空调", "650" }, { "南京", "冰箱", "900" },
{ "武汉", "彩电", "500" }, { "武汉", "空调", "150" }, { "武汉", "冰箱", "300" } };
private String[][] str_data2 = null;
public myExam2() {
str_data2 = str_data1;//这句是添加上的
if (str_data2 != null) {
for (int i = 0; i < str_data2.length; i++) {
for (int j = 0; j < str_data2[i].length; j++) {//这里str_data2.length是错误的,运行会报数组下标越界错误。应该为str_data2[i].length
System.out.print(str_data2[i][j] + "\t");
}
System.out.println();
}
}
}
public static void main(String args[]) {
new myExam2();
}
}
--------------------编程问答-------------------- 糊涂了,审题错误了,
是输出
北京 上海 广州 南京 武汉
洗衣机 700 350 800 185 0
彩电 150 100 300 0 500
空调 0 160 450 650 150
冰箱 250 600 0 900 300
再看题 --------------------编程问答-------------------- 什么烂程序结构,之前也看到一个类似的,
http://topic.csdn.net/u/20120610/22/3b267891-d485-4c2e-924f-bce5676982ec.html
出题者脑子进水,不把新人带入好的程序结构思路,反而让新人学习他的烂思路,怪不得人才凋落
public myExam2(){--------------------编程问答--------------------
/* 追加开始 */
//因为输出跟str_data1的某些项目顺序不一致,所以写死,
//谁知道出题的目的是什么,又不是排序,又不说明规律
//如果是只是调换头两个的顺序,那么题目要写清楚,否则无视出题者思路,没事找抽的
//没有import集合类,所以此处没使用集合
String[] cities = {"北京", "上海", "广州", "南京", "武汉"};
String[] price = {"", "0", "0", "0", "0", "0"};
String[] items = {"洗衣机", "彩电", "空调", "冰箱"};
//str_data2 = new String[items.length+1][cities.length+1];本来应该这样的,但是因为出题者的结构是两维维数相同,为了避免异常,所以
int max = (items.length+1>cities.length+1 ? items.length+1, cities.length+1);
str_data2 = new String[max][max];
System.arraycopy(cities, 0, str_data2[0], 1, cities.length);
for (int i=1; i<items.length+1; i++) {
System.arraycopy(price, 0, str_data2[i], 1, price.length); //初始化价格为0
}
String last = "";
for (int i=0, j=0, k=0; i<str_data1.length; i++) {
if (!last.equals(str_data1[i][0])) {
for (j=0; j<cities.length; j++) {
if (cities[j].equals(str_data1[i][0])) break;
}
if (j == cities.length) continue; //输出中没这个城市
last = str_data1[i][0];
}
for (k=0; k<items.length; k++) {
if (items[k].equals(str_data1[i][1])) break;
}
if (k == items.length) continue; //输出中没有这个产品
if (str_data2[k+1][0].equals("")) str_data2[k+1][0] = items[k];
str_data2[k+1][j+1] = str_data1[i][2];
}
/* 追加结束 */
if(str_data2 != null){
for(int i=0;i<str_data2.length;i++){
for(int j =0;j<str_data2.length;j++){ //你看出题者的思路,二维数组还默认两维维数是一样,那一开始直接写死数组大小更好
System.out.print(str_data2[i][j]+ "\t");
}
System.out.println();
}
}
}
要怒=。= --------------------编程问答--------------------
我也看见你回复的类似的题, 确实觉得出题的人有点生搬硬套的感觉, 这道题也是写死,就为了排版
补充:Java , Java SE