无法解析方法scanMark()

时间:2017-08-01 15:15:48

标签: java

我制作的程序就像是一个标记日记。我被困在用户输入。我想创建一个接受用户输入并将其放入HashMap的方法。我想我的理解方法中存在一个问题,相互之间有效。

        import java.io.*;
        import java.util.*;


        public class Main {
            HashMap marks;

            public void scanMark(){
                Scanner scan = new Scanner(System.in);
                System.out.println("Class Mark");

                String classname = scan.next();
                int mark = scan.nextInt();
                marks.put(classname, mark);
                scan.close();
            }
            public static void main(String[] args) {
                HashMap<String, Integer> markList = new HashMap<>();

                System.out.println("1.Add Mark\n2.Delete Mark\n3.Show Mark");
                Scanner sc = new Scanner(System.in);
                int x = sc.nextInt();
                sc.close();
                if(x == 1){
                    markList.scanMark();
                }
       }
       }

3 个答案:

答案 0 :(得分:1)

scanMark是类Main的一种方法,所以你不能这样做:

markList.scanMark(); 

因为markListHashMap个对象且没有方法scanMark

因此您可以创建Main类的实例,并在该对象上调用方法scanMark

public static void main(String[] args) {
    Main mApp = new Main();
    HashMap<String, Integer> markList = new HashMap<>();

    System.out.println("1.Add Mark\n2.Delete Mark\n3.Show Mark");
    Scanner sc = new Scanner(System.in);
    int x = sc.nextInt();
    sc.close();
    if (x == 1) {
        mApp.scanMark();
    }
}

答案 1 :(得分:1)

您无法在HashMap上调用scanMark方法,因为HashMap没有该方法。 scanMark方法位于Main类中,因此您需要在Main实例上调用scanMark方法。所以你的主要看起来如下:

               public static void main(String[] args) {
                HashMap<String, Integer> markList = new HashMap<>();
  `            `Main main = new Main(); //create an instance of Main
                System.out.println("1.Add Mark\n2.Delete Mark\n3.Show Mark");
                Scanner sc = new Scanner(System.in);
                int x = sc.nextInt();
                sc.close();
                if(x == 1){
                    main.scanMark(); //call scanMark on Main object
                }
       }

答案 2 :(得分:0)

我在代码中做了几处更改。现在正在运作;)

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * @author pakOverflow
 */
public class Main
{
    /** Constant - ADD MARK */
    private static final String ADD_MARK    = "Add Mark" ;

    /** Constant - DELETE MARK */
    private static final String DELETE_MARK = "Delete Mark" ;

    /** Constant - SHOW MARK */
    private static final String SHOW_MARK   = "Show Mark" ;

    /** Hash Map */
    private Map<String, Integer> marks ;

    private Main()
    {
        this.marks = new HashMap<String, Integer>() ;
    }

    public void scanMark()
    {
        Scanner scan = new Scanner(System.in) ;

        final int itemNumber = scan.nextInt() ;
        System.out.println("Item Number: " + itemNumber) ;

        String classname = null ;
        switch (itemNumber)
        {
            case 1:
                classname = ADD_MARK ;
                break ;             
            case 2:
                classname = DELETE_MARK ;
                break ;             
            case 3:
                classname = SHOW_MARK ;
                break ;
            default:
                classname = "Not found" ;
        }

        this.marks.put(classname, Integer.valueOf(itemNumber)) ;
        System.out.println(this.marks);

        scan.close() ;
    }

    public static void main(String[] args)
    {
        Main main = new Main() ;
        System.out.println("1." + ADD_MARK + "\n2." + DELETE_MARK + "\n3." + SHOW_MARK);        
        main.scanMark() ;
    }
}