我正在尝试编写一个简单的程序,该程序调用各种方法来提示用户他们的城市和州名以及人口比提供一定比例的人口。我已经解决了大部分代码问题,但是我一直遇到1个编译错误
"错误:找不到符号,变量:cityPercState"
有人能告诉我我的代码中有什么问题吗?
import java.util.Scanner;
public class CityOrozcoB52
{ // begin class
private static Scanner input = new Scanner(System.in);
public static void main(String[] args)
{ // begin main method
String city, state;
float cityPopulation, statePopulation;
cityName();
stateName();
cityPopulation(city);
statePopulation(state);
cityPercState(cityPopulation, statePopulation);
displayCityStateStats(city, state, cityPopulation, statePopulation, cityPercState);
} // end main method
public static String cityName()
{
String city = "";
System.out.printf("What is the name of your city:");
city = input.nextLine();
return city;
}
public static String stateName()
{
String state = "";
System.out.printf("What is the name of your state:");
state = input.nextLine();
return state;
}
public static float cityPopulation(String city)
{
float cityPopulation = 0;
System.out.printf("what is the population of %s:\n", city);
cityPopulation = input.nextFloat();
return cityPopulation;
}
public static float statePopulation(String state)
{
float statePopulation = 0;
System.out.printf("what is the population of %s:", state);
statePopulation = input.nextFloat();
return statePopulation;
}
public static float cityPercState(float cityPopulation, float statePopulation)
{
float cityStatePercentage = (cityPopulation / statePopulation) * 100;
}
public static void displayCityStateStats(String cityName, String stateName, float cityPopulation, float statePopulation,
float cityPercState)
{
System.out.printf("POPULATION STATISTICS\n\n"
+ "City: %s"
+ "State: %s"
+ "City Population: %f"
+ "State Population: %f"
+ "City to State Population: %.2f%%", cityName, stateName, cityPopulation, statePopulation,
cityPercState);
}
} // ends CityOrozcoLE52
答案 0 :(得分:1)
您需要在cityPercState()
方法中实际返回一个值。
添加:
return cityStatePercentage;
到方法。