java - 如何将此shoutOut Canned Message() 方法放入新类中?

标签 java

这就是程序,我想将 shoutOutCannedMessage() 放入其自己的名为 shoutbox 的类中。我不知道如何将所选方法 shoutOutCannedMessage() 放入名为 shoutbox 的新类中。有人可以帮我解决这个问题吗?我有点头晕,因为我已经有一段时间没有接触过这个了,而且我还是 Java 新手。

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package virtualworld;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @author MTAH
 */

public class Virtualworld {

    /**
     * @param args the command line arguments
     */

//to remove code from main, I have created separate methods
//myApp gets whatever methods are in run() to execute
    public static void main(String[] args) {
        // TODO code application logic here

        Virtualworld myApp = new Virtualworld();
        myApp.run();

    }

    private List<String> list;

    //run is a method that gets the methods createClone and createPet to run
    public void run() {

        createMenu();
        shoutOutCannedMessage();
        createClone();
        createPet();


    }

/*this is the createClone method, it takes input and manipulates the myclone class
it produces an output by plugging in the data taken to myclone class and 
produces the introduction
*/


    private void createMenu() {
        list = new ArrayList();
        list.add(0, " ");
        list.add(1, "1. Java is fun!");
        list.add(2, "2. Greece is famous for its archaeological history. ");
        list.add(3, "3. Spain has great churros and hot chocolate. ");
        list.add(4, "4. Sweden has beautiful architecture. ");
        list.add(5, "5. Choose Denmark if you love Lego! ");
        list.add(6, "6. South Africa has a great Safari option by Sun City!");
        list.add(7, "7. Japan is fun and filled with gorgeous cherry trees.");
        list.add(8, "8. The U.K. is a place with history right next to modernity");
        list.add(9, "9. This is a project for IT 511");
        list.add(10, "10. This was created by ");
    }

    private void shoutOutCannedMessage() {
        System.out.println("This is a list of options: ");
        int size = list.size();

        for (int i = 0; i < size; i++) {
            System.out.println(list.get(i));
        }

        int userNumber;
        Scanner scanner = new Scanner(System.in);
        System.out.println("Please enter number: ");
        userNumber = scanner.nextInt();
        System.out.println(list.get(userNumber));


    }


    private void createClone() {

        Scanner input = new Scanner(System.in);

        System.out.println("Please enter your first name: ");
        String firstName = input.next();

        System.out.println("Please enter your last name: ");
        String lastName = input.next();

        myclone individual = new myclone(firstName, lastName);
        System.out.println(individual.introduction());

    }

/*this is the createPet method, it takes input and manipulates the pet class
it produces an output by plugging in the data taken to pet class and 
produces the pet's introduction
*/

    private void createPet() {

        Scanner input = new Scanner(System.in);

        System.out.println("Please enter the type of animal your pet will be (Ex. cat, dog, etc): ");
        String animalType = input.next();

        System.out.println("Please enter what color you want your pet to be (Ex. blue, green, brown, white):  ");
        String animalColor = input.next();

        System.out.println("Please enter your pet's name: ");
        String animalName = input.next();

        pet kind = new pet(animalType, animalColor, animalName);
        System.out.println(kind.petIntroduction());


    }


}

最佳答案

您可以在自己的类中推断该方法并将其设为静态,因为它不会修改任何状态。它唯一的依赖项似乎是列表,您可以将其作为参数传递。像这样的东西。

public class ShoutBox {

    public static void shoutOutCannedMessage(List<String> list) {
       System.out.println("This is a list of options: ");
       int size = list.size();

       for(int i=0; i<size; i++)
       {
           System.out.println(list.get(i));
       }

       int userNumber;
       Scanner scanner = new Scanner(System.in);
       System.out.println("Please enter number: ");
       userNumber = scanner.nextInt();
       System.out.println(list.get(userNumber));


   }

}

然后您可以将该方法调用为 Shoutbox.shoutOutCannedMessage(list);

关于java - 如何将此shoutOut Canned Message() 方法放入新类中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35372485/

相关文章:

c# - Java、Java EE、C#、asp.net 和 SOA 的 channel

Java Swing : JTable with subitems

java - 如何让 jjs --add-opens 在 Java 9 中工作?

Java 从 findFirst() 返回一个 Optional<String>

java - 编译错误 "illegal start of expression"

java - 是否可以在构造函数中子类化 Java 对象?

java - Java IOException使Gradle构建失败

java - NoClassDefFoundError,在同一目录下为什么程序找不到jar

Java - 这段代码的解释是什么?

java - 统一成本搜索实现