【问题标题】:Recieving and Returning Methods收寄方式
【发布时间】:2014-11-10 00:14:31
【问题描述】:

处理返回和接收方法的 java 程序有问题。我不知道如何调用我的方法。

  1. Scanner 变量是唯一的全局变量。变量大小将是 2 个方法的本地变量。

  2. setOcean():此方法提示输入海洋并返回从键盘捕获的内容。

  3. setSize():此方法接收海洋的名称,提示输入海洋的描述性大小
    海洋并返回捕获的内容。将显示描述性尺寸列表(请参阅示例输出)。

  4. 用户从列表中选择。根据选择,使用开关来确定
    描述性尺寸:最大、第二大、第三大、第四大、最小或显示 错误信息:再试一次!重新输入选择。如果选择是,请使用 do/while 重新提示 任何小于 1 或大于 5 的值。将有 2 个变量:一个用于存储选择 列表和另一个将大小存储为“最大”或“第二大”等。开关使用 第一个变量确定第二个变量中的值或打印错误消息。

  5. setKilometers():该方法接收海洋的名称,提示输入海洋的总面积
    以公里为单位的海洋,并返回从键盘捕获的内容。
  6. calcTotalAreaMiles():该方法接收公里数和换算系数 0.621,将公里转换为英里,并返回以英里计算的总面积。
  7. printOceanInfo():此方法接收海洋名称、大小和以英里为单位的总面积和
    打印此信息(请参阅示例输出)。

样本输出

世界五大洋

进入海洋:南部

  1. 最大
  2. 第二大
  3. 第三大
  4. 第四大
  5. 最小的

从上面的列表中,选择南大洋的大小:4

以千米为单位输入南大洋的总面积:20.327

世界的海洋

海洋:南部 尺寸:第四大 总平方英里:12.623

进入另一个海洋?是的

进入海洋:北极

  1. 最大
  2. 第二大
  3. 第三大
  4. 第四大
  5. 最小的

从上面的列表中,选择北冰洋的大小:6

再试一次!重新输入选择。

  1. 最大
  2. 第二大
  3. 第三大
  4. 第四大
  5. 最小的

从上面的列表中,选择北冰洋的大小:0

再试一次!重新输入选择。

  1. 最大
  2. 第二大
  3. 第三大
  4. 第四大
  5. 最小的

从上面的列表中,选择北冰洋的大小:5

以千米为单位输入北冰洋的总面积:14.056

世界的海洋

海洋:北极 尺寸:最小 总平方英里:872.9 万

进入另一个海洋? n

这是我目前所拥有的。

import java.util.Scanner;

public class OceanMLE52


  private static Scanner input = new Scanner(System.in);




  public static void main(String[] args)

  {//BEGIN main()
    char answer = 'Y';

    do
    {
      System.out.printf("\n\nThe 5 World Oceans");


      setOcean();

      setSize(ocean);

      setKilometers(ocean);

      calcTotalAreaMiles(oceanKilo, conversion);

      printOcean(ocean, oceanSizeName, totalSqMiles);

      input.nextLine();

      System.out.printf("%nEnter another ocean?  ");
      answer = input.nextLine().charAt(0);

    } while (Character.toUpperCase(answer)=='Y');

    System.exit(0);
  } //END OF MAIN()

  public static String setOcean() //Prompts for ocean and stores it in a variable that the other methods can access
  {
    String ocean = "";
    System.out.printf("%nEnter an ocean :");
    ocean = input.nextLine();

    return ocean;
  }


  public static void setSize(String ocean) //Prompts for descriptive size of ocean and stores it in a variable
                               //accessible to the other methods. The user selects from the list.
                               //Based on the selection a switch is used to determine the descriptive
                               //Error message is displayed if one
  {
    int selection = 0;
    do
    {

      System.out.printf("%n1. Largest %n2. Second Largest %n3. Third Largest %n4. Fourth Largest"
                          + "%n5. Smallest" + "%nFrom the above list, select the size of the 5 Ocean:  ", ocean);
      selection = input.nextInt();
      String oceanSizeName = "";

      switch(selection)
      {
        case 1: oceanSizeName = "Largest";
        break;
        case 2: oceanSizeName = "Second Largest";
        break;
        case 3: oceanSizeName = "Third Largest";
        break;
        case 4: oceanSizeName = "Fourth Largest";
        break;
        case 5: oceanSizeName = "Smallest";
        break;
        default: System.out.printf("%nTry Again! Re-enter selection.  %n");
      }

    } while (selection < 1 || selection > 5); 

  }

  public static double setKilometers(String ocean)
  {
      double oceanKilo = 0;

    System.out.printf("%nEnter total area for the %s Ocean in kilometers:  ", ocean);
    oceanKilo = input.nextDouble();

    return oceanKilo;
  }

  public static double calcTotalAreaMiles(double oceanKilo, double conversion )
  {
   double totalSqMiles = 0;
    totalSqMiles = oceanKilo * conversion; 

    return totalSqMiles;
  }

  public static void printOcean(String ocean, String oceanSizeName, double totalSqMiles)
  {

    System.out.printf("\n\nOCEANS OF THE WORLD" +
                      "\n\nOcean:  %s" + 
                      "\n\nSize: %s" + 
                      "%nTotal Square Miles: %.3f million", ocean, oceanSizeName, totalSqMiles);


  }


}

