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个回答

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

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

 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

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!