第一行循环输出两次神秘(JAVA)

时间:2015-10-29 14:08:43

标签: java

这是一个非常奇怪的问题。

出于某种原因,当我的循环第一次迭代时,它会向用户输出一条额外的行。

这是我的代码:

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();
        double counter = 0;
        int x = 1;

        while(x<= num_customers){
            double sum = 0;

            System.out.print("Please enter the service used (\"Clowns\", \"Safari\", or \"Caravan\") for customer #"+x);
            String service_type = new_scan.nextLine();
            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();

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

            counter = counter + sum; x++;
        }
        System.out.println("The total amount of money made was $"+counter);
    }

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

问题:有人知道为什么System.out.print("Please enter the service used (\"Clowns\", \"Safari\", or \"Caravan\") for customer #"+x);在第一次迭代中输出TWICE而不是一次?我需要在明天之前传递它,并且不知道为什么循环不能正常工作并且每次迭代运行一次代码。

0 个答案:

没有答案