猿问

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

我被分配创建一个菜单驱动程序,其中包含一个显示菜单屏幕/获取用户输入的功能,但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.");

   }



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


慕侠2389804
浏览 84回答 2
2回答

红颜莎娜

Java 按值传递原始参数,因此尽管您在 in 中操作menuSelection参数displayMenu,但menuSelectioninmain保持与调用之前相同displayMenu。您可以将结果分配displayMenu给menuSelection:menuSelection = displayMenu(keyboard, menuSelection);您还可以删除menuSelection作为参数的传递并在内部声明它displayMenu,例如:public static int displayMenu(Scanner keyboard) {&nbsp; &nbsp; int menuSelection=0;&nbsp; &nbsp; //...}

海绵宝宝撒

尝试这个:import java.util.Scanner;public class menuDriven{&nbsp;public static void main(String[] args)&nbsp;{&nbsp; &nbsp; Scanner keyboard = new Scanner(System.in);&nbsp; &nbsp; displayMenu(keyboard);} // end mainprivate static void displayMenu(Scanner keyboard){&nbsp; &nbsp; System.out.println("1. Convert inches to centimeters");&nbsp; &nbsp; System.out.println("2. Convert feet to meters");&nbsp; &nbsp; System.out.println("3. Convert miles to kilometers");&nbsp; &nbsp; System.out.println();&nbsp; &nbsp; System.out.println("Enter your selection");&nbsp; &nbsp; int menuSelection = keyboard.nextInt();&nbsp; &nbsp; while (menuSelection < 1 || menuSelection > 3)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Please enter 1, 2, or 3.");&nbsp; &nbsp; &nbsp; &nbsp; menuSelection = keyboard.nextInt();&nbsp; &nbsp; }&nbsp; &nbsp; switch(menuSelection)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; case 1:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; inchesToCentimeters(keyboard);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case 2:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; feetToMeters(keyboard);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case 3:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; milesToKilometers(keyboard);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; }&nbsp; &nbsp; /* return menuSelection; */}private static void inchesToCentimeters(Scanner keyboard){&nbsp; &nbsp; double inches, centimeters;&nbsp; &nbsp; System.out.print("Enter the number of inches: ");&nbsp; &nbsp; inches = keyboard.nextDouble();&nbsp; &nbsp; centimeters = inches * 2.54;&nbsp; &nbsp; System.out.println("That is equal to " + centimeters + " centimeters.");}private static void feetToMeters(Scanner keyboard){&nbsp; &nbsp; double feet, meters;&nbsp; &nbsp; System.out.println("Enter the number of feet: ");&nbsp; &nbsp; feet = keyboard.nextDouble();&nbsp; &nbsp; meters = feet * 0.3048;&nbsp; &nbsp; System.out.println("That is equal to " + meters + " meters.");}private static void milesToKilometers(Scanner keyboard){&nbsp; &nbsp; double miles, kilometers;&nbsp; &nbsp; System.out.println("Enter the number of miles: ");&nbsp; &nbsp; miles = keyboard.nextDouble();&nbsp; &nbsp; kilometers = miles * 1.609;&nbsp; &nbsp; System.out.println("That is equal to " + kilometers + " kilometers.");&nbsp;}} // end class
随时随地看视频慕课网APP

相关分类

Java
我要回答