Java noobie:for循环调用方法不起作用

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java noobie:for循环调用方法不起作用相关的知识,希望对你有一定的参考价值。

我知道这应该是基本的,但我无法弄清楚(我还是初学者)。我想要的代码是: / *用户输入3个国家的人口和土地面积。 *计算每个国家的人口密度(即人口/土地面积), *输出所有3个国家的平均人口密度。 * /

由于某种原因,我的For循环调用方法不起作用。它只调用第一种国家方法(美国,加拿大或墨西哥),然后停止。任何帮助非常感谢!这是我的代码: (链接)jdoodle.com/a/oQY

(码)

import java.util.*;
public class Q8_PopAndLandArea2 {
    static String pplPerMile = " person/s per square mile.
";
    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: 
1.USA 
2.Canada 
3.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结构)。

  1. 首先,当你在循环中返回时,它将结束。就像休息一样。您应该删除循环内的返回参数。
  2. 当你返回一个字符串时,你必须对它做一些事情。该方法返回平均值,但不会将其弹出给用户。你在popDensity()中的位置。
  3. PopDensity()方法又调用了这些国家/地区的方法,因此您必须再次使用params。您应该存储coutries方法返回的结果,以避免再次调用它们。
  4. 你再次调用CountryCaller(),递归,所以你再次启动循环...我想你想停止循环不再启动它。
  5. 在墨西哥和加拿大的方法中,你没有返回真正的结果。
  6. 你正在为用户做一些重复,关心这一点。

这是正确的代码,请与您的代码进行比较,请与您的代码进行比较并尝试理解所有代码,如果有什么是您没有得到的,请在评论中询问:)。

码:

package massilia.export.promotion;

import java.util.Scanner;

public class Q8_PopAndLandArea2 {
    static String pplPerMile = " person/s per square mile.
";
    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: 
1.USA 
2.Canada 
3.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: 
1.USA 
2.Canada 
3.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方法 - 你从你的循环中跳出它(returning) - 这将导致循环终止

以上是关于Java noobie:for循环调用方法不起作用的主要内容,如果未能解决你的问题,请参考以下文章

for 循环中的代码在 ngOnInt() 中不起作用

Swift:for循环中延迟的动画不起作用?

在 for 循环期间为列表中的项目分配新值不起作用?

java剪辑循环不起作用

在 for 循环中使用 Promise

范围在 for 循环中不起作用