q9722088
2017-06-11 08:14
采纳率: 54.5%
浏览 814
已采纳

java异常求助java异常求助java异常求助java异常求助

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

public class Milage {
HashMap CityName = new HashMap();
private int[][] milage;

public int SetCityName(){
    Scanner mc = new Scanner(System.in);
    int i = 0;
    String s = new String();
    s = mc.nextLine();
    while( true) {
        if( s.equals("###")) {
            break;
        }else {
            CityName.put(s, i);
            i++;
            s = mc.nextLine();
        }
    }
    mc.close();
    return i;
}

public void SetInt(int n) {
    Scanner sc = new Scanner(System.in);
    int miter;
    milage = new int[n][n];
    for( int i=0; i<n; i++) {
        for( int j=0; j<n; j++) {
            miter = sc.nextInt();
            milage[i][j] = miter;
        }
    }
    sc.close();
}

public int Getmil(String a,String b) {
    int f = CityName.get(a);
    int s = CityName.get(b);
    return milage[f][s];
}
public static void main(String[] args) {
    // TODO 自动生成的方法存根
    Scanner in = new Scanner(System.in);
    Milage m = new Milage();
    int n1 = m.SetCityName();
    m.SetInt(n1);
    String name1 = in.next();
    String name2 = in.next();
    System.out.println(m.Getmil(name1, name2));
    in.close();
}

}

输入:
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at Milage.SetInt(Milage.java:30)
at Milage.main(Milage.java:50)
错误语句 30:for( int i=0; i<n; i++) {
错误语句 50:String name1 = in.next();
debug到for的时候就报错了

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • 家蹲 2017-06-11 09:12
    已采纳

    mc.close()会把System.in也关掉,把mc.close()和sc.close()去掉

    点赞 评论
  • xiao哈哈哈 2017-06-11 09:22

    mc.close()和sc.close()不写

    点赞 评论
  • 家蹲 2017-06-11 09:31
     package main;
    
    import java.util.HashMap;
    import java.util.Scanner;
    
    public class SetCityName {
    HashMap<String,Integer> CityName = new HashMap();
    private int[][] milage;
    
    public int SetCityName(){
        Scanner mc = new Scanner(System.in);
        System.out.println("请输入城市名,并以###结束");
        int i = 0;
        String s = new String();
    
    
        while( true) {
               s = mc.nextLine();
            if( s.equals("###")) {
                System.out.println("各城市及编号为");
                System.out.println(CityName);
                break;
            }else {
                CityName.put(s, i);
                i++;
    
            }
        }
    
        return i;
    }
    
    public void SetInt(int n) {
          Scanner sc = new Scanner(System.in);
          System.out.println("请输入数量");
            int miter;
            milage = new int[n][n];
            for( int i=0; i<n; i++) {
                for( int j=0; j<n; j++) {
                    miter = sc.nextInt();
                    milage[i][j] = miter;
    
                }
            }
    
    
    
    }
    
    public int Getmil(String a,String b) {
        int f =  CityName.get(a);
        int s =  CityName.get(b);
        return milage[f][s];
    }
    public static void main(String[] args) {
        // TODO 自动生成的方法存根
    
    
        SetCityName m = new SetCityName();
       int n1 = m.SetCityName();//返回最后的编号
    
        m.SetInt(n1);
        System.out.println("请输入两个城市名称");
        Scanner in = new Scanner(System.in);
        String name1 = in.next();
        String name2 = in.next();
        in.close();
        System.out.println(m.Getmil(name1, name2));
    
    }
    
    }
    

    改了些地方,虽然不知道这个代码是做什么的/笑哭

    input
    请输入城市名,并以###结束
    北京
    上海
    ###
    各城市及编号为
    {上海=1, 北京=0}
    请输入数量
    0
    1
    2
    3
    请输入两个城市名称
    北京
    上海
    1

    点赞 评论

相关推荐 更多相似问题