2013-04-10 19 views
1

現在的問題是,如果你運行我的程序出來,我應該有一個字符串輸出用戶輸入的月度投資,利率和年,它應該打印在一行和下一行,之後應該是相同但不同的價值,如果用戶進入他想輸入月度投資,利率和年份的另一個不同的值。我的箴言是它打印一切只是一個像前例如:$ 400 2.0%3 $ 500 3.0%2 < ---這從500美元開始應該在下一行不是第一個入口?線程main在調用println()之前停止?

System.out.print("Inv/Mo.\tRate\tYears\tFuture Value\n"); 
    for (int j = 0; j < FutureValueArrayList.size(); j++) 
    { 
     String myArrayList = FutureValueArrayList.get(j); 
     System.out.print(ArrayList + "\t"); 
     System.out.println(); 

    } 

我也得到這個錯誤,我似乎無法推測的方式來解決它「主線程停止前調用println()一樣」,這是印刷(ArrayList的+「\ t」的)之後的println ;在我的循環。

import java.util.*; 
import java.text.*; 

public class FutureValueApp 
{ 
public static void main(String[] args) 
{ 
    // display a welcome message 
    System.out.println("Welcome to the Future Value Calculator"); 
    System.out.println(); 

    ArrayList<String> FutureValueArrayList = new ArrayList<String>(); 

    // perform 1 or more calculations 
    Scanner sc = new Scanner(System.in); 
    String choice = "y"; 
    while (choice.equalsIgnoreCase("y")) 
    { 

     // get the input from the user 
     System.out.println("DATA ENTRY"); 
     double monthlyInvestment = getDoubleWithinRange(sc, 
      "Enter monthly investment: ", 0, 1000); 
     double interestRate = getDoubleWithinRange(sc, 
      "Enter yearly interest rate: ", 0, 30); 
     int years = getIntWithinRange(sc, 
      "Enter number of years: ", 0, 100); 

     // calculate the future value 
     double monthlyInterestRate = interestRate/12/100; 
     int months = years * 12; 
     double futureValue = calculateFutureValue(
      monthlyInvestment, monthlyInterestRate, months); 

     // get the currency and percent formatters 
     NumberFormat currency = NumberFormat.getCurrencyInstance(); 
     NumberFormat percent = NumberFormat.getPercentInstance(); 
     percent.setMinimumFractionDigits(1); 

     // format the result as a single string 
     String results = 
       "Monthly investment:\t" 
        + currency.format(monthlyInvestment) + "\n" 
      + "Yearly interest rate:\t" 
        + percent.format(interestRate/100) + "\n" 
      + "Number of years:\t" 
        + years + "\n" 
      + "Future value:\t\t" 
        + currency.format(futureValue) + "\n"; 

     // print the results 
     System.out.println(); 
     System.out.println("FORMATTED RESULTS"); 
     System.out.println(results); 

     String monthlyInvestmentFormat = currency.format(monthlyInvestment); 
     String interestRateFormat = percent.format(interestRate/100); 
     String futureValueFormat = currency.format(futureValue); 

     FutureValueArrayList.add(monthlyInvestmentFormat); 
     FutureValueArrayList.add(interestRateFormat); 
     FutureValueArrayList.add(Integer.toString(years)); 
     FutureValueArrayList.add(futureValueFormat); 




     // see if the user wants to continue 
     System.out.print("Continue? (y/n): "); 
     choice = sc.next(); 

     System.out.println(); 
    } 

    System.out.print("Inv/Mo.\tRate\tYears\tFuture Value\n"); 
    for (int j = 0; j < FutureValueArrayList.size(); j++) 
    { 
     String ArrayList = FutureValueArrayList.get(j); 
     System.out.print(ArrayList + "\t"); 
     System.out.println(); 

    } 
    System.out.println(); 
} 

public static double getDouble(Scanner sc, String prompt) 
{ 
    boolean isValid = false; 
    double d = 0; 
    while (isValid == false) 
    { 
     System.out.print(prompt); 
     if (sc.hasNextDouble()) 
     { 
      d = sc.nextDouble(); 
      isValid = true; 
     } 
     else 
     { 
      System.out.println("Error! Invalid decimal value. Try again."); 
     } 
     sc.nextLine(); // discard any other data entered on the line 
    } 
    return d; 
} 

public static double getDoubleWithinRange(Scanner sc, String prompt, 
double min, double max) 
{ 
    double d = 0; 
    boolean isValid = false; 
    while (isValid == false) 
    { 
     d = getDouble(sc, prompt); 
     if (d <= min) 
      System.out.println(
       "Error! Number must be greater than " + min + "."); 
     else if (d >= max) 
      System.out.println(
       "Error! Number must be less than " + max + "."); 
     else 
      isValid = true; 
    } 
    return d; 
} 

public static int getInt(Scanner sc, String prompt) 
{ 
    boolean isValidInt = false; 
    int i = 0; 
    while (isValidInt == false) 
    { 
     System.out.print(prompt); 
     if (sc.hasNextInt()) 
     { 
      i = sc.nextInt(); 
      isValidInt = true; 
     } 
     else 
     { 
      System.out.println("Error! Invalid integer value. Try again."); 
     } 
     sc.nextLine(); // discard any other data entered on the line 
    } 
    return i; 
} 

public static int getIntWithinRange(Scanner sc, String prompt, 
int min, int max) 
{ 
    int i = 0; 
    boolean isValid = false; 
    while (isValid == false) 
    { 
     i = getInt(sc, prompt); 
     if (i <= min) 
      System.out.println(
       "Error! Number must be greater than " + min + "."); 
     else if (i >= max) 
      System.out.println(
       "Error! Number must be less than " + max + "."); 
     else 
      isValid = true; 
    } 
    return i; 
} 

public static double calculateFutureValue(double monthlyInvestment, 
double monthlyInterestRate, int months) 
{ 
    double futureValue = 0; 
    for (int i = 1; i <= months; i++) 
    { 
     futureValue = 
      (futureValue + monthlyInvestment) * 
      (1 + monthlyInterestRate); 
    } 
    return futureValue; 
} 
} 
+0

