Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.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,我正试图编写一个贷款计算器。我好像有点问题。我正在尝试从用户那里获取输入并验证输入。我知道我做错了问题是我挠头想知道如何做对。 我在d=getDouble(sc,提示符)上得到一条红线;i=getInt(sc,prompt);我知道我没有正确的编码。我只是不知道如何着手修理它 我还必须验证continue语句,我不确定这是最好的方法,最后讲师希望代码不超过80行,我是对的,大约80行。我想我正在寻找一个更好的方法来做到这一点,但作为一个新手,我正在挠头,我希望有人能伸出援手 像往常一样,我真的很

我正试图编写一个贷款计算器。我好像有点问题。我正在尝试从用户那里获取输入并验证输入。我知道我做错了问题是我挠头想知道如何做对。 我在d=getDouble(sc,提示符)上得到一条红线;i=getInt(sc,prompt);我知道我没有正确的编码。我只是不知道如何着手修理它

我还必须验证continue语句,我不确定这是最好的方法,最后讲师希望代码不超过80行,我是对的,大约80行。我想我正在寻找一个更好的方法来做到这一点,但作为一个新手,我正在挠头,我希望有人能伸出援手

像往常一样,我真的很感谢你的帮助

   import java.util.Scanner;
   import java.text.NumberFormat;

    public class LoanCalculator
    {   
        public static double getDoubleWithinRange(Scanner sc, String prompt, double min, double max)
        {
          double d = 0.0;
          boolean isValid = false;
          while(isValid == false);
           {
                d = getDouble(sc, prompt);
                if (d <= min)
                   {
                   System.out.println("Error! Number must be greater tha 0.0");
                   }
                   else if (d >= max)
                  {
                     System.out.println("Error number must be less than 1000000.0");
                }
                else 
                    isValid = true;
            }
            return d;       
        }
            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 more than 0");
                else if (i >= max)
                    System.out.println("Error! Number must be less than 100");
                else 
                    isvalid = true;
            }   
        }
          public static void main(String[] args)
         {
            System.out.println("Welcome to the loan calculator");
            Scanner sc = new Scanner(System.in);
            String choice = "y";
            while (choice.equalsIgnoreCase("y"))
            {
                System.out.println("DATA ENTRY");
                double loanAmount = getDoubleWithinRange(sc, "Enter loan amount: ", 0.0, 1000000.0);
                double interestRate = getDoubleWithinRange(sc, "Enter yearly interest rate: ", 0, 20);
                int years = getIntWithinRange(sc, "Enter number of years: ", 0, 100);
                int months = years * 12;

                double monthlyPayment = loanAmount * interestRate/
                        (1 - 1/Math.pow(1 + interestRate, months));

                NumberFormat currency = NumberFormat.getCurrencyInstance();
                NumberFormat percent = NumberFormat.getPercentInstance();
                percent.setMaximumFractionDigits(3);
                System.out.println("RESULST");
                System.out.println("Loan Amount" + currency.format(loanAmount));
                System.out.println("Yearly interest rate: " + percent.format(interestRate));
                System.out.println("Number of years: " + years);
                System.out.println("Monthly payment: " + currency.format(monthlyPayment));

                System.out.println();
                System.out.println("Continue? (y/n): ");
                choice =sc.next();
                System.out.println();

            }           
        }
    }
