我在哪里放置扫描仪以及如何返回值?

时间:2017-10-05 20:32:26

标签: java methods

对于我的编程课程,我有以下任务:

  

在此作业中,您将编写一个可以为宠物建模的程序   商店。该计划将有一个宠物类来模拟个人宠物和   Assignment5类将包含main并充当宠物商店。   用户可以查看宠物,让它们一年一岁,   添加新宠物,并采用任何宠物。

     

创建一个私有静态String方法,打印出主菜单   该计划。然后它接受来自用户的String并返回   他们的选择。列出的命令如下。一个。列出宠物   商店。湾养宠物。 C。添加一个新宠物。 d。采用宠物。即   放弃。一世。您的方法必须验证用户输入的有效输入   在返回输入之前。

我已经获得了我的代码:

  1. 打印菜单
  2. 扫描输入
  3. 将该输入转换为大写
  4. 检查此值是否为" A"," B"," C"," D"或" E& #34;
  5. 如果没有,则要求另一个值
  6. 如果是,则执行一段代码
  7. 我遇到的最大问题是我应该放置此方法,扫描仪和其余代码。解释我应该放在哪里以及为什么会非常有帮助。

    以下是我的一些代码:     import java.util.Scanner;

    public class Assignment5{
    
    // Creates a new scanner
    Scanner scan = new Scanner(System.in);
    
    private static String mainMenu(Scanner scan){
            // Print menu and ask user for input
            System.out.println("A. List the pets in the store");
            System.out.println("B. Age up the pets"); 
            System.out.println("C. Add a new pet"); 
            System.out.println("D. Adopt a pet"); 
            System.out.println("E. Quit");
            System.out.print("Type a letter to make your selection: ");
    
            // Scan for input. Convert to uppercase
            String letter = scan.next().toUpperCase();
    
            // Check if letter is valid. Return true or false
            public static boolean isValidInput(String letter) {
                return (letter == "A" || letter == "B" || letter == "C" || letter == "D" || letter == "E");
            }
    
            // If isValidInput is false, ask the user to input another letter.
            while (!isValidInput(letter){
                System.out.println("That is not one of the options. Input another letter.");
                letter = scan.next().toUpperCase();
            }
            return letter;
        }
    public static void main(String[] args){
    
    
    
    
    
        // Create two pets
        Pet one = new Pet("Spot", 3);
        Pet two = new Pet("Fluffy", 24);
        // Initially, pet #3 has no values attached to it
        Pet three = null;
    
        // Initial greeting
        System.out.println("Welcome to the pet store!");
        mainMenu(scan);
    
    
        // A: List the pets
        if (mainMenu(scan) == "A"){
            System.out.println("Listing pets...");
            System.out.println(one.getName() + " is " + one.getAge() + "-years-old and is currently " + one.getStatus());
            System.out.println(two.getName() + " is " + two.getAge() + "-years-old and is currently " + two.getStatus());
            // Check if there is a third pet before printing its information
            if (three != null){
                System.out.println(three.getName() + " is " + three.getAge() + "-years-old and is currently " + three.getStatus());
            }
            mainMenu(scan);
        }
    

    之后会有更多代码,但我的老师并不希望我们发布我们的完整代码,因为其他人可能会找到并复制它。

    谢谢!

1 个答案:

答案 0 :(得分:1)

我想说你的方法和主要方法的位置看起来不错。一般来说,这些的位置取决于你使用的是谁以及你周围的人使用的编码风格,但是在主要方法之前使用简单的方法就可以了。

至于扫描仪。像它一样把它放在顶部,所以它可以重复用于主菜单和主菜单方法是好的。我会在其上包含一个Access Modifier(public,protected或private)。

粗略估计虽然你的所有初始化都会先行(比如你初始化你的扫描仪)然后你的方法(有些人按照它们的使用顺序排序这些,有些人通过访问修饰符来订购这些对他们而言,那么你最后的主要方法是非常标准的。

不完全确定这是你所要求的,但我相信它与此类似。