2013-09-25 21 views
0

我試圖對涉及變量的公式進行簡單的數學計算。但是,編譯器會彈出錯誤提示變量類型不匹配。我曾嘗試鑄造和更改變量類型,但它們不起作用。如何解決這個問題而不破壞我的代碼的基本格式。Java中的Casting或變量類型錯誤

我對Java沒有經驗,所以任何指針都會有所幫助。

這是代碼。這是一個錢轉換程序。錯誤在代碼的第二部分,其他部分。

import java.util.Scanner; 
public class MConvert 
{ 
    public static void main (String[] args) 
    { 
    int penny, nickel, dime, quarter, halfDollar, dollar, fiveDollar, tenDollar,  twentyDollar, fiftyDollar, hundredDollar; 
    //can sub $ sign for dollar in variable, convention otherwise 
    double totalMoney; 
    Scanner scan = new Scanner (System.in); 
     System.out.println ("Are you converting to the total? If so, type true. \nIf you are converting from the total, then type false."); 
     boolean TotalorNot = true; 
     TotalorNot = scan.nextBoolean(); 


     if (TotalorNot) { 

     System.out.println ("Type in the number of one-hundred dollar bills."); 
     hundredDollar = scan.nextInt(); 
     System.out.println ("Type in the number of fifty dollar bills."); 
     fiftyDollar = scan.nextInt(); 
     System.out.println ("Type in the number of twenty dollar bills."); 
     twentyDollar = scan.nextInt(); 
     System.out.println ("Type in the number of ten dollar bills."); 
     tenDollar = scan.nextInt(); 
     System.out.println ("Type in the number of five dollar bills."); 
     fiveDollar = scan.nextInt(); 
     System.out.println ("Type in the number of one dollar bills or coins."); 
     dollar = scan.nextInt(); 
     System.out.println ("Type in the number of half-dollar coins."); 
     halfDollar = scan.nextInt(); 
     System.out.println ("Type in the number of quarter-dollar coins."); 
     quarter = scan.nextInt(); 
     System.out.println ("Type in the number of dimes."); 
     dime = scan.nextInt(); 
     System.out.println ("Type in the number of nickels."); 
     nickel = scan.nextInt(); 
     System.out.println ("Type in the number of pennies coins."); 
     penny = scan.nextInt(); 
     totalMoney = (hundredDollar * 100) + (fiftyDollar * 50) + (twentyDollar * 20) + (tenDollar * 10) + (fiveDollar * 5) + (dollar * 1) + ((double)halfDollar * 0.5) + ((double)quarter * 0.25) + ((double)dime * 0.1) + ((double)nickel * 0.05) + ((double)penny * 0.01); 
    System.out.println ("Here is total monetary value of the bills and coins you entered: " + totalMoney); 




} else { 

     System.out.println ("Type in the total monetary value:"); 
     totalMoney = scan.nextDouble(); 
     hundredDollar = ((int)totalMoney/100); 
     fiftyDollar = ((int)totalMoney - (hundredDollar * 100))/50; 
     twentyDollar = ((int)totalMoney - (fiftyDollar * 50))/20; 
     tenDollar = ((int)totalMoney - (twentyDollar * 20))/10; 
     fiveDollar = ((int)totalMoney - (tenDollar * 10))/5; 
     dollar = ((int)totalMoney - (fiveDollar * 5))/1; 
     (double) halfDollar = (totalMoney - (dollar * 1))/0.5; 
     quarter = ((int)totalMoney - (halfDollar * 0.5))/0.25; 
     dime = ((int)totalMoney - (quarter * 0.25))/0.1; 
     nickel = ((int)totalMoney - (dime * 0.1))/0.05; 
     penny = ((int)totalMoney - (nickel * 0.05))/0.01; 

     System.out.println (hundredDollar + " hundred dollar bills"); 
     System.out.println (fiftyDollar + " fifty dollar bills"); 
     System.out.println (twentyDollar + " twenty dollar bills"); 
     System.out.println (tenDollar + " ten dollar bills"); 
     System.out.println (fiveDollar + " five dollar bills"); 
     System.out.println (dollar + " one dollar bills or coins"); 
     System.out.println (halfDollar + " half-dollar coins"); 
     System.out.println (quarter + " quarter-dollar coins"); 
     System.out.println (dime + " dimes"); 
     System.out.println (nickel + " nickel"); 
     System.out.println (penny + " penny"); 

    } 


} 

}

