簡體   English   中英

在main中具有引用變量的調用方法

[英]Calling methods with reference variables in main

我試圖在包含其他方法的循環之外的主循環中調用strList ArrayList變量的方法,但是當我這樣做時,netbeans說找不到該符號。 我已經從其他方法中調用了方法,但是主方法給我帶來了問題。 有什么我想念的或者根本不知道的嗎? 謝謝您的幫助。

例如,我的問題是:writeList(strList);

程序

    public static void main(String[] args) throws FileNotFoundException, IOException {
        // TODO code application logic here
        boolean shouldContinue = true;
        while (shouldContinue == true) {
            nameInput();
            Scanner input = new Scanner(System.in);
            shouldContinue = promptForContinue(input);
        }
writeList(strList);
    }

    /**
     *
     * @param name
     */
    public static void nameInput() throws FileNotFoundException, IOException {

        System.out.println("What is the name of the cartoon character : ");
        Scanner keyboard = new Scanner(System.in);
        CartoonStar star = new CartoonStar();
        String name = keyboard.next();
        star.setName(name);
        typeInput(keyboard, star);

    }

    public static void typeInput(Scanner keyboard, CartoonStar star) throws FileNotFoundException, IOException {

        System.out.println("What is the cartoon character type: 1 = FOX,2 = CHICKEN,3 = RABBIT,4 = MOUSE,5 = DOG,\n"
                + "6 = CAT,7 = BIRD,8 = FISH,9 = DUCK,10 = RAT");

        boolean again = true;
        while (again) {

            try {
                input = keyboard.nextInt();

            } catch (Exception e) {
                System.out.println("Error :invalid input ");
                again = true;
                keyboard.next();
            }
            if (input > 0 && input <= 10) {
                again = false;
            }
        }

        switch (input) {
            case 1:
                star.setType(CartoonType.FOX);
                break;
            case 2:
                star.setType(CartoonType.CHICKEN);
                break;
            case 3:
                star.setType(CartoonType.RABBIT);
                break;
            case 4:
                star.setType(CartoonType.MOUSE);
                break;
            case 5:
                star.setType(CartoonType.DOG);
                break;
            case 6:
                star.setType(CartoonType.CAT);
                break;
            case 7:
                star.setType(CartoonType.BIRD);
                break;
            case 8:
                star.setType(CartoonType.FISH);
                break;
            case 9:
                star.setType(CartoonType.DUCK);
                break;
            case 10:
                star.setType(CartoonType.RAT);
                break;
        }
        popularityNumber(keyboard, star);
    }

    public static void popularityNumber(Scanner keyboard, CartoonStar star) throws FileNotFoundException, IOException {
        System.out.println("What is the cartoon popularity number?");

        boolean again = true;
        while (again) {
            try {
                popularity = keyboard.nextInt();
            } catch (Exception e) {
                System.out.println("Error : invalid input:");
                again = true;
                keyboard.next();

            }
            if (popularity >= 0 && popularity <= 10) {
                again = false;
            }

        }
        star.setPopularityIndex(popularity);
        ArrayList<Object> strList = new ArrayList<Object>();
        strList.add(star.getName());
        strList.add(star.getType());
        strList.add(star.getPopularityIndex());


        writeList(strList, keyboard);
    }

    public static void printList(ArrayList<Object> strList) {
        System.out.println(strList);
    }
    public static void writeList(ArrayList<Object> strList, Scanner keyboard) throws FileNotFoundException, IOException{
        System.out.println("Enter the file name");
        String fileName = keyboard.next();
        PrintWriter writer = new PrintWriter(fileName + ".txt");
        System.out.println("Saving. . . . ");
        System.out.println("Saved!");


    }
    //To change body of generated methods, choose Tools | Templates.

    private static boolean promptForContinue(final Scanner input) {
        boolean isValid = false;
        String userInput = "";
        do {
            System.out.print("Continue (Yes/No):");
            userInput = input.next();

            isValid = userInput.matches("Yes|No");
// if the input matches yes, ask for the required variables, else break. 
            if (!isValid) {
                System.out.println("\nInvalid entry.");
            }
        } while (!isValid);

        return userInput.equals("Yes") ? true : false;
    }

}

您的writeList方法writeList兩個參數; 您只提供一個。 另外,我看不到在哪里定義作為第一個參數傳遞的變量strList 它是一個靜態變量,由您未顯示的代碼定義嗎?

看到第二個參數應該是Scanner ,也許這對您有用:

    Scanner input = new Scanner(System.in);
    while (shouldContinue == true) {
        nameInput();
        shouldContinue = promptForContinue(input);
    }
    writeList(strList, input);

將來,您應該提供編譯器提供的完整錯誤消息。 讓人們猜測錯誤可能是徒勞的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM