2

我尝试编译这个,并得到一个错误说: $javac Question2.java 2>&1 Question2.java:55: error:达到文件结尾同时解析} ^ 1错误类型不能被解析为变量

我很确定代码是正确的,但是我缺少一点东西。该程序旨在检查机票类型(标准/贵宾/限制)和折扣(无/学生/养老金领取者),如果选择了贵宾,则没有折扣,学生和养老金领取者有5%和10%的折扣分别,但我无法理清输入法或类型声明。

任何形式的帮助表示赞赏。PS,我是一名正在学习 Java 的学生,似乎无法找到解决这个问题的确切方法,因此我在这里发布了一个问题。

import java.util.Scanner;



public class Question2 {

public static void main(String args[]){

    Scanner userinput = new Scanner(System.in);

    String ticket ;
    System.out.print(" Type of Ticket: ");
    ticket = userinput.next();

    String discount; 
    System.out.print("What sort of discount do you have?");
    discount = userinput.next();

    int standard = 40;
    int restricted = 20;
    int VIP = 60;



    if ((ticket == "standard") && (type == "student")) {
        double standard_student = standard * 0.95;
        }

    if ((ticket == "standard") && (type == "pensioners")) {
        double standard_pensioners = standard * 0.90;
        }


    if ((ticket == "restricted") && (type == "student")) {
        double restricted_students = restricted * 0.95;
    }


    if ((ticket == "restricted") && (type == "pensioner")) {
        double restricted_pensioner = restricted * 0.90;
    }

    if (ticket=="standard")
        System.out.print("Your ticket costs $.");

    if (ticket == "restricted")
        System.out.print("Your ticket costs $.");


    if (ticket== "VIP")
    System.out.print("Your discount type cannot be used with your requested ticket type.");


    System.out.println ("Thank you!");
    }
4

2 回答 2

2

您的Question2班级缺少一个结束大括号:

import java.util.Scanner;

public class Question2 {
  public static void main(String args[]) {
    Scanner userinput = new Scanner(System.in);

    String ticket ;
    System.out.print(" Type of Ticket: ");
    ticket = userinput.next();

    String discount; 
    System.out.print("What sort of discount do you have?");
    discount = userinput.next();

    int standard = 40;
    int restricted = 20;
    int VIP = 60;

    if ((ticket.equals("standard")) && (type.equals("student"))) {
      double standard_student = standard * 0.95;
    }

    if ((ticket.equals("standard")) && (type.equals("pensioners"))) {
      double standard_pensioners = standard * 0.90;
    }

    if ((ticket.equals("restricted")) && (type.equals("student"))) {
      double restricted_students = restricted * 0.95;
    }


    if ((ticket.equals("restricted")) && (type.equals("pensioner"))) {
      double restricted_pensioner = restricted * 0.90;
    }

    if (ticket.equals("standard"))
      System.out.print("Your ticket costs $.");

    if (ticket.equals("restricted"))
      System.out.print("Your ticket costs $.");

    if (ticket.equals("VIP"))
      System.out.print("Your discount type cannot be used with your requested ticket type.");

    System.out.println ("Thank you!");
  }
} // <--- Add this

使用适当的缩进可以很容易地避免这种类型的错误。事实上,大多数 IDE 都可以自动为您格式化代码。

另外:请注意,.equals()如果要比较字符串值,则需要使用。使用==运算符只会比较引用。我已经更新了我的示例。

于 2013-10-24T22:25:46.790 回答
0

比较字符串的 VALUE 时,始终使用 equals 方法,而不是 == 。

例如:if (type.equals("student"))

于 2013-10-24T22:28:40.940 回答