我的修复代码是否正确? [英] Is my code for fix withdrawl correct?

查看:99
本文介绍了我的修复代码是否正确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是用于修复撤销但我的测试用例失败。



我尝试过:



  import  java.util。*; 
public class save
{
public static void main(字符串 args [])
{
double Money = 0 ,principle_amount,Interest_rate = 0 ,Amount,n;
int 时间= 0 ,Actual_amount_need;
扫描仪sc = 扫描仪(System.in);
System.out.println( 输入所需的现金流:);
System.out.println( 输入时间(以月为单位):);
System.out.println( 输入率:);
Money = sc.nextDouble();
Time = sc.nextInt();
Interest_rate = sc.nextDouble();
尝试
{
如果(Money> 0 &&&&< 0 &&< Interest_rate> = 0
{
if (Interest_rate!= 0
{
Interest_rate = Interest_rate / 100 ;
principle_amount = Money *(( 1 - Math.pow((1+(Interest_rate / 12)),( - Time)))/(Interest_rate / 12 ));
}
else
{
金额=金钱*时间;
principle_amount =金额;
}
Actual_amount_need =( int )(principle_amount + 0 5 );
System.out.println( 现在要存入的资金: + Actual_amount_need);
return ;
}
else
{
System.out.println( 错误);
}
}
catch (例外e)
{
System.out.println(e) ;
System.out.println( not Amount valid input);
}
}
}

解决方案

假设您希望从存款利息中获得收入每月1000.00。



输入所需的现金流(这将是1000)

时间(这将是12)

利率(这将是6)

您的答案将是200,000.00押金。



你会注意到所做的更改,这些更改简化了计算中的代码。

我还在数据输入点之前放置了try以捕获输入错误。



这是一个不错的小程序,可以作为大量实验的基础。



我希望我能正确理解你的意图。

这是代码。我把它放在这里有问题,所以我希望它还可以。



 



Quote:



import java.util。*;

公共类保存{



public static void main(String [] args){

// TODO自动生成的方法存根

double Money = 0.00,principle_amount = 0.00,Interest_rate = 0.00;

int Time = 0;

Scanner sc = new Scanner(System。 in);

System.out.println(输入所需的现金流:);

System.out.println(输入时间(以月计):);

System.out.println(输入率:);

尝试

{

Money = sc .nextDouble();

时间= sc.nextInt();

Interest_rate = sc.nextDouble();

sc.close() ;

if(Money> 0&&&time; 0&& Interest_rate> = 0)

{

if(Interest_rate!= 0)

{

Interest_rate = Interest_rate / 100;

principle_amount =(Money * Time)/ Interest_rate;

}

其他

{

principle_amount =金钱*时间;

}



System.out.println(现在存入资金:+ principle_amount);

返回;

}

else

{

System.out.println(错误);

}

}

catch(例外e)

{

System.out.println(e);

系统。 out.println(非金额有效输入);

}

}

}


It is for fix withdrawl but my test cases are failing.

What I have tried:

import java.util.*;
	public class save
	{
	public static void main(String args[]) 
		{
		double Money = 0, principle_amount, Interest_rate = 0, Amount, n;
		int Time = 0, Actual_amount_need;
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter desired cashflow: ");
		System.out.println("Enter time (in months): ");
		System.out.println("Enter rate: ");
		Money = sc.nextDouble();
		Time = sc.nextInt();
		Interest_rate = sc.nextDouble();
		try 
			{
			if (Money > 0 && Time > 0 && Interest_rate >= 0)
				{
				if (Interest_rate != 0) 
					{
					Interest_rate = Interest_rate / 100;
					principle_amount = Money * ((1 - Math.pow((1+(Interest_rate/12)),(-Time)))/(Interest_rate / 12));
					} 
				else 
					{
					Amount = Money * Time;
					principle_amount = Amount;
					}
				Actual_amount_need = (int)(principle_amount + 0.5);
				System.out.println("Money to be deposited now: " + Actual_amount_need);
				return;
				} 
				else 
				{
				System.out.println("Error");
				}
			}
			catch (Exception e) 
			{
			System.out.println(e);
			System.out.println("not Amount valid input");
			}
		}
	}

解决方案

Assume you want an income from the interest on your deposit of 1000.00 per month.

"Enter desired cashflow" (this will be 1000)
"Time" (this will be 12)
"Interest rate" (this will be 6)
Your answer will be 200,000.00 deposit required.

You will notice the changes made which simplified the code in your calculation.
Also I placed the try before your data entry point in order to catch the input errors.

This is a nice little program and can form the basis for a lot of experimentation.

I hope I have interpreted your intentions correctly.


Here is the code. I had a problem putting it here so I hope it is still ok.


Quote:


import java.util.*;
public class Save {

public static void main(String[] args) {
// TODO Auto-generated method stub
double Money = 0.00, principle_amount=0.00, Interest_rate = 0.00;
int Time = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter desired cashflow: ");
System.out.println("Enter time (in months): ");
System.out.println("Enter rate: ");
try
{
Money = sc.nextDouble();
Time = sc.nextInt();
Interest_rate = sc.nextDouble();
sc.close();
if (Money > 0 && Time > 0 && Interest_rate >= 0)
{
if (Interest_rate != 0)
{
Interest_rate = Interest_rate / 100;
principle_amount = (Money * Time)/Interest_rate;
}
else
{
principle_amount = Money * Time;
}

System.out.println("Money to be deposited now: " + principle_amount);
return;
}
else
{
System.out.println("Error");
}
}
catch (Exception e)
{
System.out.println(e);
System.out.println("not Amount valid input");
}
}
}


这篇关于我的修复代码是否正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