나는 프로 시저 유형 메서드에 try-catch를 넣으려고하고 있지만 함수 유형이어야한다는 것을 95 % 확신합니다. 내가 성취하고자하는 것은 코드를 메인에서 더 짧게 만드는 것입니다. 가장 큰 생각 중 하나는 try-catch를 메서드에 넣고 메서드를 호출하는 것입니다.시도하고 방법으로 캐치
정수가 아니면 입력을 확인합니다. 예외가 발생하더라도 프로그램에서 계속 입력하면 유효성이 검사 된 입력을 "기억하지"않습니다. 다음은 내가 문제가되는 코드의 일부입니다.
import java.util.Scanner;
public class ch7exercise1
{
public static double compound(double oA, double cI)
{
return roundCent((oA*(Math.pow((1+(percent(cI))),10))));
}
public static double percent(double interest)
{
return interest/100.0;
}
public static double roundCent(double amount)
{
return ((Math.round(amount*100))/100.0); //100.0 is mandatory.
}
public static void tryCatchNum(double value)
{
while(true)
{
try
{
Scanner iConsole = new Scanner(System.in);
value = Double.parseDouble(iConsole.nextLine());
System.out.println(" ");
break;
}
catch(NumberFormatException e)
{
System.out.println("NumberFormatException error has oocured. Please try again.");
}
}
}
@SuppressWarnings("unused")
public static void main(String[] args)
{
boolean f = true;
boolean f2 = true;
double origAmount = 0;
double compInterest = 0;
double total = 0;
Scanner iConsole = new Scanner(System.in);
System.out.println("10 year Compound Interest Claculator\n");
System.out.println("Input amount of money deposited in the bank");
tryCatchNum(origAmount);
System.out.println("Input compouded interest rate. (If the compound interest is 3% input 3)");
tryCatchNum(compInterest);
total = compound(origAmount,compInterest);
System.out.println("$"+total);
}
}
Double은 프리미티브이므로 값으로 전달되고 참조로 전달되지 않습니다. –
'double'은 프리미티브입니다. 'Double'은 객체입니다 ... – DNA
그리고 객체 참조도 값으로 전달됩니다. 같은 알고리즘으로 Double을 사용하면 아무 것도 바뀌지 않습니다. –