Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
嵌套另一个Java If/Else语句以在输入错误字符时打印_Java_Java Io - Fatal编程技术网

嵌套另一个Java If/Else语句以在输入错误字符时打印

嵌套另一个Java If/Else语句以在输入错误字符时打印,java,java-io,Java,Java Io,因此,我在让我的程序打印出时遇到了一个问题。根据输入的响应,您必须输入C或F或Y或N。我曾尝试将其他if-else语句放入程序中,但它不能正确运行,并中断for循环 public static void main(String[] args) { for (int i = 0; i < 4; i++) { System.out.println("Programmed by ."); double standardCompact = 30.50;

因此,我在让我的程序打印出
时遇到了一个问题。根据输入的响应,您必须输入C或F或Y或N
。我曾尝试将其他if-else语句放入程序中,但它不能正确运行,并中断for循环

public static void main(String[] args) {

    for (int i = 0; i < 4; i++) {
        System.out.println("Programmed by .");
        double standardCompact = 30.50;
        double couponCompact = ((30.50)-(30.50 * 0.07));
        double standardFullSize = 40.50;
        double couponFullSize = ((40.50)-(40.50 * 0.07));

        // Scanner Input
        Scanner input = new Scanner(System.in);
        System.out.print("Rent a Car? [Y or N]: ");

        // Response String
        String response = input.next().toUpperCase();

        if (response.equals("N")) {
            System.out.println("You entered no. Bye.");
        }
        else if (response.equals("Y")) {
            System.out.print("Compact or Full-Size? [C or F]: ");
            response = input.next().toUpperCase();
            if (response.equals("C")) {
                System.out.println("You selected Compact.");

                //case1
                System.out.print("Have coupon? [Y or N]: ");
                response = input.next().toUpperCase();
                if (response.equals("N")) {
                    System.out.println("Price is " + standardCompact + " per day.");
                }
                else if (response.equals("Y")) {
                    System.out.println("Price is " + couponCompact + " per day.");
                }
            }
            else if (response.equals("F")) {
                System.out.println("You have selected Full-Size. ");

                //case 2
                System.out.print("Have coupon? [Y or N]: ");
                response = input.next().toUpperCase();
                if (response.equals("N")) {
                    System.out.println("Price is " + standardFullSize + " per day.");
                }
                else if (response.equals("Y")) {
                    System.out.println("Price is " + couponFullSize + " per day.");
                }
            }
        }
    }
}
publicstaticvoidmain(字符串[]args){
对于(int i=0;i<4;i++){
System.out.println(“编程者”);
双标准紧凑型=30.50;
双耦合冲击=((30.50)-(30.50*0.07));
双标准全尺寸=40.50;
双联轴节全尺寸=((40.50)-(40.50*0.07));
//扫描仪输入
扫描仪输入=新扫描仪(System.in);
系统输出打印(“租车?[Y或N]:”;
//响应字符串
字符串响应=input.next().toUpperCase();
if(响应等于(“N”)){
System.out.println(“您输入了no.Bye.”);
}
else if(响应等于(“Y”)){
系统输出打印(“紧凑型或全尺寸?[C或F]:”;
response=input.next().toUpperCase();
如果(响应等于(“C”)){
System.out.println(“您选择了Compact”);
//案例1
系统输出打印(“有优惠券吗?[Y或N]:”;
response=input.next().toUpperCase();
if(响应等于(“N”)){
System.out.println(“价格为每天“+standardCompact+”);
}
else if(响应等于(“Y”)){
System.out.println(“价格为每天“+couponCompact+”);
}
}
else if(响应等于(“F”)){
System.out.println(“您已经选择了全尺寸。”);
//案例2
系统输出打印(“有优惠券吗?[Y或N]:”;
response=input.next().toUpperCase();
if(响应等于(“N”)){
System.out.println(“价格为“+标准全尺寸+”每天”);
}
else if(响应等于(“Y”)){
System.out.println(“价格为每天“+couponFullSize+”);
}
}
}
}
}

构造if语句时,
else
可用于处理以前的
if
else if
语句未捕获的所有其他条件

int x = 0;

if(x ==1){
    System.out.println("1");
}else if(x ==2){
   System.out.println("2");
}else{
  //execute if none of the other conditionals are true
  System.out.println("Other Conditions not met");
}

//Outputs:  Other Conditions not met

考虑使用布尔标志和“else”语句的组合。因此,如果用户未输入N或Y,请将此标志设置为true。类似地,如果用户未输入C或F,则将此标志设置为true

稍后,在for循环的末尾,检查此标志。如果是真的,请打印您的邮件。这可能值得一试

public static void main(String[] args) {

    boolean notValidCharacter;

    for(int i = 0; i < 4; i++) {

        notValidCharacter = false;

        System.out.println("Programmed by .");

        double standardCompact = 30.50;
        double couponCompact = ((30.50)-(30.50 * 0.07));
        double standardFullSize = 40.50;
        double couponFullSize = ((40.50)-(40.50 * 0.07));

        //Scanner Input
        Scanner input = new Scanner(System.in);
        System.out.print("Rent a Car? [Y or N]: ");

        //Response String
        String response = input.next().toUpperCase();

        if (response.equals("N")){
            System.out.println("You entered no. Bye. ");
        }
        else if (response.equals("Y")) {
            System.out.print("Compact or Full-Size? [C or F]: ");

            response = input.next().toUpperCase();
            if (response.equals("C")) {
                System.out.println("You selected Compact. ");

                //case1
                System.out.print("Have coupon? [Y or N]: ");
                response = input.next().toUpperCase();
                if (response.equals("N")) {
                    System.out.println("Price is" + " " + standardCompact + " " + "per     day.");
                }
                else if (response.equals("Y")) {
                    System.out.println("Price is" + " " + couponCompact + " " + "per day.");
                }
            }
            else if(response.equals("F")) {
                System.out.println("You have selected Full-Size. ");

                //case 2

                System.out.print("Have coupon? [Y or N]: ");
                response = input.next().toUpperCase();
                if (response.equals("N")) {
                    System.out.println("Price is" + " " + standardFullSize + " " +    "per day.");
                }
                else if (response.equals("Y")) {
                    System.out.println("Price is" + " " + couponFullSize + " " + "per day.");
                }
            }
            else {
                notValidCharacter = true;
            }
        }
        else {
            notValidCharacter = true;
        }

        if (notValidCharacter) {
            System.out.println("You must enter either C or F or Y or N");
        }
    }
}
publicstaticvoidmain(字符串[]args){
布尔notValidCharacter;
对于(int i=0;i<4;i++){
notValidCharacter=false;
System.out.println(“编程者”);
双标准紧凑型=30.50;
双耦合冲击=((30.50)-(30.50*0.07));
双标准全尺寸=40.50;
双联轴节全尺寸=((40.50)-(40.50*0.07));
//扫描仪输入
扫描仪输入=新扫描仪(System.in);
系统输出打印(“租车?[Y或N]:”;
//响应字符串
字符串响应=input.next().toUpperCase();
if(响应等于(“N”)){
System.out.println(“您输入了no.Bye.”);
}
else if(响应等于(“Y”)){
系统输出打印(“紧凑型或全尺寸?[C或F]:”;
response=input.next().toUpperCase();
如果(响应等于(“C”)){
System.out.println(“您选择了Compact”);
//案例1
系统输出打印(“有优惠券吗?[Y或N]:”;
response=input.next().toUpperCase();
if(响应等于(“N”)){
System.out.println(“价格为每天“+”+standardCompact+”);
}
else if(响应等于(“Y”)){
System.out.println(“价格为每天“+”+couponCompact+”);
}
}
else if(响应等于(“F”)){
System.out.println(“您已经选择了全尺寸。”);
//案例2
系统输出打印(“有优惠券吗?[Y或N]:”;
response=input.next().toUpperCase();
if(响应等于(“N”)){
System.out.println(“价格为每天“+”+standardFullSize+”);
}
else if(响应等于(“Y”)){
System.out.println(“价格为每天“+”+couponFullSize+”);
}
}
否则{
notValidCharacter=true;
}
}
否则{
notValidCharacter=true;
}
if(notValidCharacter){
System.out.println(“您必须输入C或F或Y或N”);
}
}
}

你熟悉
其他方面的知识吗
?老实说,不太熟悉,我还在学习java,我在这里很不在行。到目前为止,我知道If,Else If,Else哪个是默认的正确答案?您只需要在两个条件句中添加
Else
子句。在第一个条件中,您检查
N
,然后检查
Y
,如果他们输入了
Z
?听起来你需要一个
else
。在第二个条件中,您检查
C
,然后检查
F
,那么如果他们输入
X
?可以使用另一个点
else
。这有用吗?