2017-10-08 56 views
-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 ; 
//} 

} 
+1

什麼是掃描? – glennsl

+0

您需要在方法調用之前聲明變量'scan'。在這種情況下,它將是:'Scanner scan = new Scanner(System.in);' –

+0

@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)' –

回答

0

您遺漏了變量聲明。由於您使用的是多個位置,因此我建議將這些方法提供給同一個實例。

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; 
    } 

}