简体   繁体   English

如果用户输入了无效密码,该如何编码?

[英]If the user enters an invalid code how to code it?

If the user enters an invalid code, display You selected an invalid option and set the price to 0. 如果用户输入了无效的代码,则显示“您选择了无效的选项,并将价格设置为0”。

import java.util.Scanner;
public class ShadyRestRoom {

    public static void main(String[]args) {
        Scanner scanner= new Scanner(System.in); 
        int QueenBed = 125;
        int KingBed = 139;
        int KingPullout = 165;

         System.out.println("Choose: 1 for a Queen bed Choose: 2 for a King bed or Choose: 3 for a King bed with a pullout couch");
         int RoomChoice= scanner.nextInt();

         if (RoomChoice == 1) 
             System.out.println("$125 for a Queen bed");
         if (RoomChoice == 2)
             System.out.println("$139 for a King bed");
         if (RoomChoice == 3)
             System.out.println("$165 for a King bed with a pullout couch");

         while (RoomChoice == 4) 
             System.out.println("ERROR Please enter a valid Choice");
             RoomChoice = scanner.nextInt();
         if (RoomChoice == 8)
             System.out.println("ERROR Please enter a valid Choice");
             RoomChoice = scanner.nextInt();    
    }
}

I can't seem to figure out how to set it so it does= ( If the user enters an invalid code, display You selected an invalid option and set the price to 0.) 我似乎无法弄清楚如何设置它,所以它=(如果用户输入无效的代码,则显示“您选择了无效的选项并将价格设置为0。”)

One way do accomplish a choice is a switch statement. 完成选择的一种方法是switch语句。 It is made for it. 它是为此做的。 So a choice calls for a switch. 因此,选择需要进行切换。

Now in a switch you have for every valid choice one "case" statement. 现在,在开关中,对于每个有效选择,您都有一个“ case”语句。 Additionally in the end there is a "default" for everything else not mentioned explicitely. 此外,最后还有一个“默认值”,用于未明确提及的所有其他内容。

You can use that default to "cheat" and reset a variable to a defined value, of your choice. 您可以使用默认值“ cheat”,并将变量重置为定义的值。

Now that again makes the condition in the loop simpler as it isn't so vague("some input except..") anymore but there is only one defined value that says "another loop". 现在,这又使循环中的条件变得更简单了,因为它不再那么模糊(“除了..以外还有其他输入”),但是只有一个定义的值表示“另一个循环”。

I took "-1" as an indicator, that says "something went wrong". 我以“ -1”作为指标,表示“出了点问题”。 But you can use whatever makes sence to you as "escape"(thats what such values are called: -1 is an "escape code"). 但是您可以将任何有意义的信息用作“转义符”(这就是所谓的值:-1是“转义码”)。 And so the while checks for my escape "-1" and continues asking until the user gives something in range. 因此,while会检查我的转义符“ -1”,并继续询问,直到用户给出范围内的内容为止。

public static class ShadyRestRoom {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int QueenBed = 125;
        int KingBed = 139;
        int KingPullout = 165;
        int RoomChoice = -1;
        do { 
            System.out.println("Choose: 1 for a Queen bed Choose: 2 for a King bed or Choose: 3 for a King bed with a pullout couch");
            RoomChoice = scanner.nextInt();
            switch (RoomChoice) {
                case 1:
                    System.out.println("$125 for a Queen bed");
                    break;
                case 2:
                    System.out.println("$139 for a King bed");
                    break;
                case 3:
                    System.out.println("$165 for a King bed with a pullout couch");
                    break;
                //you can also explicitely take a group of numbers that need to be treated the same way:
                case 4:
                case 8:
                default:    //this is the default for everything that wasn't metioned before
                    RoomChoice = -1;
                    System.out.println("ERROR Please enter a valid Choice");
            }
        } while (-1 == RoomChoice);

    }
}

Btw: it is a convention to use lower case letters for variables. 顺便说一句:习惯上将小写字母用于变量。 So "roomChoice" instead of "RoomChoice" would be better. 因此,以“ roomChoice”代替“ RoomChoice”会更好。 With that you could see immediately that is a variable and not class. 这样,您可以立即看到这是一个变量而不是类。

暂无
暂无

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

相关问题 在用户输入-1之前,如何循环这段代码? - How do I loop this code until the user enters -1? 用户在数组中输入 10 个数字我将如何编写代码来查找该数字并输出它在数组中的位置? - user enters 10 numbers into an array how would I write code to find that number and output what position it is in the array? Java-如果用户输入无效的输入,如何返回异常? - Java - How would I return an exception if the user enters a invalid input? 如何在下面的代码中使用循环,以便在用户再次输入任何字母程序时要求输入数字而不是终止程序? - how can loop be used in following code so that if user enters any alphabet program again ask to enter a nuumber rahter than terminating a program? 当用户输入无效数据时,避免NullPointerException - Avoiding a NullPointerException when the user enters invalid data 如果用户输入无效选项,则请求相同的输入 - Requesting the same input if user enters an invalid option 如何分割用户输入的字符串 - How to split a string that the user enters 当用户输入错误的字符或无效的输入数据时,如何显示“打印”错误? - How to display a “print” error when the user enters wrong character or invalid input data? 在Websphere中,如何在用户输入无效网址时更改我重定向到的页面? - In Websphere, how do I change the page I get redirected to when a user enters an invalid url? 当用户在Spring启动应用程序中输入无效URL时,如何显示自定义404页面? - How to show custom 404 page when user enters invalid URL in spring boot application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM