0
這是該程序,我想將shoutOutCannedMessage()
放入其自己的類shoutbox
。我不知道如何着手將選定的方法shoutOutCannedMessage()
加入到名爲shoutbox
的新類中。有人可以幫助我嗎?我有一點腦霧,因爲我已經選擇了這一點,並且我是Java新手。如何將此shoutOutCannedMessage()方法放入新類中?
/*
* 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());
}
}
謝謝!這似乎是訣竅! – MTAH