Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我的数组列表只输出一行?_Java - Fatal编程技术网

Java 我的数组列表只输出一行?

Java 我的数组列表只输出一行?,java,Java,我添加了我的全部代码来澄清,我的问题不是我缺少了一个“println”,就像许多人在下面的代码中感谢地建议的那样我的问题我要求用户输入月投资、年利率和年数,然后我问他们是否想继续添加另一个月投资、年利率,几年后,直到他们说不,之后我想展示它。问题是,每当他们对输入更多数据说“是”时,它就应该在程序结束时的第二行中显示该日期,或者说“不”继续,而只是在一行中显示所有内容 import java.util.*; import java.text.*; public class FutureValu

我添加了我的全部代码来澄清,我的问题不是我缺少了一个“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;
}
import java.util.*;
导入java.text.*;
公共类FutureValueApp
{
公共静态void main(字符串[]args)
{
//显示欢迎信息
System.out.println(“欢迎使用未来值计算器”);
System.out.println();
ArrayList FutureValueArrayList=新的ArrayList(4);
//执行一次或多次计算
扫描仪sc=新的扫描仪(System.in);
字符串选择=“y”;
while(choice.equalsIgnoreCase(“y”))
{
//从用户那里获取输入
System.out.println(“数据输入”);
双月投资=getDoubleWithinRange(sc,
“输入每月投资:”,0,1000);
double interestRate=getDoubleWithinRange(sc,
“输入年利率:”,0,30);
整数年=getIntWithinRange(sc,
“输入年数:”,0,100);
//计算未来价值
双月利率=利率/12/100;
整数个月=年*12;
双倍未来价值=计算未来价值(
月投资,月利息,月);
//获取货币和百分比格式化程序
NumberFormat currency=NumberFormat.getCurrencyInstance();
NumberFormat percent=NumberFormat.getPercentInstance();
百分比。setMinimumFractionDigits(1);
//将结果格式化为单个字符串
字符串结果=
“每月投资:\t”
+currency.format(monthlyInvestment)+“\n”
+“年利率:\t”
+百分比格式(利率/100)+“\n”
+“年数:\t”
+年数+“\n”
+“未来值:\t\t”
+currency.format(futureValue)+“\n”;
//打印结果
System.out.println();
System.out.println(“格式化结果”);
系统输出打印项次(结果);
字符串monthlyInvestmentFormat=currency.format(monthlyInvestment);
字符串interestRateFormat=百分比格式(interestRate/100);
字符串futureValueFormat=currency.format(futureValue);
FutureValueArrayList.add(monthlyInvestmentFormat);
FutureValueArrayList.add(利率格式);
FutureValueArrayList.add(整数.toString(年));
添加(futureValueFormat);
//查看用户是否要继续
系统输出打印(“是否继续?”);
choice=sc.next();
System.out.println();
}
System.out.print(“库存/生产任务。\t策略\t耳朵\t未来值\n”);
对于(int i=0;i对于(int i=1;i您需要使用
println()
在新行中显示每个条目。它将打印您的条目,然后在末尾插入行分隔符以终止该行

System.out.println(FutureValueArray);
否则,您可以修改
print()
方法参数,使其在新行上打印。类似如下:-

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

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

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

System.out.println(FutureValueArray);
否则,您可以修改
print()
方法参数,使其在新行上打印。类似如下:-

System.out.print(FutureValueArray + "\n");
请注意,
\t
表示