简体   繁体   English

通过方法正确返回和传递/调用变量

[英]Properly returning and passing/calling variables through methods

I have the method readAccts and in it I am returning the varaible numAccts.我有 readAccts 方法,并在其中返回变量 numAccts。 When I print to the console to try to see the value of numAccts as it runs, it shows as 6, which is correct according to my input file.当我打印到控制台以尝试查看运行时 numAccts 的值时,它显示为 6,根据我的输入文件,这是正确的。 However, I think when it returns back to main it sets itself back to 0?但是,我认为当它返回 main 时,它会将自己设置回 0? Any input is appreciated and thank you.感谢您提供任何意见。

import java.io.*;
import java.util.*;


public class HW7 {

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

        File myFile = new File("HW71input.txt"); // Making a new text file for input
        Scanner inputFile = new Scanner(myFile); // Letting it read the file
        PrintWriter outputFile = new PrintWriter("HW71output.txt"); // Creating the text output file


        final int MAX_NUM = 50;

        int[] acctNum = new int[MAX_NUM];
        double[] balance = new double[MAX_NUM];
        int numAccts = 0;
        int maxAccts = 50;
        char myChoice;

        readAccts(acctNum, balance, maxAccts);
        printAccts(acctNum, balance, numAccts, outputFile);
        
        
        do {

            menu();

            myChoice = inputFile.next().charAt(0);

            System.out.println("You chose: " + myChoice);
            System.out.println("");

            switch (myChoice) {

            case 'W':
            case 'w':

                break;

            case 'D':
            case 'd':

                break;

            case 'N':
            case 'n':

                break;

            case 'B':
            case 'b':

                break;

            case 'Q':
            case 'q':

                break;

            case 'X':

                break;

            default:
                
                System.out.println("Wrong Option!");
                break;
            }
            
        } while(inputFile.hasNext());


        inputFile.close();
        outputFile.close();

    }

    public static int readAccts(int[] acctNum, double[] balance, int maxAccts) throws IOException{

        File myFile = new File("initAccts.txt");
        Scanner inputFile = new Scanner(myFile);
        
        int numAccts = 0;
        int j = 0;
        int n = 0;

        while(inputFile.hasNext()) {
            
            acctNum[n] = inputFile.nextInt();
            n++;
            balance[j] = inputFile.nextDouble();
            j++;
            numAccts++;

        }
        
        System.out.println("\n\n\n\n\n" + numAccts);

        inputFile.close();
        return numAccts;

    }

    public static void menu() {

        System.out.println("What action would you want to do?:\n"
                + "W for Withdrawl\n"
                + "D for Deposit\n"
                + "N for New Account\n"
                + "B for Balance\n"
                + "Q to Quit\n"
                + "X for Delete Account\n");
    }

    public static void printAccts(int[] acctNum, double[] balance, int numAccts, PrintWriter HW71output) throws IOException{
        
        for(int i = 0; i < numAccts; i++) {

            HW71output.println("Account Number: " + acctNum[i]);
            HW71output.println("Balance: " + balance[i]);
            HW71output.println("");

        }


    }



}

You're ignoring the return value from readAccts .您忽略了readAccts的返回值。 You need to assign it to a variable to save it.您需要将其分配给一个变量以保存它。 Eg:例如:

numAccts = readAccts(acctNum, balance, maxAccts);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM