租金率计算

时间:2016-01-20 00:47:08

标签: java

大家好我正在参加我的学位的java课程,我得到了一个计算出租率的作业,到目前为止我能够得到代码,但我的错误是持久性的,我觉得我是非常接近,但肯定需要一些调整。如果有人愿意花时间让我知道我可以添加或取出的内容会很棒。

以下是错误:

RentalRates.java:117: error: illegal start of expression
         private static clacGen(s.gender);
         ^
RentalRates.java:117: error: illegal start of expression
         private static clacGen(s.gender);
                 ^
RentalRates.java:117: error: ';' expected
         private static clacGen(s.gender);
                               ^
RentalRates.java:117: error: not a statement
         private static clacGen(s.gender);
                                 ^
RentalRates.java:117: error: ';' expected
         private static clacGen(s.gender);
                                        ^
RentalRates.java:133: error: illegal start of expression
         private static calcAge(birthMonth,birthDay,birthYear);
         ^
RentalRates.java:133: error: illegal start of expression
         private static calcAge(birthMonth,birthDay,birthYear);
                 ^
RentalRates.java:133: error: ';' expected
         private static calcAge(birthMonth,birthDay,birthYear);
                               ^
RentalRates.java:133: error: not a statement
         private static calcAge(birthMonth,birthDay,birthYear);
                                ^
RentalRates.java:133: error: ';' expected
         private static calcAge(birthMonth,birthDay,birthYear);
                                          ^
RentalRates.java:133: error: not a statement
         private static calcAge(birthMonth,birthDay,birthYear);
                                           ^
RentalRates.java:133: error: ';' expected
         private static calcAge(birthMonth,birthDay,birthYear);
                                                   ^
RentalRates.java:133: error: not a statement
         private static calcAge(birthMonth,birthDay,birthYear);
                                                    ^
RentalRates.java:133: error: ';' expected
         private static calcAge(birthMonth,birthDay,birthYear);
                                                             ^
RentalRates.java:136: error: ';' expected
            today_date = curMonth,curDay,curYear
                                 ^
RentalRates.java:136: error: not a statement
            today_date = curMonth,curDay,curYear
                                  ^
RentalRates.java:136: error: ';' expected
            today_date = curMonth,curDay,curYear
                                        ^
RentalRates.java:155: error: illegal start of expression
         private static String calcRateClass(age, gender);
         ^
RentalRates.java:155: error: illegal start of expression
         private static String calcRateClass(age, gender);
                 ^
RentalRates.java:155: error: ';' expected
         private static String calcRateClass(age, gender);
                              ^
RentalRates.java:162: error: illegal start of expression
         private static displayResults(gender, age, rateResult);
         ^
RentalRates.java:162: error: illegal start of expression
         private static displayResults(gender, age, rateResult);
                 ^
RentalRates.java:162: error: ';' expected
         private static displayResults(gender, age, rateResult);
                                      ^
RentalRates.java:162: error: not a statement
         private static displayResults(gender, age, rateResult);
                                       ^
RentalRates.java:162: error: ';' expected
         private static displayResults(gender, age, rateResult);
                                             ^
RentalRates.java:162: error: not a statement
         private static displayResults(gender, age, rateResult);
                                               ^
RentalRates.java:162: error: ';' expected
         private static displayResults(gender, age, rateResult);
                                                  ^
RentalRates.java:162: error: not a statement
         private static displayResults(gender, age, rateResult);
                                                    ^
RentalRates.java:162: error: ';' expected
         private static displayResults(gender, age, rateResult);
                                                              ^

29个错误

我想发生什么:__________________________________

欢迎来到汽车租赁者的费率查询器。

在此输入代码请输入承租人的性别(m / f):m

请输入今天的日期(mm dd yyyy):10 4 2011

请输入承租人的出生日期(mm dd yyyy):1 22 1990

谢谢。

男性租客是21岁。

费率等级为:抱歉,承租人不是25岁或以上。

CODE:__________________________________

enter code here
import java.util.*;

