0

我已经添加了我的整个代码来澄清,我的问题不是缺少一个“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>(4);

    // 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 i = 0; i < FutureValueArrayList.size(); i++)
      {

         System.out.print(FutureValueArrayList + "\n");

      }

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

}

我的 for 循环给了我这样的输出,粗体是当用户输入是以输入更多数据时,它应该在第二行而不是他们第一次输入数据时:

$100.00 2.0% 2 $2,450.64 $150.00 2.0% 2 $36,420.71

4

3 回答 3

3

您在哪里使用System.out.print()which 将打印一些没有分隔符的 1 行\n

使用System.out.println()which 将自动将\n分隔符放在字符串的末尾。

\n是一个新的行分隔符。

\t是制表符分隔符。

编辑:我认为您正在尝试标记每个输出。(因为第一个打印语句)

如果是这样,您不能使用 for 循环(技术上)。主要是因为循环不知道哪个标签去哪个元素/索引。

如果您知道哪个元素将存储哪些数据,只需手动标记它们

System.out.println("速率:" + FutureValueArrayList.get(i))

// i 是存储速率值的索引`

编辑:关于你的评论。

你需要调查Formatter

示例代码- 这将打印一个列宽为 20 个字符的表格。

Formatter formatter = new Formatter();
    System.out.println(formatter.format("%20s %20s %20s %20s %20s", "Title*", "Title*", "Title*", "Title*", "Title*"));

    for (int i = 0; i < 10; i++) {
        formatter = new Formatter();
        String rowData = "info" + i;
        System.out.println(formatter.format("%20s %20s %20s %20s %20s", rowData, rowData, rowData, rowData, rowData));
    }
于 2013-04-10T03:34:16.290 回答
1

您需要用于println()在新行中显示每个条目。它将打印您的条目,然后在末尾插入一个行分隔符以终止该行。

System.out.println(FutureValueArray);

否则,您可以修改print()方法参数以使其在新行上打印。像这样的东西: -

System.out.print(FutureValueArray + "\n");

请注意,\t表示水平制表符,而\n表示换行符。

于 2013-04-10T03:32:27.597 回答
1

采用

for (int i = 0; i < FutureValueArrayList.size(); i++)

      {

         String FutureValueArray = FutureValueArrayList.get(i);

         System.out.println(FutureValueArray + "\n");

      }

println 自动在末尾添加换行符

"/n" 也可以用作换行符

于 2013-04-10T03:35:56.593 回答