非法开始表达式public boolean setTuitionFees;(double fees); {

时间:2012-09-17 15:43:17

标签: java

我正在尝试创建一个方法,因此我可以将“tuitionFees”和“奖学金”设置为程序用户配置文件的双精度。我不确定我是否正确地设置了它。

    import java.util.Scanner;
    /**
    StudentInvoice.java
    Defines StudentInvoice objects
    @author: Evan Fravert
    */
    public class StudentInvoice {
    // declare instance variables here
    // constructor
    // methods
    // toString method (for String output)
       private String studentName;
       private String studentNumber;
       private double tuitionFees;
       private double scholarships;

    public String toString() {
     String output = "Name: " + studentName + "\n";
     output += "Student ID Number: " + studentNumber + "\n";
     output += "Tuition & Fees: " + tuitionFees + "\n";
     output += "Scholarship: " + scholarships + "\n";
     return output;
     }

    public StudentInvoice(String name, String number, double fees, double scholarship){
     studentName = name;
     studentNumber = number;
     tuitionFees = fees;
     scholarships = scholarship;
     }
    public boolean setStudentName(String name){
     if(name.length() == 0)
     {
     return false;
     }
     else
     {
     studentName = name;
     return true;
     }
     }
    public boolean setStudentNumber(String number){
     if(number.length() == 0)
     {
     return false;
     }
     else
     {
     studentNumber = number;
     return true;
     }  
    public boolean setTuitionFees(double fees){
     if(fees < 0.0) {
 return false;
 }
 else {
 tuitionFees = fees;
 return true;
 }
 }
 public boolean setScholarships (double scholarship){
 if(scholarship < 0.0) {
 return false;
 }
 else {
 Scholarships = scholarship;
 return true;
 }
    }
    }
    }

我的班级与互动:

    public class StudentInvoiceApp {
    public static void main (String[] args) {
    StudentInvoice Evan = new StudentInvoice("Evan Fravert");
    Evan.setName("Evan Fravert");
    Evan.setNumber(01234);
    Evan.setTuitionFees(0.00);
    Evan.setScholarship(0.00);
    System.out.print(Evan);
    }
    }

2 个答案:

答案 0 :(得分:1)

在尝试编辑你的帖子时,我发现你在setStudentNumber之后缺少一个大括号。具有适当格式的更正代码如下。在任何编程语言中,格式化代码对于理解其含义非常重要。人们写了关于如何格式化代码的全书,并且有充分的理由。通过良好的格式化,您可能会注意到缺少的大括号。

搜索“如何格式化Java代码”会产生一些有用的约定,包括Oracle提供的有关如何格式化Java代码的文档。如果有疑问,大多数IDE都提供格式化代码的快捷方式。在Eclipse中,它是Ctrl+Shift+F,在NetBeans中,它是Alt+Shift+F

import java.util.Scanner;

/**
 * StudentInvoice.java Defines StudentInvoice objects
 * 
 * @author: Evan Fravert
 */
public class StudentInvoice {
    // declare instance variables here
    // constructor
    // methods
    // toString method (for String output)
    private String studentName;
    private String studentNumber;
    private double tuitionFees;
    private double scholarships;

    public String toString() {
        String output = "Name: " + studentName + "\n";
        output += "Student ID Number: " + studentNumber + "\n";
        output += "Tuition & Fees: " + tuitionFees + "\n";
        output += "Scholarship: " + scholarships + "\n";
        return output;
    }

    public StudentInvoice(String name, String number, double fees,
            double scholarship) {
        studentName = name;
        studentNumber = number;
        tuitionFees = fees;
        scholarships = scholarship;
    }

    public boolean setStudentName(String name) {
        if (name.length() == 0) {
            return false;
        } else {
            studentName = name;
            return true;
        }
    }

    public boolean setStudentNumber(String number) {
        if (number.length() == 0) {
            return false;
        } else {
            studentNumber = number;
            return true;
        }
    }

    public boolean setTuitionFees(double fees) {
        if (fees < 0.0) {
            return false;
        } else {
            tuitionFees = fees;
            return true;
        }
    }

    public boolean setScholarships(double scholarship) {
        if (scholarship < 0.0) {
            return false;
        } else {
            scholarships = scholarship;
            return true;
        }
    }
}

答案 1 :(得分:0)

将setStudentNumber方法更改为此

 public boolean setStudentNumber(String number){
 if(number.length() == 0)
 {
 return false;
 }
 else
 {
 studentNumber = number;
 return true;
 }
 }  

你错过了其他的结束括号