将参数传递给方法

时间:2015-02-02 18:33:56

标签: java parameters parameter-passing

我已经设置了一个挑战来创建一个温度程序,允许用户输入一周中每一天的温度,然后让他们选择他们希望查看温度的那一天,显示温度的平均温度。一周然后退出。

但是当我调用average()时,我遇到了参数问题,当涉及到开关时;和day();它强调红色?
同样在我的方法中,平均值为' Java说我需要一个返回声明,但我不认为我做了吗?

 import java.util.Scanner;
/**
 * Created by IntelliJ IDEA.
 * Date: 28/01/2015
 * Time: 15:53
 * UPDATE COMMENT ABOUT PROGRAM HERE
 */
public class Temp
{
   static Scanner keyboard = new Scanner (System.in);
   static int choice;
   static double average, finalAverage;




   public static void temp()
   {
      double monTemp,tuesTemp,wedTemp,thursTemp,friTemp,satTemp,sunTemp;

      System.out.println("You have chosen option 1");
      System.out.println("Please enter the temperature for Monday in degrees celsius");
      monTemp = keyboard.nextDouble();


      System.out.println("Please enter the temperature for Tuesday in degrees celsius");
      tuesTemp = keyboard.nextDouble();


      System.out.println("Please enter the temperature for Wednesday in degrees celsius");
      wedTemp = keyboard.nextDouble();

      System.out.println("Please enter the temperature for Thursday in degrees celsius");
      thursTemp = keyboard.nextDouble();

      System.out.println("Please enter the temperature for Friday in degrees celsius");
      friTemp = keyboard.nextDouble();

      System.out.println("Please enter the temperature for Saturday in degrees celsius");
      satTemp = keyboard.nextDouble();

      System.out.println("Please enter the temperature for Sunday in degrees celsius");
      sunTemp = keyboard.nextDouble();

   }//Temp

   public static void day(double monTemp, double tuesTemp, double wedTemp, double thursTemp, double friTemp, double satTemp, double sunTemp)
   {

      System.out.println("You have chosen to see the temperature for a specified day");
      System.out.println("Please choose a day below");
      System.out.println(" 1. Monday");
      System.out.println(" 2. Tuesday");
      System.out.println(" 3. Wednesday");
      System.out.println(" 4. Thursday");
      System.out.println(" 5. Friday");
      System.out.println(" 6. Saturday");
      System.out.println(" 7. Sunday ");
      choice = keyboard.nextInt();

      if (choice==1)

      {
         System.out.println(" You have chosen to see the temperature for Monday");
         System.out.println(" The temperature for Monday is " +monTemp + "°C");
      }//if

      if  ( choice== 2)
      {
         System.out.println(" You have chosen to see the temperature for Tuesday");
         System.out.println(" The temperature for Tuesday " + tuesTemp+ "°C");

      }//if
      else if ( choice== 3)
      {
         System.out.println(" You have chosen to see the temperature for Wednesday");
         System.out.println(" The temperature for Wednesday is " +wedTemp + "°C");
      }//elseif
      else if ( choice== 4)
      {
         System.out.println(" You have chosen to see the temperature for Thursday");
         System.out.println(" The temperature for Thursday is " +thursTemp + "°C");
      }//elseif
      else if ( choice== 5)
      {
         System.out.println(" You have chosen to see the temperature for Friday");
         System.out.println(" The temperature for Friday is " +friTemp + "°C");
      }//elseif
      else if ( choice== 6)
      {
         System.out.println(" You have chosen to see the temperature for Saturday");
         System.out.println(" The temperature for Saturday is " +satTemp + "°C");
      }//if
      else if (choice==7)
      {
         System.out.println(" You have chosen to see the temperature for Sunday");
         System.out.println(" The temperature for Sunday is " +sunTemp + "°C");
      }//elseif
      else
      {
         System.out.println("Oops something went wrong, please try again");
         day(monTemp, tuesTemp, wedTemp, thursTemp, friTemp, satTemp, sunTemp);
      }//else



   }//day