我不确定如何调用不同的方法。

请帮忙!

【问题讨论】:

  • I'm not sure how to call the different methods 是什么意思?您已经在程序中调用方法。顺便说一句,请阅读这个问题:Scanner issue when using nextLine after nextXXXinput.nextLine()input.nextInt()input.nextDouble() 的混合可能会导致一些问题。
  • 我的意思是,每次我尝试编译时,我都会继续收到错误代码“找不到符号”“符号:变量海洋”“位置:类 OceanLE52”并且我的所有方法都继续出现该错误来电。
  • 您的变量ocean 在您的方法setOcean() 中声明和初始化,这意味着它只存在于其中。你不能像setSize(ocean); 这样在main 中使用它。你需要像String s = setOcean(); 这样的东西,然后使用setSize(s);
  • 哦,好的,我明白了。谢谢!

标签: java


【解决方案1】:

我看到的主要问题是您正在从您的方法返回内容,但没有将它们分配给要传递给其他方法的任何变量。例如,您的 setOcean 方法返回一个字符串,然后您将一个名为 ocean 的变量传递给 setSize,但您的 main 方法中没有名为 ocean 的局部变量。您应该将该方法分配给一个名为 ocean 的字符串变量,即String ocean = setOcean(); 任何其他返回您需要传递给另一个方法的东西的方法都应该被类似地调用和分配。

【讨论】:

    【解决方案2】:

    您需要正确使用变量才能使其正常工作。 我还使用了 Tom 所说的关于结合 input.nextInt() 和 input.nextLine() 的内容,你去吧:

    import java.util.Scanner;
    

    公开课 OceanMLE52 {

    private static Scanner input = new Scanner(System.in);
    
    
    public static void main(String[] args)
    
    {//BEGIN main()
        char answer = 'Y';
        String ocean = "";
        String oceanSizeName = "";
        double oceanKilo = 0;
        double conversion = .38610; // convert km^2 to mi^2 
        double totalSqMiles = 0;
    
        do
        {
            System.out.printf("\n\nThe 5 World Oceans");
    
            ocean = setOcean();
    
            oceanSizeName = setSize(ocean);
    
            oceanKilo = setKilometers(ocean);
    
            totalSqMiles = calcTotalAreaMiles(oceanKilo, conversion);
    
            printOcean(ocean, oceanSizeName, totalSqMiles);
    
            input.nextLine();
    
            System.out.printf("%nEnter another ocean?  ");
            answer = input.nextLine().charAt(0);
    
        } while (Character.toUpperCase(answer)=='Y');
    
        System.exit(0);
    } //END OF MAIN()
    
    public static String setOcean() //Prompts for ocean and stores it in a variable that the other methods can access
    {
        String ocean = "";
        System.out.printf("%nEnter an ocean :");
        ocean = input.nextLine();
    
        return ocean;
    }
    
    
    public static String setSize(String ocean) //Prompts for descriptive size of ocean and stores it in a variable
    //accessible to the other methods. The user selects from the list.
    //Based on the selection a switch is used to determine the descriptive
    //Error message is displayed if one
    {
        int selection = 0;
        String oceanSizeName = "";
        do
        {
    
            System.out.printf("%n1. Largest %n2. Second Largest %n3. Third Largest %n4. Fourth Largest"
                    + "%n5. Smallest" + "%nFrom the above list, select the size of the 5 Ocean:  ", ocean);
            selection = input.nextInt();
    
            switch(selection)
            {
            case 1: oceanSizeName = "Largest";
            break;
            case 2: oceanSizeName = "Second Largest";
            break;
            case 3: oceanSizeName = "Third Largest";
            break;
            case 4: oceanSizeName = "Fourth Largest";
            break;
            case 5: oceanSizeName = "Smallest";
            break;
            default: System.out.printf("%nTry Again! Re-enter selection.  %n");
            }
    
        } while (selection < 1 || selection > 5); 
        input.nextLine(); // consume the newline
        return oceanSizeName;
    }
    
    public static double setKilometers(String ocean)
    {
        double oceanKilo = 0;
    
        System.out.printf("%nEnter total area for the %s Ocean in kilometers:  ", ocean);
        oceanKilo = input.nextDouble();
        input.nextLine(); // consume the newline
        return oceanKilo;
    }
    
    public static double calcTotalAreaMiles(double oceanKilo, double conversion )
    {
        double totalSqMiles = 0;
        totalSqMiles = oceanKilo * conversion; 
    
        return totalSqMiles;
    }
    
    public static void printOcean(String ocean, String oceanSizeName, double totalSqMiles)
    {
    
        System.out.printf("\n\nOCEANS OF THE WORLD" +
                "\n\nOcean:  %s" + 
                "\n\nSize: %s" + 
                "%nTotal Square Miles: %.3f million", ocean, oceanSizeName, totalSqMiles);
    
    
    }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-30
      • 2012-07-22
      • 2012-07-04
      相关资源
      最近更新 更多