JavaNoobie:for循环调用不起作用的方法

JavaNoobie:for循环调用不起作用的方法,java,for-loop,methods,Java,For Loop,Methods,我知道这应该是基本的,但我不明白(我还是个初学者)。我想让代码做的是: /*为3个国家/地区的人口和土地面积获取用户输入。 *计算每个国家的人口密度(即人口/土地面积), *输出所有3个国家的平均人口密度。 */ 出于某种原因,我的For循环调用这些方法不起作用。它只调用第一个国家方法(美国、加拿大或墨西哥),然后停止。非常感谢您的帮助!这是我的代码: (链接) (代码) import java.util.*; 公共类Q8_Popandland区域2{ 静态字符串pplPerMile=“每平方

我知道这应该是基本的,但我不明白(我还是个初学者)。我想让代码做的是:
/*为3个国家/地区的人口和土地面积获取用户输入。
*计算每个国家的人口密度(即人口/土地面积),
*输出所有3个国家的平均人口密度。
*/

出于某种原因,我的For循环调用这些方法不起作用。它只调用第一个国家方法(美国、加拿大或墨西哥),然后停止。非常感谢您的帮助!这是我的代码:
(链接)

(代码)

import java.util.*;
公共类Q8_Popandland区域2{
静态字符串pplPerMile=“每平方英里人数。\n”;
静态扫描仪x=新扫描仪(System.in);
公共静态void main(字符串[]args){
Q8_PopandlandArea 2 obj=新的Q8_PopandlandArea 2();
obj.CountryCaller();
}
选择字符串用户;
公共字符串CountryCaller(){
int i;

对于(i=0;i,返回站正在停止

您应该更改您的退货单,以备打印

试试这个:

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;  
}
公共字符串CountryCaller(){
int i;
整数总密度=0;

对于(i=0;i,
return
语句终止函数的执行,并将控制权返回给调用函数

查看一下您的
CountryCaller
方法-您正在从循环内部跳出(
return
ing)-这将导致循环终止

您的问题在于“return”关键字。 调用return时,程序将退出其当前方法(即使它位于循环中)。 因此,您实际要做的是为USA输入if条件,然后通过返回字符串退出该方法。
我的建议是:只有当您知道已经获得了必要的值/条件并且想要退出该方法时,才在循环中使用“return”。

您有几个错误,我认为您根本不了解OO编程的概念(或者只是Java结构)

  • 首先,当你在一个循环中返回时,它会结束。就像一个中断。你应该移除循环中的返回状态
  • 当您返回字符串时,必须对其进行处理。该方法返回平均值,但不会将其弹出给用户。您可以在popDensity()中执行此操作
  • PopDensity()方法正在另一次调用countries方法,因此您必须再次引入参数。您应该存储Courties方法返回的结果,以避免再次调用它们
  • 您再次调用CountryCaller(),递归性,因此再次启动循环…我认为您希望停止循环,而不是再次启动循环
  • 在墨西哥和加拿大的方法中,您没有返回实际结果
  • 你给用户做了一些复制品,注意这个
  • 这是正确的代码,请与您的代码进行比较,请与您的代码进行比较,并尝试理解所有内容,如果您不了解某些内容,请在注释中询问:)

    代码:

    package massilia.export.promotion;
    导入java.util.Scanner;
    公共类Q8_Popandland区域2{
    静态字符串pplPerMile=“每平方英里人数。\n”;
    静态扫描仪x=新扫描仪(System.in);
    公共静态void main(字符串[]args){
    Q8_PopandlandArea 2 obj=新的Q8_PopandlandArea 2();
    obj.countryCaller();
    }
    选择字符串用户;
    void countryCaller(){
    int i;
    整数总密度=0;
    对于(i=0;i<3;i++){
    int实际密度=0;
    System.out.println(“输入以下三个国家之一:\n1.USA\n2.Canada\n3.Mexico”);
    userSelected=x.next();
    if(userSelected.equalsIgnoreCase(“美国”)){
    实际密度=美国();
    }else if(userSelected.equalsIgnoreCase(“加拿大”)){
    实际密度=加拿大();
    }else if(userSelected.equalsIgnoreCase(“墨西哥”)){
    实际密度=墨西哥();
    }否则{
    System.out.println(“我不明白。要不要再试一次?”);
    字符串tryAgain2=x.next();
    if(tryAgain2.等效信号情况(“N”)){
    回来
    }
    }
    总密度+=实际密度;
    }
    System.out.println(“平均值为”+总密度/3);
    }
    公共互联网(美国){
    System.out.println(“输入近似总体:”);
    int popUSA=x.nextInt();
    System.out.println(“输入国家的土地面积(单位:平方英里)”;
    int AREA USA=x.nextInt();
    int calcUSA=波普萨/areaUSA;
    System.out.println(“美国的人口密度为”+calcUSA+pplPerMile+calcUSA);
    返回calcUSA;
    }
    加拿大公共情报局(){
    System.out.println(“输入近似总体:”);
    int popCanada=x.nextInt();
    System.out.println(“输入国家的土地面积(单位:平方英里)”;
    int Area Canada=x.nextInt();
    int calcCanada=加拿大人口/加拿大地区;
    System.out.println(“加拿大的人口密度为”+calcCanada+pplPerMile);
    返回calcCanada;
    }
    墨西哥公共情报局(){
    System.out.println(“输入近似总体:”);
    int popMexico=x.nextInt();
    System.out.println(“输入国家的土地面积(单位:平方英里)”;
    int AREA MEXICOX=x.nextInt();
    int calcMexicio=墨西哥/墨西哥地区;
    System.out.println(“墨西哥的人口密度为”+calcMexicio+pplPerMile);
    返回calcmexio;
    }
    }
    
    将代码包括在问题中谢谢@XtremeBaumer。我现在已经这样做了。:
    return
    语句结束方法执行(当然还有它的
    for
    循环)。我确信这是重复的,但我很难找到一个好的问题来重复它。但是,是的,问题不是
    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;  
    }
    
    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;
        }
    
    }