我的Java程序中的奇怪错误 [英] Odd error in my Java program

查看:91
本文介绍了我的Java程序中的奇怪错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Java程序,旨在接受客户的输入,然后为每个客户运行一个循环。然后用户有3个选择输入:小丑,野生动物园山姆或音乐大篷车。我只是不明白我的 if 语句有什么问题。你看,如果用户输入clowns,相应的 if 语句工作正常, if 语句执行。但是,如果用户输入safari sam或music caravan,则 if 语句不会执行。

I have a Java program that is designed to take an input of customers, then run a loop for each. Then the user has 3 choices to input: clowns, safari sam, or music caravan. I just don't understand what is wrong with my if statements. You see, if a user enters "clowns", the corresponding if statement works fine and the if statement is executed. However, if a user inputs "safari sam" or "music caravan", the if statements do not execute.

我的问题是:如果第一个 if 语句被执行,那么为什么要跳过其他2个(不是在条件满足时执行)?

My question is: If the first if statement is executed, then why are the other 2 being skipped (not executing when conditions are met)?

代码:

    import java.util.Scanner;   
    public class FunRentals {
        public static void main(String[] args) {
            Scanner new_scan = new Scanner(System.in);  
            System.out.println("Enter the amount of customers: ");
            int num_customers = new_scan.nextInt();

            for(int i = 1; i<=num_customers; i++){
                System.out.println("Please enter the service used (\"Clowns\", \"Safari Sam\", or \"Music Caravan\") for customer #"+i);
                String service_type = new_scan.next();
                String service_type_real = service_type.toLowerCase();

                if(service_type_real.equals("clowns")){ 
                    System.out.println("Please enter the amount of ADDITONAL hours");   
                    double additional_hours = new_scan.nextDouble();
                    System.out.println("The total bill for customer #" +i +" is "+ clowns(additional_hours));
                } 
                else if(service_type_real.equals("safari sam")){
                    System.out.println("Please enter the amount of ADDITONAL hours");   
                    double additional_hours = new_scan.nextDouble();
                    System.out.println("The total bill for customer #" +i +" is "+ safari_sam(additional_hours));
                } 
                else if(service_type_real.equals("music caravan")){
                    System.out.println("Please enter the amount of ADDITONAL hours");   
                    double additional_hours = new_scan.nextDouble();
                    System.out.println("The total bill for customer #" +i +" is "+ music_caravan(additional_hours));
                }

            }

        }

        public static double clowns(double a){
            double additional_cost = a*35;
            double total_cost = additional_cost + 45;
            return total_cost;
        }

        public static double safari_sam(double a){
            double additional_cost = a*45;
            double total_cost = additional_cost + 55;
            return total_cost;
        }
        public static double music_caravan(double a){
            double additional_cost = a*30;
            double total_cost = additional_cost + 40;
            return total_cost;
        }
    }


推荐答案

你需要使用 nextLine()而不是 next()来读取用户输入。 nextLine()将读取整行,但 next()只会读取下一个单词。

You need to use nextLine() instead of next() to read user input. nextLine() will read the entire line, but next() will only read the next word.

这篇关于我的Java程序中的奇怪错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