【问题标题】:syntax error on token BLANK while expected?预期的令牌空白语法错误?
【发布时间】:2021-05-03 16:34:18
【问题描述】:

我对编码有点陌生,不明白为什么会出现这种情况。有人请帮忙。

导入 java.util.Scanner;

类calculateMortgageA { 公共静态 void main (String[]args) {

//create scanner 
Scanner input =new Scanner (System.in);

//prompt user to write in salaries so they can be read in while declaring the variables for the salaries
System.out.println("Enter person 1's salary:");
double salaryOne = input.nextDouble();

System.out.println("Enter person 2'a salary:");
double salaryTwo=input.nextDouble();

//calculate the mortgage 
System.out.print("The mortgage will be £" + calcmortgage(salaryOne,salaryTwo));

input.close();

}

public static double calcmortgage(双薪一,双薪二){

    double mortgage;

    if (salaryOne > salaryTwo)
           mortgage = 3*salaryOne + salaryTwo;
           return mortgage;

    
    else (salaryTwo > salaryOne)
           mortgage = 3*salaryTwo + salaryOne;
           return mortgage;

}

}

【问题讨论】:

  • 哪一行给了你错误?
  • 你需要一个块,在两个最后的 if 语句之后用大括号 ( { ... } ) 分隔。

标签: java arrays syntax


【解决方案1】:

看起来您缺少 ifelse 块周围的花括号:

public static double calcmortgage (double salaryOne, double salaryTwo) {

    double mortgage;

    if (salaryOne > salaryTwo) { // Here
           mortgage = 3*salaryOne + salaryTwo;
           return mortgage;
    } // And here
    else (salaryTwo > salaryOne) { // And here
           mortgage = 3*salaryTwo + salaryOne;
           return mortgage;
    } // And here
}

【讨论】:

    猜你喜欢
    • 2013-07-16
    • 2012-08-02
    • 1970-01-01
    • 2012-04-14
    • 1970-01-01
    • 1970-01-01
    • 2012-07-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多