   public static double average(double monTemp, double tuesTemp, double wedTemp, double thursTemp, double satTemp, double sunTemp, double friTemp)
   { //error here
      average=monTemp+tuesTemp+wedTemp+thursTemp+friTemp+satTemp+sunTemp;
      finalAverage=(average/7);

      System.out.println(" You have chosen to see the average temperature for the week");
      System.out.println(" The temperature for the week is " +finalAverage + "°C");

   }//Average

   public static void menu()
   {

      System.out.println("Welcome the temperature program. Please select an option for the menu below b entering the number");
      System.out.println("1)Enter temperatures for each day of the week");
      System.out.println("2)Display average temperature for specified choice");
      System.out.println("3)Display average temperature for the week");
      System.out.println("4)Exit");
      choice = keyboard.nextInt();

      switch (choice)
      {
         case 1:

            temp();
            menu();
            break;


         case 2:

            day();
            menu();
            break;

         case 3:

            average(); //error here
            menu();
            break;


         case 4:

            System.exit(0);



         default:
            System.out.println("Oops, something went wrong, please try again!");
            menu();
      }//switch
   }//menu

   public static void main(String[] args)
   {
      menu();

   }//main
}//class

5 个答案:

答案 0 :(得分:1)

您的temp()方法将温度存储在局部变量中。

局部变量仅存在于定义它们的块中。也就是说,在temp方法中。一旦该方法完成,它们就会消失。

