1

尝试运行下面的程序,但在使用 switch 方法时出现错误

导入 java.util.Scanner;

/** ** @author kern 公共类贷款 {

public static void main(String[] args) {

扫描仪输入 = 新扫描仪(System.in);

    //variabled decleared
    double rate, payment,principal,interest,n;
    int length;
    String period;

    //input
    System.out.print("Enter the amount of money borrowed: $");
    principal = input.nextDouble();
    System.out.print("Enter the annual interest rate: ");
    interest = input.nextDouble();
    System.out.print("Enter the payment period :");
    period = input.next();
    System.out.print("Enter Loan Length:");
    length = input.nextInt();
    //process

    rate=interest/100;
    payment= principal*(rate*Math.pow((1+rate),n)/ Math.pow ((1+rate),n));

      if (period==annually) {
               n=1*length;
                System.out.prtintf(Your monthly sum is %f:,payment);{

 if (period==semiannuall) {
               n=2*length;
                System.out.prtintf(Your monthly sum is %f:,payment);{

 if (period== quarterly) {
               n=4*length;
                System.out.prtintf(Your quarterly sum is %f:,payment);{

 if (period==monthly) {
               n=12*length;
                System.out.prtintf(Your monthly sum is %f:,payment);{




}

}

4

2 回答 2

3

String从 java 7 支持 case 值

你需要像这样使用它

if("annually".equals(period)){
}
于 2012-11-17T02:29:04.807 回答
0

根据 Jigar Joshi 链接,您现在可以执行字符串:

在 switch 语句中使用字符串

在 Java SE 7 及更高版本中,您可以在 switch 语句的表达式中使用 String 对象。以下代码示例 StringSwitchDemo 根据名为 month 的字符串的值显示月份的编号:

public class StringSwitchDemo {
public static int getMonthNumber(String month) {

    int monthNumber = 0;

    if (month == null) {
        return monthNumber;
    }

    switch (month.toLowerCase()) {
        case "january":
            monthNumber = 1;
            break;
        case "february":
            monthNumber = 2;
            break;
        case "march":
            monthNumber = 3;
            break;
        case "april":
            monthNumber = 4;
            break;
        case "may":
            monthNumber = 5;
            break;
        case "june":
            monthNumber = 6;
            break;
        case "july":
            monthNumber = 7;
            break;
        case "august":
            monthNumber = 8;
            break;
        case "september":
            monthNumber = 9;
            break;
        case "october":
            monthNumber = 10;
            break;
        case "november":
            monthNumber = 11;
            break;
        case "december":
            monthNumber = 12;
            break;
        default: 
            monthNumber = 0;
            break;
    }

    return monthNumber;
}

public static void main(String[] args) {

    String month = "August";

    int returnedMonthNumber =
        StringSwitchDemo.getMonthNumber(month);

    if (returnedMonthNumber == 0) {
        System.out.println("Invalid month");
    } else {
        System.out.println(returnedMonthNumber);
    }
}

}

于 2012-11-17T02:40:42.543 回答