编译代码时出现此错误:
AuthoringAssistant.java:19: error: cannot find symbol
char menuChoice = printMenu(scan);
^
symbol: variable scan
location: class AuthoringAssistant
1 error
我刚刚开始学习Java这个学期,所以我仍然试图掌握方法,我现在真的很困惑扫描仪如何在方法和main()之间进行交互。如果有人可以指出我正确的方向,这将是一个很大的帮助,因为我必须写另外5种方法来填补这个东西。
import java.util.Scanner;
public class AuthoringAssistant {
public static String scnrMethod() {
Scanner scnr = new Scanner(System.in);
String userInput = scnr.nextLine(); // userInput scans for text
return userInput;
}
public static void main(String[] args) {
System.out.println("Enter a sample text:");
scnrMethod();
System.out.println();
System.out.println("You entered: " + scnrMethod());
char menuChoice = printMenu(scan);
boolean t = true;
if (menuChoice == 'q') {
System.exit(0);
}
//else if (menuChoice == 'c') {
// System.out.println();
//}
//else if (menuChoice == 'w') {
// System.out.println();
//}
//else if (menuChoice == 'f') {
// System.out.println();
//}
//else if (menuChoice == 'r') {
// System.out.println();
//}
//else if (menuChoice == 's') {
// System.out.println();
//}
else {
t = false;
}
if (t != true) {
System.out.print("Input an option: " + menuChoice);
}
}
public static char printMenu(Scanner scan) {//provides menu for main()
System.out.println();
System.out.println("MENU");
System.out.println("c - Number of non-whitespace characters");
System.out.println("w - Number of words");
System.out.println("f - Find text");
System.out.println("r - Replace all !'s");
System.out.println("s - Shorten spaces");
System.out.println("q - Quit\n");
System.out.println("Choose an option:");
char menuChoice = scan.nextLine().charAt(0);
return menuChoice;
}
//public static int getNumOfNonWSCharacters() { //finds number of non-
//whitespace characters in the text
//
//
// return ;
//}
}
答案 0 :(得分:0)
你错过了一个变量声明。由于您使用的是Scanner
多个地方,我建议您为这些方法提供相同的实例。
import java.util.Scanner;
public class AuthoringAssistant {
private static Scanner scan = new Scanner(System.in);
public static String scnrMethod() {
String userInput = scan.nextLine(); // userInput scans for text
return userInput;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter a sample text:");
String input = scnrMethod();
System.out.println("You entered: " + input);
char menuChoice = printMenu();
boolean t = true;
if (menuChoice == 'q') {
System.exit(0);
}
else {
t = false;
}
if (t != true) {
System.out.print("Input an option: " + menuChoice);
}
}
public static char printMenu() {// provides menu for main()
System.out.println();
System.out.println("MENU");
System.out.println("c - Number of non-whitespace characters");
System.out.println("w - Number of words");
System.out.println("f - Find text");
System.out.println("r - Replace all !'s");
System.out.println("s - Shorten spaces");
System.out.println("q - Quit\n");
System.out.println("Choose an option:");
char menuChoice = scan.nextLine().charAt(0);
return menuChoice;
}
}