出于这个原因,你不需要t have the data available to you when you need to run another method, like the average()`方法。

您应该将温度定义为字段。事实上,如果您已经学会了如何使用它,最好使用数组。但无论您是否使用数组,都必须将它们声明为字段 - 在所有构造函数和方法之前。

您的所有方法都是静态的,因此您必须将它们定义为静态字段(尽管通常您应该尝试避免静态执行所有操作的程序),例如您的平均值和finalAverage。

当你将它们定义为字段时(记住不要在temp()中重新定义它们!只分配给它们,不要在那里声明它们),你将能够从其他方法访问它们。然后你有两个选择:

  1. 不要将它们声明为day()average()的参数,而是直接使用它们。在这种情况下,day()average()将在没有参数的情况下声明,您无需更改switchmenu中的任何内容。
  2. 将它们声明为参数,但请记住将它们作为menu的参数传递。
  3. 你有另一个严重的问题,那就是menu()递归调用自己。理论上,如果您长时间使用该程序,您将获得Stack Overflow异常。你不应该自己调用menu()。你应该有一个显示菜单的循环,然后选择退出。

    至于return语句,其他答案已经告诉过你 - 如果你声明了一个返回类型的方法,那么你必须返回一些东西。如果您不想从方法中返回任何内容,则必须将其声明为void

    请记住,方法的使用方式应与声明方式相匹配。如果调用具有参数的方法,则必须传递参数。如果声明了返回类型,则必须在方法内的return语句中返回匹配值。

答案 1 :(得分:0)

public static double average(double monTemp, double tuesTemp, double wedTemp, double thursTemp, double satTemp, double sunTemp, double friTemp)

返回类型是双倍的。所以你需要回报。 最好使该方法无效。

public static void average(double monTemp, double tuesTemp, double wedTemp, double thursTemp, double satTemp, double sunTemp, double friTemp)

答案 2 :(得分:0)

有一些错误,我已经纠正了。

import java.util.Scanner;

public class Temp
{
 static Scanner keyboard = new Scanner (System.in);
 static int choice;
 static double average, finalAverage;
 static double monTemp,tuesTemp,wedTemp,thursTemp,friTemp,satTemp,sunTemp;




 public static void temp()
 {

  System.out.println("You have chosen option 1");
  System.out.println("Please enter the temperature for Monday in degrees celsius");
  monTemp = keyboard.nextDouble();


  System.out.println("Please enter the temperature for Tuesday in degrees celsius");
  tuesTemp = keyboard.nextDouble();


  System.out.println("Please enter the temperature for Wednesday in degrees celsius");
  wedTemp = keyboard.nextDouble();

  System.out.println("Please enter the temperature for Thursday in degrees celsius");
  thursTemp = keyboard.nextDouble();

  System.out.println("Please enter the temperature for Friday in degrees celsius");
  friTemp = keyboard.nextDouble();

  System.out.println("Please enter the temperature for Saturday in degrees celsius");
  satTemp = keyboard.nextDouble();

  System.out.println("Please enter the temperature for Sunday in degrees celsius");
  sunTemp = keyboard.nextDouble();

 }//Temp

 public static void day()
 {

  System.out.println("You have chosen to see the temperature for a specified day");
  System.out.println("Please choose a day below");
  System.out.println(" 1. Monday");
  System.out.println(" 2. Tuesday");
  System.out.println(" 3. Wednesday");
  System.out.println(" 4. Thursday");
  System.out.println(" 5. Friday");
  System.out.println(" 6. Saturday");
  System.out.println(" 7. Sunday ");
  choice = keyboard.nextInt();

  if (choice==1)

  {
     System.out.println(" You have chosen to see the temperature for Monday");
     System.out.println(" The temperature for Monday is " +monTemp + "°C");
  }//if

  else if  ( choice== 2)
  {
     System.out.println(" You have chosen to see the temperature for Tuesday");
     System.out.println(" The temperature for Tuesday " + tuesTemp+ "°C");

  }//if
  else if ( choice== 3)
  {
     System.out.println(" You have chosen to see the temperature for Wednesday");
     System.out.println(" The temperature for Wednesday is " +wedTemp + "°C");
  }//elseif
  else if ( choice== 4)
  {
     System.out.println(" You have chosen to see the temperature for Thursday");
     System.out.println(" The temperature for Thursday is " +thursTemp + "°C");
  }//elseif
  else if ( choice== 5)
  {
     System.out.println(" You have chosen to see the temperature for Friday");
     System.out.println(" The temperature for Friday is " +friTemp + "°C");
  }//elseif
  else if ( choice== 6)
  {
     System.out.println(" You have chosen to see the temperature for Saturday");
     System.out.println(" The temperature for Saturday is " +satTemp + "°C");
  }//if
  else if (choice==7)
  {
     System.out.println(" You have chosen to see the temperature for Sunday");
     System.out.println(" The temperature for Sunday is " +sunTemp + "°C");
  }//elseif
  else
  {
     System.out.println("Oops something went wrong, please try again");
     day();
  }//else



 }//day

 public static void average()
 {
  average=monTemp+tuesTemp+wedTemp+thursTemp+friTemp+satTemp+sunTemp;
  finalAverage=(average/7);

  System.out.println(" You have chosen to see the average temperature for the week");
  System.out.println(" The temperature for the week is " +finalAverage + "°C");

  }//Average

 public static void menu()
 {

  System.out.println("Welcome the temperature program. Please select an option for the menu below b entering the number");
  System.out.println("1)Enter temperatures for each day of the week");
  System.out.println("2)Display average temperature for specified choice");
  System.out.println("3)Display average temperature for the week");
  System.out.println("4)Exit");
  choice = keyboard.nextInt();

  switch (choice)
  {
     case 1:

        temp();
        menu();
        break;


     case 2:

       day();
        menu();
        break;

     case 3:

        average();
        menu();
        break;


     case 4:

        System.exit(0);



     default:
        System.out.println("Oops, something went wrong, please try again!");
        menu();
    }//switch
 }//menu

 public static void main(String[] args)
  {
  menu();

  }//main
 }//class

答案 3 :(得分:-1)

您必须在方法平均值中返回双倍:“public static double

public static double average(double monTemp, double tuesTemp, double wedTemp, double thursTemp, double satTemp, double sunTemp, double friTemp) {
      double average=monTemp+tuesTemp+wedTemp+thursTemp+friTemp+satTemp+sunTemp;
      finalAverage=(average/7);

      System.out.println(" You have chosen to see the average temperature for the week");
      System.out.println(" The temperature for the week is " +finalAverage + "°C");

      return average;
}

答案 4 :(得分:-1)

解决问题的一种方法:

import java.util.Scanner;

public class Temp {
static Scanner keyboard = new Scanner(System.in);
static int choice;
static double average, finalAverage;

static double monTemp, tuesTemp, wedTemp, thursTemp, friTemp, satTemp, sunTemp;

public static void temp() {

    System.out.println("You have chosen option 1");
    System.out.println("Please enter the temperature for Monday in degrees celsius");
    monTemp = keyboard.nextDouble();

    System.out.println("Please enter the temperature for Tuesday in degrees celsius");
    tuesTemp = keyboard.nextDouble();

    System.out.println("Please enter the temperature for Wednesday in degrees celsius");
    wedTemp = keyboard.nextDouble();

    System.out.println("Please enter the temperature for Thursday in degrees celsius");
    thursTemp = keyboard.nextDouble();

    System.out.println("Please enter the temperature for Friday in degrees celsius");
    friTemp = keyboard.nextDouble();

    System.out.println("Please enter the temperature for Saturday in degrees celsius");
    satTemp = keyboard.nextDouble();

    System.out.println("Please enter the temperature for Sunday in degrees celsius");
    sunTemp = keyboard.nextDouble();

}// Temp

public static void day() {

    System.out.println("You have chosen to see the temperature for a specified day");
    System.out.println("Please choose a day below");
    System.out.println(" 1. Monday");
    System.out.println(" 2. Tuesday");
    System.out.println(" 3. Wednesday");
    System.out.println(" 4. Thursday");
    System.out.println(" 5. Friday");
    System.out.println(" 6. Saturday");
    System.out.println(" 7. Sunday ");
    choice = keyboard.nextInt();

    switch (choice) {
    case 1:
        System.out.println(" You have chosen to see the temperature for Monday");
        System.out.println(" The temperature for Monday is " + monTemp + "°C");
        break;
    case 2:
        System.out.println(" You have chosen to see the temperature for Tuesday");
        System.out.println(" The temperature for Tuesday " + tuesTemp + "°C");
        break;
    case 3:
        System.out.println(" You have chosen to see the temperature for Wednesday");
        System.out.println(" The temperature for Wednesday is " + wedTemp + "°C");
        break;
    case 4:
        System.out.println(" You have chosen to see the temperature for Thursday");
        System.out.println(" The temperature for Thursday is " + thursTemp + "°C");
        break;
    case 5:
        System.out.println(" You have chosen to see the temperature for Friday");
        System.out.println(" The temperature for Friday is " + friTemp + "°C");
        break;
    case 6:
        System.out.println(" You have chosen to see the temperature for Saturday");
        System.out.println(" The temperature for Saturday is " + satTemp + "°C");
        break;
    case 7:
        System.out.println(" You have chosen to see the temperature for Sunday");
        System.out.println(" The temperature for Sunday is " + sunTemp + "°C");
        break;
    default:
        System.out.println("Oops something went wrong, please try again");
        break;

    }

}// day

public static void average() {
    average = monTemp + tuesTemp + wedTemp + thursTemp + friTemp + satTemp + sunTemp;
    finalAverage = (average / 7);

    System.out.println(" You have chosen to see the average temperature for the week");
    System.out.println(" The temperature for the week is " + finalAverage + "°C");

}// Average

public static void menu() {

    System.out.println("Welcome the temperature program. Please select an option for the menu below b entering the number");
    System.out.println("1)Enter temperatures for each day of the week");
    System.out.println("2)Display average temperature for specified choice");
    System.out.println("3)Display average temperature for the week");
    System.out.println("4)Exit");
    choice = keyboard.nextInt();

    switch (choice) {
    case 1:
        temp();
        menu();
        break;

    case 2:
        day();
        menu();
        break;

    case 3:
        average();
        menu();
        break;
    case 4:
        System.exit(0);
    default:
        System.out.println("Oops, something went wrong, please try again!");
        menu();
    }// switch
}// menu

public static void main(String[] args) {
    menu();
}// main
}// class