-2

我正在尝试创建一种方法,以便可以将“tuitionFees”和“scholarships”设置为程序用户个人资料的双倍。我不确定我是否设置正确。

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

2 回答 2

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;
        }
    }
}
于 2012-09-17T15:52:04.643 回答
0

将您的 setStudentNumber 方法更改为此

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

你错过了一个闭括号 else

于 2012-09-17T15:47:32.197 回答