2015-10-11 34 views
-3

所以我有一個大學的鍛鍊完成,我相信我接近它,但迄今爲止。我不是要求一個具體的答案,我很想知道如何「覆蓋變量?」 (不知道這是否是這種行爲的正確名稱)。程序奇怪的輸出,太愚蠢瞭解決:c

這裏有一個問題:http://pastebin.com/riDYS39D

,我有是,我不知道如何改寫這是在if語句創建可變數據的問題。

和代碼,任何幫助是非常歡迎

import java.util.Scanner; 



public static void main(String[] args) { 

    Scanner input = new Scanner(System.in); 
    double grossPaid, allowance1, allowance2, totalTaxPaid, netPay, grossAfterAllowance; 
    double taxSaved; 
    final double GROSSRATIO; 
    String idNumber, name, address, strln; 
    char maritalStatus; 
    allowance1 = 25000; 
    allowance2 = 20000; 
    GROSSRATIO = 500000; 

    taxSaved = 0.00; 




    /* trash 
    grossAfterAllowance = grossPaid - allowanceGiven; 
    totalTaxPaid = grossAfterAllowance - netPay; 
    */ 


    System.out.println("Enter your employee identification number: "); 
    idNumber = input.nextLine(); 

    System.out.println("Enter your name: "); 
    name = input.nextLine(); 

    System.out.println("Enter your address: "); 
    address = input.nextLine(); 

    System.out.println("Enter your marital status: "); 
    strln = input.next(); 
    maritalStatus = strln.charAt(0); 
     if (maritalStatus == 'S')                           
      taxSaved = 0.20; 

     if (maritalStatus == 'M') 
      taxSaved = 0.23; 





    System.out.println("Enter your gross payment: "); 
    grossPaid = input.nextDouble(); 
     if (grossPaid < GROSSRATIO) 
     netPay = (grossPaid - allowance1) * taxSaved; 

     if (grossPaid >= GROSSRATIO) 
     netPay = (grossPaid - allowance2) * taxSaved; 




    System.out.println(maritalStatus); 
    netPay = (grossPaid * maritalStatus); 
    System.out.println("Name: " + name); 
    System.out.println("ID: " + idNumber); 
    System.out.println("Address: " + address); 
    System.out.println("Marital Status: " + strln); 
    System.out.println("Gross Payment: " + grossPaid); 
    System.out.println("Net pay: " + netPay); 



    } 

}

+0

是變量taxSaved? –

+0

@domfarr是先生 – thedumbone

+0

umm ..但taxSaved不是在if語句中創建的? – eis

回答

1

考慮您的第一個輸入變量idNumber,你這是怎麼重寫一個變量:

String idNumber = ""; 
System.out.println("Enter your employee identification number: "); 
idNumber = input.nextLine(); 
System.out.println("ID Number: " + idNumber); 

System.out.println("Enter your updated employee identification number: "); 
idNumber = input.nextLine(); 
System.out.println("Updated ID Number: " + idNumber); 
+0

您沒有閱讀我的問題,「我不知道如何覆蓋if語句中創建的變量中的數據。」即taxSaved – thedumbone

+0

嗯......它不是在if語句中創建的。它被分配了一個值,我想你仍然可以用你想要的任何值覆蓋它,這有什麼問題? –

0

你說「任何幫助總是受歡迎的」,所以這裏是我對你的代碼的分析。

這是一般的做法是聲明變量接近其使用成爲可能,最常見的是第一次分配時表示,例如:

String address = input.nextLine(); 

婚姻狀況應該是小寫's''m'。您應該使用nextLine()並驗證它,例如

char maritalStatus; 
do { 
    System.out.println("Enter your marital status (s/m):"); 
    String line = input.nextLine(); 
    maritalStatus = (line.length() == 1 ? line.charAt(0) : '?'); 
} while (maritalStatus != 's' && maritalStatus != 'm'); 

GROSSRATIO應該是50000,不500000

你對使用常量不一致。

  • GROSSRATIO是大寫和最終的。完善!
  • allowance1和​​是小寫的而不是最終的。
  • 0.200.23未被命名爲常量。

maritalStatuschar,所以這行編譯,但絕對不是你想要的,是你的「怪輸出」的原因。請刪除該行:

netPay = (grossPaid * maritalStatus); 

您也正在打印maritalStatus本身沒有標籤。去掉它。

你有兩個if語句分配netPay是相反的,但編譯器沒有看到,所以netPay沒有明確分配。只需將第二個if聲明更改爲else即可。

totalTaxPaidgrossAfterAllowance未被使用。刪除它們。

netPay的計算實際上是計算totalTax

您不打印所有需要的輸出。

+0

好吧,所以我改變了一些東西,我不能使用'做,而'等等,因爲我們還沒有講授它:v,它現在似乎正在工作http://pastebin.com/RvHDsX28謝謝! – thedumbone