+1

使用會話like ... hundredDollar =(int)(totalMoney/100); –

回答

1
(double) halfDollar = (totalMoney - (dollar * 1))/0.5; 

是完全錯誤的,先刪除這條指令。

還要更改您的代碼與下面的行。

quarter = (int)((totalMoney - (halfDollar * 0.5))/0.25); 
dime = (int) ((totalMoney - (quarter * 0.25))/0.1); 
nickel =(int)((totalMoney - (dime * 0.1))/0.05); 
penny = (int)((totalMoney - (nickel * 0.05))/0.01); 

你還需要改進你的代碼的邏輯,我可以清楚地看到它不符合你想要實現的。

+0

您如何建議更改邏輯以達到輸入貨幣金額並輸出達到該金額所需的最少數量的賬單/硬幣的目標?感謝您給予的任何幫助。這個例子確實幫了我,但我仍然得到錯誤的結果。 – user2813879

0

在該線 ((int)的totalMoney - (tenDollar * 10))/ 5;

投適用於只是totalMoney

投你必須這樣做

(int)(totalMoney - (tenDollar * 10))/5); 
+0

非常感謝。然而,價值觀並不正確。這個項目的重點在於編程中將貨幣數量轉換爲最少數量的賬單和硬幣。我的問題可能很有問題。你有什麼想法?如果用戶輸入25,則爲 – user2813879

+0

。50美元,該計劃必須說20美元= 1美元,5美元 - 1美元,半美元= 1;這是你想要的嗎 –

+0

是的。它應該顯示爲零。 – user2813879

0

的問題是在下面的行整個表達式的值:

(double) halfDollar = (totalMoney - (dollar * 1))/0.5; 

是什麼這裏的(double)的含義是什麼?您不能將變量賦值的左側(變量本身)轉換爲double。該變量被聲明爲一個int,並且不能被改變。決不。

此行編譯(但可以是針對語義不正確的):

halfDollar = (int) ((totalMoney - (dollar * 1))/0.5); 

除此之外:我不認爲,該程序的功能,你想要它做的事情......

+0

非常感謝。然而,價值觀並不正確。這個項目的重點在於編程中將貨幣數量轉換爲最少數量的賬單和硬幣。我的問題可能很有問題。你有什麼想法? – user2813879

0
(double) halfDollar = (totalMoney - (dollar * 1))/0.5; 

你想通過halfDollar上的雙投達到什麼目的?先刪除。我花了5分鐘才知道爲什麼你的代碼中有這麼多紅線。

接下來,你可以做這樣一個簡單的例子: -

halfDollar = (int)((totalMoney - (dollar * 1))/0.5); 

但它不是明智的投雙重價值爲int,因爲你可能會失去大量的十進制數據的。爲什麼不簡單地把所有penny, nickel, dime, quarter, halfDollar, dollar, fiveDollar, tenDollar,twentyDollar, fiftyDollar, hundredDollar;當作double本身?

+0

非常感謝。然而,價值觀並不正確。這個項目的重點在於編程中將貨幣數量轉換爲最少數量的賬單和硬幣。我的問題可能很有問題。你有什麼想法? – user2813879

+0

@ user2813879 - 你可以給一個樣本I/O。只要說我給虛假,給100作爲'totalMoney'。您希望所有美元價值的產出是多少? – SudoRahul

+0

在這種情況下,我預計可能的票據/硬幣數量最少,因此,它應該輸出1百美元的賬單,就是這樣。 – user2813879