字符串扫描仪

时间:2019-04-11 16:24:31

标签: java eclipse

我正在尝试通过输入字母A,B,C ||来要求用户选择包裹。 a,b,c

我陷入困境,我们将不胜感激!谢谢!

public class AssignmentFive {

    public static void main(String[] args) {

        String A = "You chose package A";
        String B = "You chose package B";
        String C = "You chose package C";

        System.out.println ("Package A: For $9.95 per month, 10 hours of access is provided. Additional hours are $2.00 per hour.");
        System.out.println("Package B: For $13.95 per month, 20 hours of access is provided. Additional hours are $1.00 per hour");
        System.out.println("Package C: For $19.95 per month, unlimited access is provided");

        System.out.println();

        System.out.println("Please enter the letter of the package you want");

        Scanner value = new Scanner(System.in);
        System.out.println(value.nextLine());

        if(userinput) {
            System.out.println(B);
        }        

    }
}
  

线程“主”中的异常java.lang.Error:未解决的编译   问题:无法将userinput解析为变量   Assignments.AssignmentFive.main(AssignmentFive.java:24)

2 个答案:

答案 0 :(得分:0)

这将起作用。

public class Main {

    public static void main(String[] args) {
        String A = "You chose package A";
        String B = "You chose package B";
        String C = "You chose package C";

        System.out.println ("Package A: For $9.95 per month, 10 hours of access is provided. Additional hours are $2.00 per hour.");
        System.out.println("Package B: For $13.95 per month, 20 hours of access is provided. Additional hours are $1.00 per hour");
        System.out.println("Package C: For $19.95 per month, unlimited access is provided");

        System.out.println();

        System.out.println("Please enter the letter of the package you want");

        Scanner value = new Scanner(System.in);
        String userinput = value.nextLine().toUpperCase();

        if((userinput.equals("A")))
            System.out.println(A);
        else if ((userinput.equals("B")))
            System.out.println(B);
        else if ((userinput.equals("C")))
            System.out.println(C);
        }
  }

答案 1 :(得分:0)

您需要按照异常说明声明userInput变量。这种特殊的异常是编译时异常,您需要在运行程序之前解决它。

此外,您还需要修改if条件,因为它取决于您如何定义userInput变量。目前,它是布尔变量。如果将userInput定义为字符串,则必须使用equals方法来验证用户输入。

相关问题