简体   繁体   English

Java错误: <identifier> 预产期”

[英]Java error: <identifier> expected: “date”

I am into around 4 weeks of learning Java programming, and I am receiving this error: expected final date; 我学习Java编程大约需要4周的时间,并且收到以下错误消息:预期的最终发布日期;

I am stuck on this error message, and don't want to continue creating the program till' I can figure it out. 我一直停留在此错误消息上,直到'我能弄清楚为止,再不想继续创建该程序。 It has to be simple. 它必须很简单。 Any ideas? 有任何想法吗? I don't receive the error when I have the block below, but that's how I think my teacher wants it written as. 当我在下面的代码块中时,我没有收到错误,但这就是我认为我的老师希望将其编写为的错误。

 import java.util.Scanner;

 public class project1Naja {

  public static void main(String[] args) 
  {  

  String firstName; // To hold first name
  String lastName; // To hold last name
  int hours; // Child's hours
  final date; // Date of Service
  final double RATE; // Hourly rate
  final double TAX_RATE; // Tax percentage
  int fee; // Cost before tax added
  int taxAmount; // Tax total
  double totalFee; // Fee including tax

  // Scanner created to read input.
  Scanner childCare = new Scanner(System.in);

  String firstName; // Input. Enter first name 
  System.out.print("Enter your first name: " );
  firstName = childCare.nextLine();

  String lastName; // Input. Enter Last name 
  System.out.print("Enter your last name: " );
  lastName = childCare.nextLine();

  String hours; // Input. Enter child's hours
  System.out.print("Enter the child's hours here: " );
  hours = childCare.nextLine();

  String date; // Input. Enter child's hours
  System.out.print("Enter the child's hours here: " );
  date = childCare.nextLine();

  }
 }

A few problems here. 这里有几个问题。 At the top of the class, you have date declared as just final . 在课程的顶部,您将date声明为final Final is not a type, it means that the variable being declared cannot be changed. Final不是一种类型,它意味着声明的变量无法更改。 The correct initialization is this: 正确的初始化是这样的:

final String date;

Then at the bottom, you redefine date to be a String . 然后在底部,将date重新定义为String So delete that line and just replace it with the code I put above, and it should work. 因此,删除该行,然后将其替换为我上面输入的代码即可,它应该可以工作。

As a matter of fact, you do that with every variable. 事实上,您可以对每个变量进行此操作。 You are defining them multiple times. 您多次定义它们。 You only need to define them once: 您只需定义一次即可:

 import java.util.Scanner;

 public class project1Naja {

     public static void main(String[] args) {  

         String firstName; // To hold first name
         String lastName; // To hold last name
         int hours; // Child's hours
         final String date; // Date of Service
         final double RATE; // Hourly rate
         final double TAX_RATE; // Tax percentage
         int fee; // Cost before tax added
         int taxAmount; // Tax total
         double totalFee; // Fee including tax

         // Scanner created to read input.
         Scanner childCare = new Scanner(System.in);

         System.out.print("Enter your first name: " );
         firstName = childCare.nextLine();

         System.out.print("Enter your last name: " );
         lastName = childCare.nextLine();

         System.out.print("Enter the child's hours here: " );
         hours = childCare.nextLine();

         System.out.print("Enter the child's hours here: " );
         date = childCare.nextLine();      
     }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM