我知道这应该是基本的,但我无法弄清楚(我还是初学者)。我想要的代码是: / *用户输入3个国家的人口和土地面积。 *计算每个国家的人口密度(即人口/土地面积), *输出所有3个国家的平均人口密度。 * /
由于某种原因,我的For循环调用方法不起作用。它只调用第一种国家方法(美国,加拿大或墨西哥),然后停止。任何帮助非常感谢!这是我的代码: (链接)jdoodle.com/a/oQY
(码)
import java.util.*;
public class Q8_PopAndLandArea2 {
static String pplPerMile = " person/s per square mile.\n";
static Scanner x = new Scanner(System.in);
public static void main (String[] args) {
Q8_PopAndLandArea2 obj = new Q8_PopAndLandArea2();
obj.CountryCaller();
}
String userSelected;
public String CountryCaller() {
int i;
for (i=0; i<3; i++) {
System.out.println("Enter one of these 3 countries: \n1.USA \n2.Canada \n3.Mexico");
userSelected = x.next();
if (userSelected.equalsIgnoreCase("USA")) {
return "The population density of the USA is " + USA();
} else if (userSelected.equalsIgnoreCase("Canada")){
return "The population density of the Canada is " + Canada();
} else if (userSelected.equalsIgnoreCase("Mexico")){
return "The population density of the Mexico is " + Mexico();
} else {
System.out.println("I don't understand. Care to try again?");
String tryAgain2 = x.next();
if (tryAgain2.equalsIgnoreCase("N")) {
System.out.println("Goodbye!");
} else {
CountryCaller();
}
}
} return "The average is"+ popDensity();
}
public int USA() {
int calcUSA2 = 0;
String calcUSAstr = "The population density of the USA is ";
System.out.println("Enter the approximate population: ");
int popUSA = x.nextInt();
System.out.println("Enter the land area of the country (in sq miles): ");
int areaUSA = x.nextInt();
calcUSA2 = popUSA/areaUSA;
System.out.println(calcUSAstr + calcUSA2 + pplPerMile + calcUSA2);
return calcUSA2;
}
public int Canada() {
int calcCanada2 = 0;
String calcCanadaStr = "The population density of Canada is ";
System.out.println("Enter the approximate population: ");
int popCanada = x.nextInt();
System.out.println("Enter the land area of the country (in sq miles): ");
int areaCanada = x.nextInt();
int calcCanada = popCanada/areaCanada;
System.out.println(calcCanadaStr + calcCanada + pplPerMile);
return calcCanada2;
}
public int Mexico() {
int calcMexicio2 = 0;
String calcMexicioStr = "The population density of Mexico is ";
System.out.println("Enter the approximate population: ");
int popMexico = x.nextInt();
System.out.println("Enter the land area of the country (in sq miles): ");
int areaMexico = x.nextInt();
int calcMexicio = popMexico/areaMexico;
System.out.println(calcMexicioStr + calcMexicio + pplPerMile);
return calcMexicio2;
}
public int popDensity() {
int calcAve = USA() + Canada() + Mexico() / 3;
System.out.println("The average is " + calcAve);
return calcAve;
}
}
你有几个错误,我认为你没有完全理解OO编程(或只是Java结构)。
这是正确的代码,请与您的代码进行比较,请与您的代码进行比较并尝试理解所有代码,如果有什么是您没有得到的,请在评论中询问:)。
码:
package massilia.export.promotion;
import java.util.Scanner;
public class Q8_PopAndLandArea2 {
static String pplPerMile = " person/s per square mile.\n";
static Scanner x = new Scanner(System.in);
public static void main(String[] args) {
Q8_PopAndLandArea2 obj = new Q8_PopAndLandArea2();
obj.countryCaller();
}
String userSelected;
void countryCaller() {
int i;
int totalDensity = 0;
for (i = 0; i < 3; i++) {
int actualDensity = 0;
System.out.println("Enter one of these 3 countries: \n1.USA \n2.Canada \n3.Mexico");
userSelected = x.next();
if (userSelected.equalsIgnoreCase("USA")) {
actualDensity = USA();
} else if (userSelected.equalsIgnoreCase("Canada")) {
actualDensity = Canada();
} else if (userSelected.equalsIgnoreCase("Mexico")) {
actualDensity = Mexico();
} else {
System.out.println("I don't understand. Care to try again?");
String tryAgain2 = x.next();
if (tryAgain2.equalsIgnoreCase("N")) {
return;
}
}
totalDensity += actualDensity;
}
System.out.println("The average is" + totalDensity / 3);
}
public int USA() {
System.out.println("Enter the approximate population: ");
int popUSA = x.nextInt();
System.out.println("Enter the land area of the country (in sq miles): ");
int areaUSA = x.nextInt();
int calcUSA = popUSA / areaUSA;
System.out.println("The population density of the USA is " + calcUSA + pplPerMile + calcUSA);
return calcUSA;
}
public int Canada() {
System.out.println("Enter the approximate population: ");
int popCanada = x.nextInt();
System.out.println("Enter the land area of the country (in sq miles): ");
int areaCanada = x.nextInt();
int calcCanada = popCanada / areaCanada;
System.out.println("The population density of Canada is" + calcCanada + pplPerMile);
return calcCanada;
}
public int Mexico() {
System.out.println("Enter the approximate population: ");
int popMexico = x.nextInt();
System.out.println("Enter the land area of the country (in sq miles): ");
int areaMexico = x.nextInt();
int calcMexicio = popMexico / areaMexico;
System.out.println("The population density of Mexico is " + calcMexicio + pplPerMile);
return calcMexicio;
}
}
您的问题出在“return”关键字中。当你调用return时,你的程序会退出当前的方法(即使它在循环中)。所以你实际上在做的是输入USA的if条件并通过返回一个字符串退出方法。我的建议是:当你知道已经获得必要的值/条件并且想要退出方法时,只在循环内使用“return”。
退货声明正在做你的停止。
您应该更改毛皮婴儿印花内的退货声明。
试试这个:
public String CountryCaller() {
int i;
int totalDensity = 0;
for (i=0; i<3; i++) {
System.out.println("Enter one of these 3 countries: \n1.USA \n2.Canada \n3.Mexico");
userSelected = x.next();
if (userSelected.equalsIgnoreCase("USA")) {
totalDensity += USA();
System.out.println("The population density of the USA is " + USA());
} else if (userSelected.equalsIgnoreCase("Canada")){
totalDensity += Canada();
System.out.println("The population density of the Canada is " + Canada());
} else if (userSelected.equalsIgnoreCase("Mexico")){
totalDensity += Mexico();
System.out.println("The population density of the Mexico is " + Mexico());
} else {
System.out.println("I don't understand. Care to try again?");
String tryAgain2 = x.next();
if (tryAgain2.equalsIgnoreCase("N")) {
System.out.println("Goodbye!");
} else {
CountryCaller();
}
}
} return "The average is"+ totalDensity/3;
}
return
语句终止函数的执行并将控制返回给调用函数。
看看你的CountryCaller
方法 - 你从你的循环中跳出它(return
ing) - 这将导致循环终止