子程序将数组作为Java中的参数

时间:2016-02-26 09:33:32

标签: java arrays scope

在以下程序中,用户应输入String(城市名称),程序应返回数组中相应城市的索引。

但是我在子例程indexCities中收到错误消息:

  

“nameCity无法解析”。

我想这是变量范围的问题,但我不知道应该怎么做。 谢谢你的帮助。

import java.util.Scanner;

public class villes {

    public static void main(String[] args) {
        String cities[] = {"Vierzon","Salbris","Nouans","LB","LFSA","Orleans"};
        Scanner input = new Scanner(System.in);
        String nameCity = input.nextLine();
        indexCities(cities);
    }

    public static int indexCities(String cities[]) {
        for (int i = 0; i < cities.length; i++) {
            if(nameCity.equals(cities[i])) {
                System.out.println(i);
                break;
            }
        }
    }

}

4 个答案:

答案 0 :(得分:3)

nameCity是main方法中的局部变量。您无法在方法之外访问它。 您可以选择将nameCity作为indexCities方法中的参数传递。此外,indexCities方法的返回类型应该为void,因为您没有返回任何内容。

public static void main(String[] args) { 

    String cities[] = {"Vierzon","Salbris","Nouans","LB","LFSA","Orleans"};

    Scanner input = new Scanner(System.in);

    String nameCity = input.nextLine();

    indexCities(cities, nameCity);

}

public static void indexCities(String cities[], String nameCity){

    for (int i = 0; i < cities.length; i++) {
        if(nameCity.equals(cities[i])) {
            System.out.println(i);
            break;
        }
    }
}

答案 1 :(得分:0)

你可以这样做:

public static void main(String[] args) {
    String cities[] = { "Vierzon", "Salbris", "Nouans", "LB", "LFSA", "Orleans" };
    int index = indexCities(cities, "Vierzon");
    System.out.println("Index of city Vierzon is: " + index);
}

public static int indexCities(String cities[], String cityName) {
    List<String> cityList = Arrays.asList(cities);
    return cityList.indexOf(name);
}

答案 2 :(得分:0)

变量nameCity的范围仅限于主要功能。您无法在主要功能之外访问它。

答案 3 :(得分:0)

当您尝试在方法indexCities中使用变量时,该变量超出范围。一种解决方案是将变量nameCity变为实例变量,方法是将其定义移出main方法,但您的代码也可以通过多种方式进行改进。检查下面的一些选项:

这将在数组中打印您要查找的城市的索引:

import java.util.Scanner;

public class villes {

  public static void main(String[] args) {    
    String cities[] = {"Vierzon","Salbris","Nouans","LB","LFSA","Orleans"};
    Scanner input = new Scanner(System.in);
    String nameCity = input.nextLine();
    indexCities(nameCity, cities);  
  }

  public static void indexCities(String copyOfNameCity, String cities[]){
    for (int i = 0; i < cities.length; i++) {
      if(copyOfNameCity.equals(cities[i])) {
        System.out.println(i);
        break;
      }
    }
  }
}

您可以通过使方法返回值来改进它。像这样:

import java.util.Scanner;

public class villes {

  public static void main(String[] args) {    
    String cities[] = {"Vierzon","Salbris","Nouans","LB","LFSA","Orleans"};
    Scanner input = new Scanner(System.in);
    String nameCity = input.nextLine();
    int cityIndex = indexCities(nameCity, cities);
      System.out.println(cityIndex == -1 ? "City not found" : "City found in index " + cityIndex);    
  }

  public static int indexCities(String nameCity, String cities[]){  
    for (int i = 0; i < cities.length; i++) {
      if(nameCity.equals(cities[i])) {
        return i;
      }
    }
    return -1;
  }
}

另一种方式是:

import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class test2 {

    public static void main(String[] args) {
        String cities[] = {"Vierzon", "Salbris", "Nouans", "LB", "LFSA", "Orleans"};
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the name of city to be searched -> ");
        String nameCity = input.nextLine();
        int cityIndex = indexCities(nameCity, cities);
        System.out.println(cityIndex == -1 ? "City not found" : "Found at position " + cityIndex);
        input.close();
    }

    public static int indexCities(String cityName, String cities[]) {
        List<String> cityList = Arrays.asList(cities);
        return cityList.indexOf(cityName);
    }
}