本人要用blueJ 编一个一年里下雨量小到大的月份排列 自己编得好乱啊,请大家帮帮忙看看怎么改,月份还要显出降雨量。。main程序:public class Rain
{
double [] monthlyAmount = new double[12];
public RainFall(){
}
public RainFall(double[] dataIn){
setAllRainFall(dataIn);
}
public void setRainFall(double amount, int index){
monthlyAmount [index] = amount;
}
public void setAllRainFall(double[] dataIn){
for(int i=0; i<12; ++i){
monthlyAmount[i] = dataIn[i];
}
}
public double getMonthValue(int index){
return monthlyAmount[index];
}
public double[] getAllData(){
return monthlyAmount;
}
public void bubblesort(){
for (int pass=1; pass < m.length; pass++){
for (int i=0; i < m.length - pass; i++){
if (m[i] > m[i+1]){
double temp = m[i];
m[i]= m[i+1];
m[i+1] = temp;
}
}
}
}另外的text部分:public class TestRain
{
public static void min(String [] args){
double [] myData = {2.0,5.0,4.0,7.0,5.0,9.0,3.0,5.0,2.0,1.0,11.0,0.0};
RainFall brandonRainFall = new RainFall(myData);
System.out.println();
}
}
{
double [] monthlyAmount = new double[12];
public RainFall(){
}
public RainFall(double[] dataIn){
setAllRainFall(dataIn);
}
public void setRainFall(double amount, int index){
monthlyAmount [index] = amount;
}
public void setAllRainFall(double[] dataIn){
for(int i=0; i<12; ++i){
monthlyAmount[i] = dataIn[i];
}
}
public double getMonthValue(int index){
return monthlyAmount[index];
}
public double[] getAllData(){
return monthlyAmount;
}
public void bubblesort(){
for (int pass=1; pass < m.length; pass++){
for (int i=0; i < m.length - pass; i++){
if (m[i] > m[i+1]){
double temp = m[i];
m[i]= m[i+1];
m[i+1] = temp;
}
}
}
}另外的text部分:public class TestRain
{
public static void min(String [] args){
double [] myData = {2.0,5.0,4.0,7.0,5.0,9.0,3.0,5.0,2.0,1.0,11.0,0.0};
RainFall brandonRainFall = new RainFall(myData);
System.out.println();
}
}
(value为降雨量,key为月份),然后根据降雨量来排序数组,就可以了。
至于排序的算法就随你遍了(冒泡、选择、快速排序都行)。
- -不明白呀。。什么是HashMap类没学过呀~~
public void bubblesort(){
double [] m = {2.0,5.0,4.0,7.0,5.0,9.0,3.0,5.0,2.0,1.0,11.0,0.0};//降雨量
int [] month = {1,2,3,4,5,6,7,8,9,10,11,12};//月份 for (int pass=1; pass < m.length; pass++){
for (int i=0; i < m.length - pass; i++){
if (m[i] > m[i+1]){
//降雨量交换
double temp = m[i];
m[i]= m[i+1];
m[i+1] = temp;
//交换月份
int tempMonth=month[i];
month[i]=month[i+1];
month[i+1]=tempMonth;
}
}
}
}
你试试吧
还是用HashMap方便啊