This question already has answers here:
Reached end of file while parsing compilation error

(3个答案)


7个月前关闭。




我收到错误BankInterest(第93行):解析时到达文件末尾,有人知道我为什么得到这个吗?这是下面的代码,因此您可以确定可能是什么问题。

先感谢您。

import java.nio.file.*;
import java.util.*;
import java.io;

public class BankInterest {

public static void main(String[] args) throws IOException {

/* TASK 1: Declare variables */

String accountType;
double principal;
double rate;
double balance;
int year;

 /* Check if the expected command line is provided */

  if (args.length < 1) {
 /* Display the Usage */
   System.out.println("Usage: java BankInterest interestRateFileName");
 /* Programs quits with an error code */
   System.exit(-1);
  }
  /* TASK 2: Read interest rates from a file */
 try {

   Scanner x = new Scanner(Paths.get("commbank.txt"));
   System.out.println(x.nextDouble());
  } catch (IOException e) {

  /* TASK 3: Take user input - Which Account */

  Scanner keyboard = new Scanner(System.in);
  System.out.println("Which Account: ");
  System.out.println("1 - Savings");
  System.out.println("2 - Term Deposits");

  String line = keyboard.nextLine();

  if (line.equals("1")) {
   accountType = "Savings";
  } else if (line.equals("2")) {
   accountType = "Term Deposits";
  }

 /* TASK 4: Take user input - Principal and Period */

  Scanner input = new Scanner(System.in);
  System.out.println("Principal: ");
  principal = keyboard.nextDouble();

  System.out.println("Years: ");
  year = keyboard.nextInt();

  /* TASK 5: Calculate balance for the chosen account type */

  if (accountType == "Savings") {
   balance = principal * Math.pow((1 + rate / 100), year);
  } else if (accountType == "Term Deposits") {
   balance = (principal * rate * time) / 100;
  }

  /* TASK 6: Display results */

 if (accountType == "Savings") {
  System.out.println("");
  System.out.println("The Compound Interest is: " + balance);enter code here
 } else if (accountType == "Term Deposits") {
  System.out.println("");
  System.out.println("The Simple Interest is: " + balance);
 } else {
  System.out.println("");
  System.out.println("Error! Account is not recognized.");
 }
}

最佳答案

您在文件末尾缺少两个大括号。


一关你的主
和另一个关闭您的课程。


同样,在比较accountType作为注释中提到的Satya时,应使用equals()。

10-04 11:07