gpt4 book ai didi

java - 在 main 中调用带有引用变量的方法

转载 作者:行者123 更新时间:2023-12-01 13:50:25 24 4
gpt4 key购买 nike

我试图在包含其他方法的循环之外调用主程序中 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方法有两个参数;您只提供一个。另外,我不知道您在哪里定义作为第一个参数传递的变量 strList 。它是一个静态变量,在您未显示的代码中定义吗?

看到第二个参数应该是一个Scanner,也许这对你有用:

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

将来您应该提供来自编译器的完整错误消息。让人们猜测错误可能是什么是没有成效的。

关于java - 在 main 中调用带有引用变量的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20005930/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com