public class RentalRates
{
private static final boolean DEBUG = true;

private static final String BEST_RATE = "Best rate - $40.00 per day or   $200.00 per week.";
private static final String RISK_RATE_1= "Risk rate 1-$50.00 per day or $255.00 per week.";
private static final String RISK_RATE_2= "Risk rate 2-$57.00 per day or $285.00 per week.";
private static final String RISK_RATE_3= "Risk rate 3-$%4.2f per day or $%5.2f per week.";

public static void main(String[] args)
{    
  int curMonth = 0;
  int curDay = 0;
  int curYear = 0;
  int birthMonth = 0;
  int birthDay = 0;
  int birthYear = 0;
  String gender = "";
  int age = 0;
  String rateResult;            



  if (DEBUG == true)
  {
    // Establish a 'current' date for testing...
     curMonth = 2;
     curDay = 1;
     curYear = 2016;

     System.out.println("First test case: Renter is not old enough to rent...");
     birthMonth = 2;
     birthDay = 2;
     birthYear = 1991;
     gender = "m";
     age = calcAge(curMonth, curDay, curYear, birthMonth, birthDay, birthYear);
     rateResult = calcRateClass(age, gender);
     displayResults(gender, age, rateResult);

     System.out.println("\nSecond test case: Renter is barely old enough (57/285)...");
     birthMonth = 2;
     birthDay = 1;
     birthYear = 1991;
     gender = "m";
     age = calcAge(curMonth, curDay, curYear, birthMonth, birthDay, birthYear);
     rateResult = calcRateClass(age, gender);
     displayResults(gender, age, rateResult);

     System.out.println("\nThird test case: Renter is 35 and male (40/200)...");
     birthMonth = 1;
     birthDay = 1;
     birthYear = 1981;
     gender = "m";
     age = calcAge(curMonth, curDay, curYear, birthMonth, birthDay, birthYear);
     rateResult = calcRateClass(age, gender);
     displayResults(gender, age, rateResult);

     System.out.println("\nFourth test case: Renter is 35 and female (40/200)...");
     birthMonth = 1;
     birthDay = 1;
     birthYear = 1981;
     gender = "f";
     age = calcAge(curMonth, curDay, curYear, birthMonth, birthDay, birthYear);
     rateResult = calcRateClass(age, gender);
     displayResults(gender, age, rateResult);

     System.out.println("\nFifth test case: Renter is 30 and male (57/285)...");
     birthMonth = 1;
     birthDay = 1;
     birthYear = 1986;
     gender = "m";
     age = calcAge(curMonth, curDay, curYear, birthMonth, birthDay, birthYear);
     rateResult = calcRateClass(age, gender);
     displayResults(gender, age, rateResult);

     System.out.println("\nSixth test case: Renter is 30 and female (40/200)...");
     birthMonth = 1;
     birthDay = 1;
     birthYear = 1986;
     gender = "f";
     age = calcAge(curMonth, curDay, curYear, birthMonth, birthDay, birthYear);
     rateResult = calcRateClass(age, gender);
     displayResults(gender, age, rateResult);

     System.out.println("\nSeventh test case: Renter is 76 and male (62/255)...");
     birthMonth = 1;
     birthDay = 1;
     birthYear = 1940;
     gender = "m";
     age = calcAge(curMonth, curDay, curYear, birthMonth, birthDay, birthYear);
     rateResult = calcRateClass(age, gender);
     displayResults(gender, age, rateResult);        

     System.out.println("\nEighth test case: Renter is 76 and female (68/270)...");
     birthMonth = 1;
     birthDay = 1;
     birthYear = 1940;
     gender = "f";
     age = calcAge(curMonth, curDay, curYear, birthMonth, birthDay, birthYear);
     rateResult = calcRateClass(age, gender);
     displayResults(gender, age, rateResult);         
  }
  else
  {
     Scanner kb = new Scanner(System.in);
     System.out.println("Welcome to the car renter's rate finder.");
  // Get the gender...
  //    Your code goes here...
     private static clacGen(s.gender);
        {
         System.out.print("what is the Gender of the Renter? :");
         boolean f = female;
         boolean m = male;

         if (gender = "f")
         {
           return ("female");
         }

         else if (gender = "m")
         {
           return ("male");
         }                
     }         
     private static calcAge(birthMonth,birthDay,birthYear);
     {
        System.out.println("what is the Renters Bithday?(mm/dd/yy):");
        today_date = curMonth,curDay,curYear
        today_year = today_date.getFullYear();
        today_month = today_date.getMonth();
        today_day = today_date.getDate();
        age = today_year - birthYear;

        if ( today_month < (birthMonth - 1))
        {
           age--;
        }
        if (((birthMonth - 1) == today_month) && (today_day < birthDay))
        {
           age--;
        }
           return age;
      }


  // Get the rental rate...
     private static String calcRateClass(age, gender);
     {
        rateResult = calcRateClass(age, gender);
        return rateResult;
     }

  // Display the results...
     private static displayResults(gender, age, rateResult);
     {
        System.out.Println("Thank you");
        System.out.println("the ",gender," is ",age," years old.");
        System.out.println("the rate class is: ",rateResult);
     }
  }

} }

1 个答案:

答案 0 :(得分:1)

我讨厌打破它,但你并不是很亲密。这段代码是一个文件中所有内容的混合,它违反了所有规则。它表明缺乏对基本java概念的理解。这段代码基本上难以辨认。

以下是您可以采取的步骤,以使您的代码和编译器更好地协同工作。我没有特别的顺序介绍它们。

1)分离您的生产和测试代码:

RentalsRate.java - 应该定义程序行为。有如何构造代码的选项。看起来你倾向于静态方法,如果你小心你的类变量就没问题。 RentalRates.java类应该静态定义所有方法。

RentalsRateTest.java应测试该程序。你的主要功能将包含你的所有测试用例(基本上是DEBUG == true内的所有测试用例。方法调用需要一些tweeking)。

如果您将测试代码和生产代码分开,则您不需要if / else语句,并且您也可以获得在顶部定义的布尔DEBUG。

2)函数需要返回值,或关键字void

例如,以下函数声明缺少返回值:

  

private static displayResults(gender, age, rateResult)

但是该功能需要知道什么,如果有的话,一旦完成它的工作就会回到它的呼叫者。如果没有任何内容返回到被调用者,则将返回值设置为关键字void。至少,通过这种方式,该功能知道它不应该给它任何回复的呼叫者。

3)函数输入参数需要定义类型

例如,函数private static String calcRateClass(age, gender)应显示为private static String calcRateClass(int age, char gender)。如果你没有在函数声明中定义输入类型,那么在调用函数时,该函数应该如何知道输入有效输入?你的编译器会爆炸,并伴随它,你的家庭作业。

了解这一点,您现在应该返回并修复private static displayResults(gender, age, rateResult)的声明,以便还具有类型定义的输入参数以及所有其他函数

4)提取您的方法

方法定义不要进入if / else语句。它们是独立的代码片段。它们可以从代码中其他地方的if / else语句中调用,但它们的定义应该是它自己的定义。您当前在else语句中定义了方法。这是错的。事实上,如果你在我的列表中执行1)事情,这将不再是一个问题。

5)删除;来自){在方法声明中。

private static String calcRateClass(age, gender);
      {

应为

private static String calcRateClass(int age, String gender) {

6)令人困惑的递归通话

这部分是一个拙劣的递归调用。如果你不知道这意味着什么,那很好。

  private static String calcRateClass(age, gender);
     {
        rateResult = calcRateClass(age, gender);
        return rateResult;
     }

除了许多其他问题之外,问题在于你是从内部调用函数。这实际上是一个无限循环。检查你的逻辑。

这就是我现在要付出的一切。你有很多需要工作的东西,但是如果你从这些开始并回来证明你试图实现一些基础知识,我不介意引导你多一点。