现在使用if else语句时出现不兼容的错误

时间:2012-11-17 02:27:50

标签: java switch-statement

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

import 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);{




}

}

2 个答案:

答案 0 :(得分:3)

String从java 7支持案例值

查看

您需要像

一样使用它
if("annually".equals(period)){
}

答案 1 :(得分:0)

Per Jigar Joshi链接你现在可以做弦:

在交换机语句中使用字符串

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

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);
    }
}

}