你有錯誤的堆棧跟蹤? PS:在本地類之後命名變量確實是......非常混亂! – christopher 2013-04-10 19:20:06

+0

另外,作爲編碼清晰度約定,變量名的第一個字符應該是小寫字母。命名一個字符串「ArrayList」是荒謬的混淆。 – Aurand 2013-04-10 19:21:13

+0

@nickecarlo它不會引發任何語法錯誤。剛剛嘗試過。 – christopher 2013-04-10 19:21:48

回答

1

你需要像這樣的代碼

System.out.print("Inv/Mo.\tRate\tYears\tFuture Value\n"); 
for (int j = 0; j < FutureValueArrayList.size(); j++) 
{ 
    String ArrayList = FutureValueArrayList.get(j); 
    System.out.print(ArrayList + "\t"); 
    if((j+1)%4 == 0){ 
     System.out.println(); 
    } 

} 
System.out.println(); 

注意使用(J + 1)%4招。

注意:這只是一個格式化黑客。這樣做的正確方法是創建一個帶有4個參數(rate,sum,time,futureValue)的FutureValue對象,並重寫toString()方法以正確打印所需格式的對象。 我鼓勵你試試這個。

方法2:

/*FutureValueArrayList.add(monthlyInvestmentFormat); 
      FutureValueArrayList.add(interestRateFormat); 
      FutureValueArrayList.add(Integer.toString(years)); 
      FutureValueArrayList.add(futureValueFormat);*/ 

      FutureValueArrayList.add(monthlyInvestmentFormat+"\t"+interestRateFormat+"\t"+Integer.toString(years)+"\t"+futureValueFormat); 

現在有沒有必要以任何方式更改您的打印代碼。

+0

是否有另一種方式做它是類似於我的循環? – user2264244 2013-04-10 19:54:13

+0

我可以建議另一種方法。每條線有四個FutureValueArrayList.add()。而不是隻有一個FutureValueArrayList.add(組合字符串)。將更新我的答案來證明這一點。 – prashant 2013-04-10 19:57:02

+0

非常感謝! – user2264244 2013-04-10 20:05:26

相關問題