【问题标题】:Odd error in my Java program我的 Java 程序中的奇怪错误
【发布时间】:2015-10-29 02:44:07
【问题描述】:

我有一个 Java 程序,旨在获取客户的输入,然后为每个客户运行一个循环。然后用户有 3 个输入选项:小丑、safari sam 或音乐大篷车。我只是不明白我的if 语句有什么问题。你看,如果用户输入“小丑”,相应的if 语句工作正常,if 语句被执行。但是,如果用户输入“safari sam”或“music caravan”,if 语句将不会执行。

我的问题是:如果执行了第一个if 语句,那么为什么会跳过另外两个语句(满足条件时不执行)?

代码:

    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;
        }
    }

【问题讨论】:

  • 尝试在分配后立即打印 service_type_real 的值;那么你应该会看到问题所在。

标签: java


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    要在控制台中读取用户提供的字符串,您必须使用 .nextLine() 所以尝试使用这个 -

     String service_type = new_scan.nextLine();
    

    这应该将您在控制台中提供的任何值存储到字符串“service_type”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-07
      • 1970-01-01
      • 2012-05-03
      • 1970-01-01
      • 2016-07-07
      • 1970-01-01
      • 2013-07-09
      • 2015-06-02
      相关资源
      最近更新 更多