Now am getting incompatible error when using the if else statements

trying to run the program below but am getting an error when using the switch method

import java.util.Scanner;

/** * * @author kern public class loans {

public static void main(String[] args) {

Scanner input = new Scanner(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);{




}

}


String as case value are supported from java 7

See

  • Switch case
  • You need to use it like

    if("annually".equals(period)){
    }
    

    Per Jigar Joshi link you can do strings now:

    Using Strings in switch Statements

    In Java SE 7 and later, you can use a String object in the switch statement's expression. The following code example, StringSwitchDemo, displays the number of the month based on the value of the String named 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);
        }
    }
    

    }

    链接地址: http://www.djcxy.com/p/96108.html

    上一篇: Java多个扫描器

    下一篇: 现在我在使用if else语句时出现不兼容的错误