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

时间:2018-03-05 14:20:07

标签: java for-loop methods

我知道这应该是基本的,但我无法弄清楚(我还是初学者)。我希望代码做的是:
/ *用户输入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;
    }

} 

4 个答案:

答案 0 :(得分:1)

返回声明正在停止。

您应该通过打印更改您的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;  
}

答案 1 :(得分:1)

你的问题出在&#34; return&#34;关键词。 当你调用return时,你的程序会退出当前的方法(即使它在循环中)。 所以你实际做的是输入USA的if条件并返回一个字符串退出方法。 我的建议是:只使用&#34; return&#34;当你知道已经获得必要的价值/条件而你想要退出方法时,在一个循环中。

答案 2 :(得分:1)

你有几个错误,我认为你没有完全理解OO编程(或只是Java结构)。

  1. 首先,当你在循环中返回时,它将结束。就像休息一样。您应该删除循环内的返回参数。
  2. 当你返回一个字符串时,你必须对它做一些事情。该方法返回平均值,但您不会将其弹出给用户。你在popDensity()中的位置。
  3. PopDensity()方法正在调用另一个国家/地区的方法,因此您必须再次引用params。你应该存储coutries方法返回的结果,以避免再次调用它们。
  4. 你再次调用CountryCaller(),递归,所以你再次启动循环...我想你想停止循环再次启动它。
  5. 在墨西哥和加拿大的方法中,你没有回复真实的结果。
  6. 你正在为用户做一些重复,关心这个。
  7. 这是正确的代码,请与您的代码进行比较,请与您的代码进行比较,并尝试理解所有代码,如果您有什么不明白的地方,请在评论中提问:)。

    代码:

    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;
        }
    
    }
    

答案 3 :(得分:0)

return语句终止函数的执行并将控制权返回给调用函数。

看看你的CountryCaller方法 - 你从循环中跳出它(return ing) - 这将导致循环终止