1

现在的问题是,如果你在输出时运行我的程序,我应该有一个字符串输出用户输入的每月投资、利率和年份,它应该在一行和下一行打印它,之后应该是相同的,但是如果用户输入了不同的值,他想为每月投资、利率和年份输入另一个不同的值。我的可能是它把所有东西都打印成一个像 ex:$400 2.0% 3 $500 3.0% 2 <--- 这从 $500 开始应该在下一行而不是第一个条目的第一个?

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() 之前停止”这是 print(ArrayList + "\t") 之后的 println;在我的 for 循环中。

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;
 }
}
4

1 回答 1

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 个参数(速率、总和、时间、futureValue)的 FutureValue 对象,并覆盖 toString() 方法以便以所需格式正确打印对象。我鼓励你试试这个。

方法二:

/*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);

现在无需以任何方式更改您的打印代码。

于 2013-04-10T19:35:06.277 回答