Closed. This question needs details or clarity。它当前不接受答案。
                            
                        
                    
                
            
                    
                
                        
                            
                        
                    
                        
                            想改善这个问题吗?添加详细信息并通过editing this post阐明问题。
                        
                        4年前关闭。
                    
                
        

这是问题所在:旅馆营业员在文本文件中输入营业额。每行包含以下内容,并用分号分隔:客户名称,出售的服务(例如Dinner,Conference,Lodging等),销售金额以及该事件的日期。编写一个程序,读取该文件并显示每个服务类别的总金额。如果文件不存在,显示格式错误,则显示错误。

这是我所拥有的:

package practice;

import java.io.*;
import java.io.FileNotFoundException;
import java.util.*;
import java.io.PrintWriter;

public class practice1 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws FileNotFoundException {
    Scanner console = new Scanner(System.in);
    System.out.println("Please enter input file name: ");
    String inputFileName = console.next();
    System.out.println("Please enter desired output file name: ");
    String outputFileName = console.next();
    console.useDelimiter(";");

    //Construct Scanner and PrintWriter objects for reading and writing

    File inputFile = new File(inputFileName);
    Scanner in = new Scanner(inputFile);
    PrintWriter out = new PrintWriter(outputFileName);
    double dinnerTotal = 0;
    double conferenceTotal = 0;
    double lodgingTotal = 0;
    double total = dinnerTotal + conferenceTotal + lodgingTotal;

    //Read the input and write the output


    while (in.hasNext())
    {
        String line = in.nextLine();
        String[] parts = line.split(";");
        if(parts[2].equals("Conference")){
            conferenceTotal = conferenceTotal+ Double.parseDouble(parts[3]);
        } else if(parts[2].equals("Dinner")){
            dinnerTotal += Double.parseDouble(parts[3]);
        } else if(parts[2].equals("Lodging")){
            lodgingTotal += Double.parseDouble(parts[3]);


        }
    }

    out.printf("Dinner Total:", dinnerTotal);
    out.println();
    out.printf("Conference Total:", conferenceTotal);
    out.println();
    out.printf("Lodging Total", lodgingTotal);
    out.println();
    out.printf("Total", total);

    in.close();
    out.close();
    }


}


该程序将创建一个新文件,如下所示:
晚餐总数
会议总数
住宿总数


它不会打印实际总数。如何打印总计?

最佳答案

double total = dinnerTotal + conferenceTotal + lodgingTotal;行需要低于while循环。

关于java - 卡在酒店营业员Java程序上,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36414143/

10-13 05:40