2017-04-04 57 views
0

我需要按升序排序我的Money對象數組,但我得到3個編譯器錯誤。排序我的數組對象時鍵入錯誤

TestMoney.java:44: error: bad operand types for binary operator '<' 
      if (list[j] < list[min]) { 
         ^
    first type: Money 
    second type: Money 
TestMoney.java:50: error: incompatible types: Money cannot be converted to int 
      final int temp = list[i]; 
           ^
TestMoney.java:52: error: incompatible types: int cannot be converted to Money 
      list[min] = temp; 
         ^

class TestMoney 
{ 
    public static void main(String [] args) 
    { 
     Money[] list = new Money[15]; 
     for(int i =0; i<15; i++) 
     { 
     int dollar =(int) (Math.random() * 30 + 1); 
     int cent = (int) (Math.random() * 100); 
     list[i] = new Money(dollar, cent); 

     } 
     sortArray(list); 
     printArray(list); 

    } 
    public static void printArray(Money[] list) 
    { 
     for(int i =0; i <list.length; i++) 
     { 

     if(i%10 ==0) 
     { 
      System.out.println(); 
     } 

     System.out.print(" " + list[i]); 
    } 

    } 
    public static void sortArray(Money[] list) 
    { 
    int min; 
    for (int i = 0; i < list.length; i++) { 
     // Assume first element is min 
     min = i; 
     for (int j = i + 1; j < list.length; j++) { 
      if (list[j] < list[min]) { 
       min = j; 

      } 
     } 
     if (min != i) { 
      final int temp = list[i]; 
      list[i] = list[min]; 
      list[min] = temp; 
     } 
     System.out.println(list[i]);// I print the in ascending order 
    } 

    } 
} 

class Money 
{ 
    private int dol; 
    private int cen; 

    Money() 
    { 
     dol = 0; 
     cen = 00; 
    } 
    Money(int dol,int cen) 
    { 
     int remainder = cen % 100; 
     int divisor = cen/100; 
     this.dol = dol+ divisor; 
     this.cen = remainder; 
    } 
    public int getDollors(int dol) 
    { 
     return dol; 
    } 
    public int getCents(int cen) 
    { 
     return cen; 
    } 
    public void setDollors(int d) 
    { 
     dol = d; 
    } 
    public void setCents(int c) 
    { 
     cen = c; 
    } 
    public Money addMoney(Money m) 
    { 
     int d = this.dol + m.dol; 
     int c = this.cen + m.cen; 
     return new Money(d, c); 
    } 
    public int compareTo(Money m) 
    { 

    if(this.dol<m.dol && this.cen<m.cen) 
    return -1; 
    else if(m.dol<this.dol && m.cen<this.cen) 
    return 1; 
    return 0; 


    } 
    public Money subtract(Money m) 
    { 
     int cents1 = this.dol*100 + this.cen; 
     int cents2 = m.dol *100 + m.cen; 
     int cents = cents1 -cents2; 
     return new Money(cen/100,cen%100); 
    } 


    public String toString() 
    { 

     return String.format("$%d.%02d", this.dol,this.cen); 

    } 
} 
+0

需要實現的比較級,以使<評價兩點塊錢的對象,看看這個例子 - https://www.tutorialspoint.com/java/java_using_comparator.htm –

+3

閱讀您發佈的代碼。 'int temp = list [i]',當list [i]'是'Money'時,顯然不起作用。 'int'與'Money'不一樣,所以它們不是賦值兼容的。你甚至讀過錯誤信息嗎? –

+1

你不能在一個對象('Money')和一個值('int')之間進行轉換。你期望做什麼? – David

回答

1

那是因爲你正試圖在兩個Money對象與僅適用於數字<運營商相比,你需要replce如下:

if (list[j] < list[min]) {

if (list[j].getDollors() < list[min].getDollors() 
    || (list[j].getDollors() == list[min].getDollors() && list[j].getCents() < list[min].getCents())) { 

此外,你不需要你的getters接受一個參數,他們可以是零參數方法,因爲他們只是返回一個值。

+0

非常感謝! – CrazyGal

0

的另一種方式,也許,是用比較接口,就像這樣:

public class Money implements Comparator<Money>{ 
int n; //as an example, compare two object by them "n" variable 

@Override 
public int compare(Money o1, Money o2) { 
    int result; 
    if (o1.n > o2.n) 
     result = 1; //greater than case 
    else 
     result = o1.n < o2.n ? -1 // less than case 
       : 0; // equal case 
    return result; 
} 

}

在比較方法,你可以使用GT-LS-EQ的標準基礎上的金額。 最後做:

for (int j = i + 1; j < list.length; j++) { 
     if (list[j].compare(list[min]) < 0) { //you ask if is greater, less, 
               //or equal than 0 
      min = j; 
     } 
} 
+0

非常感謝!我很欣賞這 – CrazyGal

+0

我給你一個大拇指..但它不會顯示給你,因爲我在這裏是新的 – CrazyGal

+0

沒關係!我很高興我能幫助你 –