输入菜单屏幕方法始终返回0

时间:2019-05-01 02:55:15

标签: java function return

我被分配去创建一个菜单驱动程序,其中包含一个显示菜单屏幕/获取用户输入的功能,但是displaymenu()始终且仅返回0而不是新输入的数字。

我对编程还很陌生,我用谷歌搜索了所有我能想到的东西,而且感觉很卡住。

import java.util.Scanner;

public class menuDriven
{
   public static void main(String[] args)
   {
      int menuSelection=0;
      double inches, centimeters, feet, meters, miles, kilometers;

      Scanner keyboard = new Scanner(System.in);

      displayMenu(keyboard, menuSelection);


      switch(menuSelection)
      {
      case 1:
         inchesToCentimeters(keyboard);
         break;

      case 2:
         feetToMeters(keyboard);
         break;

      case 3:
         milesToKilometers(keyboard);
         break;
      }

   } // end main

   public static int displayMenu(Scanner keyboard,  int menuSelection) 
   {

   System.out.println("1. Convert inches to centimeters");
   System.out.println("2. Convert feet to meters");
   System.out.println("3. Convert miles to kilometers");
   System.out.println();

   System.out.println("Enter your selection");
   menuSelection = keyboard.nextInt();
    while (menuSelection < 1 || menuSelection > 3)
      {
         System.out.println("Please enter 1, 2, or 3.");
         menuSelection = keyboard.nextInt();
      }
   return menuSelection;
   }

   public static void inchesToCentimeters(Scanner keyboard)
   {
      double inches, centimeters;
      System.out.print("Enter the number of inches: ");
      inches = keyboard.nextDouble();
      centimeters = inches * 2.54;
      System.out.println("That is equal to " + centimeters + " 
centimeters.");

   }

   public static void feetToMeters(Scanner keyboard)
   {
      double feet, meters;
      System.out.println("Enter the number of feet: ");
      feet = keyboard.nextDouble();
      meters = feet * 0.3048;
      System.out.println("That is equal to " + meters + " meters.");
   }

   public static void milesToKilometers(Scanner keyboard)
   {
      double miles, kilometers;
      System.out.println("Enter the number of miles: ");
      miles = keyboard.nextDouble();
      kilometers = miles * 1.609;
      System.out.println("That is equal to " + kilometers + " 
kilometers.");
   }
} // end class

程序应使用用户输入1、2或3,然后将该数字输入开关并显示代码。

2 个答案:

答案 0 :(得分:0)

Java通过值传递原始参数,因此尽管您在menuSelection中操纵displayMenu参数,但menuSelection中的main与调用{{1 }}。

您可以将displayMenu的结果分配给displayMenu

menuSelection

您还可以删除传递的menuSelection = displayMenu(keyboard, menuSelection); 作为参数,并在menuSelection内声明它,例如:

displayMenu

答案 1 :(得分:0)

尝试一下:

import java.util.Scanner;

public class menuDriven{
 public static void main(String[] args)
 {
    Scanner keyboard = new Scanner(System.in);

    displayMenu(keyboard);

} // end main

private static void displayMenu(Scanner keyboard)
{

    System.out.println("1. Convert inches to centimeters");
    System.out.println("2. Convert feet to meters");
    System.out.println("3. Convert miles to kilometers");
    System.out.println();

    System.out.println("Enter your selection");
    int menuSelection = keyboard.nextInt();
    while (menuSelection < 1 || menuSelection > 3)
    {
        System.out.println("Please enter 1, 2, or 3.");
        menuSelection = keyboard.nextInt();
    }
    switch(menuSelection)
    {
        case 1:
            inchesToCentimeters(keyboard);
            break;

        case 2:
            feetToMeters(keyboard);
            break;

        case 3:
            milesToKilometers(keyboard);
            break;
    }
    /* return menuSelection; */
}

private static void inchesToCentimeters(Scanner keyboard)
{
    double inches, centimeters;
    System.out.print("Enter the number of inches: ");
    inches = keyboard.nextDouble();
    centimeters = inches * 2.54;
    System.out.println("That is equal to " + centimeters + " centimeters.");

}

private static void feetToMeters(Scanner keyboard)
{
    double feet, meters;
    System.out.println("Enter the number of feet: ");
    feet = keyboard.nextDouble();
    meters = feet * 0.3048;
    System.out.println("That is equal to " + meters + " meters.");
}

private static void milesToKilometers(Scanner keyboard)
{
    double miles, kilometers;
    System.out.println("Enter the number of miles: ");
    miles = keyboard.nextDouble();
    kilometers = miles * 1.609;
    System.out.println("That is equal to " + kilometers + " kilometers.");
 }
} // end class