import java.util.Scanner;
导入java.text.NumberFormat;
公共类贷款计算器
{   
公共静态双getDoubleWithinRange(扫描仪sc、字符串提示、双最小值、双最大值)
{
双d=0.0;
布尔值isValid=false;
while(isValid==false);
{
d=getDouble(sc,提示符);
如果(d=最大值)
{
System.out.println(“错误号必须小于1000000.0”);
}
其他的
isValid=true;
}
返回d;
}
公共静态整型整型getIntWithinRange(扫描程序sc、字符串提示、整型最小值、整型最大值)
{
int i=0;
布尔值isvalid=false;
while(isvalid==false)
{
i=getInt(sc,提示符);
如果(i=最大值)
System.out.println(“错误!数字必须小于100”);
其他的
isvalid=true;
}   
}
公共静态void main(字符串[]args)
{
System.out.println(“欢迎使用贷款计算器”);
扫描仪sc=新的扫描仪(System.in);
字符串选择=“y”;
while(choice.equalsIgnoreCase(“y”))
{
System.out.println(“数据输入”);
double loanAmount=getDoubleWithinRange(sc,“输入贷款金额:”,0.0,1000000.0);
double interestRate=getDoubleWithinRange(sc,“输入年利率:”,0,20);
整数年=getIntWithinRange(sc,“输入年数:”,0,100);
整数个月=年*12;
双月付款=贷款金额*利率/
(1-1/数学功率(1+利率,月));
NumberFormat currency=NumberFormat.getCurrencyInstance();
NumberFormat percent=NumberFormat.getPercentInstance();
百分比。setMaximumFractionDigits(3);
System.out.println(“result”);
System.out.println(“贷款金额”+货币格式(贷款金额));
System.out.println(“年利率:+百分比格式(利率));
System.out.println(“年数:+年”);
System.out.println(“月付款:+货币.格式(月付款));
System.out.println();
系统输出打印项次(“是否继续”);
choice=sc.next();
System.out.println();
}           
}
}

我认为
getDouble
getInt
是字符串函数,因此必须先获取字符串,然后调用这些方法。但是,由于您有一个扫描仪,我假设您希望将其与
nextXXX
方法一起使用:

Scanner sc = new Scanner (System.in);
double d = sc.nextDouble();
您可以将此完整代码段用于教育目的:

import java.util.Scanner;
class Test {
    public static void main (String args[]) {
        Scanner sc = new Scanner (System.in);

        System.out.print("Enter your double: ");
        double d = sc.nextDouble();

        System.out.print("Enter your integer: ");
        int i = sc.nextInt();

        System.out.println("You entered:  " + d + " and " + i);
    }
}
发言稿:

Enter your double: 3.14159
Enter your integer: 42
You entered:  3.14159 and 42
基本上,这个过程是:

  • 使用标准输入流实例化扫描仪
  • 使用
    print
    作为提示
  • 使用扫描仪
    nextXXX
    方法获取输入值

根据您的评论,这里需要更多的帮助

在主要功能中,您有:

double loanAmount = getDoubleWithinRange(sc, "Enter loan amount: ", 0.0, 1000000.0)
该功能具有原型:

public static double getDoubleWithinRange(
    Scanner sc, String prompt, double min, double max)
这意味着原型中的这些变量将被设置为调用中的值。因此,为了提示输入信息,您可以使用以下内容(这是为了替换
d=getDouble(sc,prompt);
行):

在这里,您已经提示用户并从他们那里输入double。第一行打印出提示,第二行使用扫描仪从用户处获取输入

另外,您对最小值和最大值的检查是正确的,但错误消息的硬编码值为
0
100K
。我建议您使用参数来定制这些消息,例如更改:

System.out.println("Error! Number must be greater tha 0.0");
进入:

这样,如果将来
min
max
发生变化,您的用户就不会感到困惑:-)



我将让您对整数输入执行类似的操作。毕竟,这是你的家庭作业:-)

你还没有实现getDouble(Scanner,String)和getInt(Scanner,String),这就是为什么你会得到红线

因为您已经有了一个扫描仪,并且提示字符串将其更改为

System.out.print(prompt);
d = sc.nextDouble();
整数呢

System.out.print(prompt);
i = sc.nextInt();

我应该把代码放在哪里?它能代替getDouble吗?这就是我真正不明白的。上周我做了另一个项目,我到处都是。基本上,我的东西都倒过来了,这真的把我搞砸了。@Jeremy,我已经用一个完整(尽管很小)的程序更新了答案,说明了你需要做什么。我不会为你做太多的工作,因为这可能是课程作业,如果你自己在一点帮助下完成它,你会感觉更好(有些人可能会说,比感觉更好更重要的是,你会感觉更好)。那么你是说我需要重写我的主要内容,所以它更像你编写的代码?我该怎么办
System.out.print(prompt);
d = sc.nextDouble();
System.out.print(prompt);
i = sc.nextInt();