-1
當我編譯代碼我得到這個錯誤:掃描儀方法之間的相互作用/ main()的
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 ;
//}
}
什麼是掃描? – glennsl
您需要在方法調用之前聲明變量'scan'。在這種情況下,它將是:'Scanner scan = new Scanner(System.in);' –
@Frederik Hansen當我這樣做時拋出: '線程中的異常「main」java.util.NoSuchElementException:找不到行 \t在java.util.Scanner.nextLine(Scanner.java:1540) \t在AuthoringAssistant.scnrMethod(AuthoringAssistant.java:8) \t在AuthoringAssistant.main(AuthoringAssistant.java:18)' –