Java:需要帮助调整我的代码以获得所需的输出

问题描述 投票:0回答:0

我的问题:我不确定如何在不弄乱我的整个代码的情况下得到双输出。

我的代码:

`\`import java.util.*;
import java.io.*;
public class LabProgram {

public static void main(String\[\] args) {
Scanner scnr = new Scanner(System.in);
int size;
double debtAmount;
String nameString, stateName;
int nameMatchCustomerCount = 0;
int debtFreeCustomerCount = 0;
int overDebtLimitCustomerCount = 0;

        int stateNameMatchCustomerCount = 0;
        int stateDebtFreeCustomerCount = 0;
        int stateOverDebtLimitCustomerCount = 0;
        
        double highestDebt = Double.NEGATIVE_INFINITY;
        String highestDebtName = "";
        double highestStateDebt = Double.NEGATIVE_INFINITY;
        String highestStateDebtName = "";
        int stateCustomerCount = 0;
        
        size = scnr.nextInt();
        debtAmount = scnr.nextDouble();
        nameString = scnr.next();
        stateName = scnr.next();
        String [] names = new String[size];
        double [] debt = new double[size];
        String [] states = new String[size];
        // fill array with data from external file (described in another section)
        
        readCustomerData(names, states, debt);
        
        // Loop the data 
        for(int index = 0; index < names.length; ++index){
            // Check if current debt values is the highest, if yes store the name
            if (debt[index] > highestDebt){
                highestDebt = debt[index];
                highestDebtName = names[index];
            }
            
            // Check if the name starts with the input name
            if (names[index].startsWith(nameString)){
                nameMatchCustomerCount += 1;
            }
            
            // Check if the debt amount is greater than the input debt amount 
            if (debt[index] > debtAmount){
                overDebtLimitCustomerCount += 1;
            }
            
            // Check if the debt amount is zero
            if (debt[index] == 0){
                debtFreeCustomerCount += 1;
            }
            
            // Checks for state specific details
            if (states[index].equals(stateName)){
                stateCustomerCount += 1;
                // Highest debt check for given state
                if (debt[index] > highestStateDebt){
                    highestStateDebt = debt[index];
                    highestStateDebtName = names[index];
                }
                
                // Name check for given state
                if (names[index].startsWith(nameString)){
                    stateNameMatchCustomerCount += 1;
                }
                
                // Over the limit debt check for given state
                if (debt[index] > debtAmount){
                    stateOverDebtLimitCustomerCount += 1;
                }
                
                // Debt free customer check for given state
                if (debt[index] == 0){
                    stateDebtFreeCustomerCount += 1;
                }
            }
            
        }
        
        // Print the US report
        System.out.println("U.S. Report");
        System.out.println("Customers: " + size);
        System.out.println("Highest debt: " + highestDebtName);
        System.out.println("Customer names that start with \"" + nameString + "\": " + nameMatchCustomerCount);
        System.out.println("Customers with debt over $" + debtAmount + ": " + overDebtLimitCustomerCount);
        System.out.println("Customers debt free: " + debtFreeCustomerCount);
        
        // Print the State specific report
        System.out.println("\n" + stateName + " Report");
        System.out.println("Customers: " + stateCustomerCount);
        System.out.println("Highest debt: " + highestStateDebtName);
        System.out.println("Customer names that start with \"" + nameString + "\": " + stateNameMatchCustomerCount);
        System.out.println("Customers with debt over $" + debtAmount + ": " + stateOverDebtLimitCustomerCount);
        System.out.println("Customers debt free: " + stateDebtFreeCustomerCount);
    }
    
    public static void readCustomerData(String [] names, String [] states, double [] debt){
        try{
            File f = new File("CustomerNames.csv");
            Scanner scnr = new Scanner(f);
            scnr.useDelimiter("[, \r\n]+");
            for(int index = 0; index < names.length; ++index) {
                names[index] = scnr.next(); // last name
                states[index] = scnr.next(); // state of residence
                debt[index] = scnr.nextDouble(); // amount of debt
            }
            scnr.close();
        }
        catch(IOException e){
            System.out.println("Failed to read the data file: ");
        }
    }

}`

你的输出开始于 美国报告 客户:3000 最高债务:罗宾逊 以“Gr”开头的客户名称:36 债务超过 $1200.0 的客户:2319 客户无债务:647

预期输出开始于: 美国报告 客户:3000 最高债务:罗宾逊 以“Gr”开头的客户名称:36 债务超过1200美元的客户:2319 客户无债务:647

java arrays
© www.soinside.com 2019 - 2024. All rights reserved.