使用数组创建自定义方法打印值

时间:2017-11-25 23:20:32

标签: java arrays methods

我试图让这个方法打印String [] strArr中包含的四个字符串消息之一。我试过通过调用main方法中的方法,通过键入许多不同形式的simpleArray()来尝试这样做;我试过填写括号,用几种不同的方式写它,但没有任何效果。我实际上已经工作了好几天了,通常我会放弃并继续使用代码的不同部分。

虽然看起来似乎不切实际,但我确实需要按照它的方式编写方法,因为我的项目标准声明它必须包含一个参数并返回void。

public static void simpleArray(String[] greetings) {

    String[] strArr = {"Welcome To CWU BANK!", "Thank you for using CWU ATM!", "Please insert DEBIT card", "We value your business!"};

        int i = (int)(Math.random() * strArr.length);
        System.out.println(strArr[i]);
    }

这是我的主要方法,我尝试在第6行调用自定义方法。

public static void main(String[] args) throws IOException   {

    double amountToWithdrawl;
    double saveRandomBalance;
    double remainingBalance; 
    simpleArray();
    printStartupMessage();

    Scanner keyboard = new Scanner(System.in);
    Scanner keyboardDouble = new Scanner(System.in);

    saveRandomBalance = getRandomBalance();

    System.out.println("CHECKING BALANCE**** $" + saveRandomBalance);
              System.out.println("Would you like to withdrawl from CHECKING****? Y/N");
    String proceedWithWithdrawl = keyboard.nextLine();

    while (!proceedWithWithdrawl.equalsIgnoreCase("y") && !proceedWithWithdrawl.equalsIgnoreCase("n") 
                 && !proceedWithWithdrawl.equalsIgnoreCase("yes") && !proceedWithWithdrawl.equalsIgnoreCase("no")) 
    {
        System.out.println("Invalid response. Enter [Y] or [N].");
                 proceedWithWithdrawl = keyboard.next();
    } 

        switch(proceedWithWithdrawl)
        {
            case "N": 
            case "n":
            case "nO":   
            case "NO":
            case "No":
                System.out.println("Returning card... please wait...");
                System.out.println("Card returned. Thank you for using CWU Bank!");
                break;
            case "yeS":
            case "YEs":
            case "yEs":
            case "yES":   
            case "YeS":
            case "YES":
            case "Yes": 
            case "yes":
            case "y":
            case "Y":  
                System.out.println("Enter amount to withdrawl: ");
                amountToWithdrawl = keyboardDouble.nextDouble();
                remainingBalance = saveRandomBalance - amountToWithdrawl;
                remainingBalance = Math.round(remainingBalance * 100);
                remainingBalance = remainingBalance/100;

                if (amountToWithdrawl % 20 == 0 && amountToWithdrawl <= saveRandomBalance)
                {

                    System.out.println("Dispensing...");
                    System.out.println("ACCOUNT BALANCE: $" + remainingBalance);
                    System.out.println("$" + amountToWithdrawl + " has been withdrawn from CHECKING****");
                    System.out.println("Returning card... please wait...");
                    System.out.println("Card returned. Thank you for using CWU Bank!");
                    //CallDollarBill.dollarBill();
                }
                else if (amountToWithdrawl > saveRandomBalance)
                {
                    System.out.println("Insufficient Balance.");
                }
                else if (amountToWithdrawl % 20 != 0)
                {
                    System.out.println("Please enter multiples of 20.");
                }
                //else
                //{
                //     System.out.println("invalid input");
                //}
        } 

}

现在,它提供的错误如下。

firstDraftFinal.java:69:error:类firstDraftFinal中的方法simpleArray不能应用于给定的类型;                   simpleArray();                   ^

必需:字符串[]   发现:没有争论   原因:实际和正式的参数列表长度不同

1错误

我知道问题的一部分可能是int i(和strrArr)是整数,但我不知道该怎么做。我雇了一个导师,但我没时间了。我也知道switch语句效率不高,我会改变它。 谢谢。

2 个答案:

答案 0 :(得分:0)

您有编译错误,因为您没有将任何参数传递给需要它们的方法,只是将任何字符串数组传递给您的方法,因为您在此方法中不使用传递参数:

public static void main(String[] args) throws IOException   {

    double amountToWithdrawl;
    double saveRandomBalance;
    double remainingBalance; 
    simpleArray(new String[0]);
    printStartupMessage();

要继续重构你的代码,你可能想要摆脱这个论点(如果代码的其他部分可能会使用它,请注意)并重写它:

public static void printGreetings() {
     String[] greetings = {"Welcome To CWU BANK!",
                             "Thank you for using CWU ATM!",
                             "Please insert DEBIT card",
                             "We value your business!"
     };
     int random = new Random().nextInt(greetings.length);
     System.out.println(greetings[random]);
}

答案 1 :(得分:0)

您当前的代码指定了未使用的参数;虽然不清楚你为什么要这样做,但你可以简单地传递null。但是,也许你打算通过问候清单;即见下面的第二个版本。



    class Test {
        public static void main(String[] args) {
            // Per your current code.
            for (int i=0; i<10; i++) simpleArray(null);
            System.out.println("");

            // What you may be looking for.
            String[] strArr = { "Welcome To CWU BANK!", "Thank you for using CWU ATM!", "Please insert DEBIT card",
            "We value your business!" };
            for (int i=0; i<10; i++) simpleArray2(strArr);
        }

        public static void simpleArray(String[] greetings) {
            String[] strArr = { "Welcome To CWU BANK!", "Thank you for using CWU ATM!", "Please insert DEBIT card",
                    "We value your business!" };

            int i = (int) (Math.random() * strArr.length);
            System.out.println(strArr[i]);
        }

        public static void simpleArray2(String[] greetings) {
            int i = (int) (Math.random() * greetings.length);
            System.out.println(greetings[i]);
        }

    }