Java中的try语句的使用_Java中的Try-Catch语句

更新:感谢关于保持代码SSCCE的建议.就像我说的,这是我第一次在这里发帖.我下次肯定会花时间确保代码在我下次发布之前已经充分调整.

所以我正在为我的计算机科学课写一个程序,并且遇到了一个我无法弄清楚的奇怪问题.我尝试通过使用try / catch语句来阻止无效的数据类型输入,然后它可能会结束并结束整个程序,从而使我的程序健壮,但它似乎不起作用.输入无效数据类型时,程序结束时不会在窗口中显示任何Java的标准错误消息.我不知道为什么会这样,但我假设我错误地使用了try / catch语句.以下是我的程序的两个类:

/* The core class for the finance calculations to be done in the FinanceApp class. */

public class FinanceCore{

// Instance variables

int numYears;

double principal;

double interestRate;

double balance;

public FinanceCore(){

numYears = 0;

principal = 0;

interestRate = 0;

balance = 0;

}

// Mutator methods that return boolean values depending on whether the input was valid or not

public boolean setYears(int y){

if(y >= 0){

numYears = y;

return true;

}

else return false;

}

public boolean setPrincipal(double p){

if(p >= 0){

principal = p;

balance = principal;

return true;

}

else return false;

}

public boolean setInterestRate(double ir){

if(ir >= 0 && ir <= 1){

interestRate = ir;

return true;

}

else return false;

}

// Two accessors

public int getYears(){

return numYears;

}

public double getPrincipal(){

return principal;

}

// This method calculates and returns the balance at the end of each year

public double plusYear(){

balance = balance*(1+interestRate);

return balance;

}

}

/* This program recieves three pieces of data (interest rate, principal amount, number of years) and generates an output

* table that shows how much money will be in a fund with the given parameters at the end of every year. */

import java.util.Scanner;

public class FinanceApp{

public static void main(String[]args){

// First, we will declare our global variables, and set them to default values

Scanner reader = new Scanner(System.in);

FinanceCore account = new FinanceCore();

int menuItem = 0;

// Now, we'll greet the user (because we're friendly like that)

System.out.println("Welcome! Please select a menu option below.");

while(true){

/* Now, our first user interface: a menu system that displays four options to the user arranged in

* columns for aesthetic effect. This is accomplished using the printf method.

*/

System.out.printf("%n%-20s%-20s%n%-20s%-20s%n%-20s%n",

"Set Principal[1]","Set Interest Rate[2]","Set Timespan[3]","Calculate[4]","Quit[5]");

System.out.print(": ");

// Now we get the user input until it is valid, and catch and errors in input type

try {

menuItem = reader.nextInt();

}

catch(Exception e){

reader.nextLine(); // Clear the input stream to avoid an infinite loop

System.out.println("Please a valid number 1-5.");

}

// The code for setting the principal amount

if(menuItem == 1){

while(true){

System.out.print("Please enter the principal investment amount: ");

try{

if(account.setPrincipal(reader.nextDouble()));

break;

}

catch(Exception e){

reader.nextLine(); // Clear the input stream to avoid an infinite loop

System.out.println("Please enter a valid dollar amount.");

}

}

}

// The code for setting the interest rate

else if(menuItem == 2){

while(true){

System.out.print("Please enter the quarterly interest rate: ");

try{

if(account.setInterestRate(reader.nextDouble()));

break;

}

catch(Exception e){

reader.nextLine(); // Clear the input stream to avoid an infinite loop

System.out.println("Please enter a valid decimal number between 0 and 1 (inclusive).");

}

}

}

// The code for setting the number of years

else if(menuItem == 3){

while(true){

System.out.print("Please enter the number of years the account will exist: ");

try{

if(account.setYears(reader.nextInt()));

break;

}

catch(Exception e){

reader.nextLine(); // Clear the input stream to avoid an infinite loop

System.out.println("Please enter a valid integer value.");

}

}

}

// This part actually executes the calculation

else if(menuItem == 4){

System.out.printf("%-10s%-10s%n%-10d%-10.2f%n","YEAR","BALANCE",0,account.getPrincipal());

int count = 1;

for(int c = account.getYears(); c > 0; c--){

System.out.printf("%-10d%-10.2f%n",count,account.plusYear());

count++;

}

}

// If the user enters any other number, the program quits

else

break;

}

}

}

请注意,此程序存在一个我似乎无法解决的问题.出于某种原因,每一次

用户在菜单选择提示下输入无效数据类型,程序结束(尽管没有任何错误

被抛出).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值