首页 > 解决方案 > 如何打印我的 LinkedList 中的条目?

问题描述

我需要有关 Java 编程课程作业的帮助。我正在创建一个控制台花(商店)应用程序,它存储花的名称、颜色、年龄和价格。但是,我在打印我创建的用于存储添加的鲜花记录的 LinkedList 时遇到问题。我已经尝试使用 Test 类中的 showFlowers 方法进行了一段时间的调整,但没有任何效果。我会很感激帮助,谢谢。这是我的代码。

花班

public class Flower {
    //variables
    private String name; //name of flower
    private String colour; //colour of flower
    private int age; //age of flower (days)
    private double price; //price of flower

    public Flower(String n, String c, int a, double p) {
        this.name = n;
        this.colour = c;
        this.age = a;
        this.price = p;
    }

    public void getName() {
        System.out.println("Name: " + this.name);
    }

    public void getColour() {
        System.out.println("Colour: " + this.colour);
    }

    public void getAge() {
        System.out.println("Age (Days): " + this.age);
    }

    public void getPrice() {
        System.out.println("Price: " + this.price);
    }

    public void getFullDetails(){
        System.out.println("Name: " + this.name);
        System.out.println("Colour: " + this.colour);
        System.out.println("Age (Days): " + this.age);
        System.out.println("Price: " + this.price);
    }
}

花卉测试班

package flower;

import java.util.LinkedList;
import java.util.Scanner;

public class FlowerTest {

    private static LinkedList<Flower> myFlowers = new LinkedList();

    public static void firstMenu() {
        System.out.println("<------------ Welcome to the Flower Menu -------------->");
        System.out.println("1. Add Flower Details");
        System.out.println("2. Show All Flowers");
        System.out.println("3. Exit");
        System.out.println("Enter choice: ");
    }

    public static void mainMenu() {

        for (;;) {
            firstMenu();
            Scanner inputScanner = new Scanner(System.in);
            int choice = inputScanner.nextInt();

            switch (choice) {
                case 1:
                    createFlower(inputScanner);
                    break;

                case 2:
                    showFlowers(inputScanner);
                    break;

                case 3:
                    System.out.println("Goodbye");
                    System.exit(0);
                    break;

                default:
                    //error handling
                    System.err.println("Unrecognized option");
                    break;
            }
        }
    }

    public static Flower createFlower(Scanner in) {
        System.out.println("<-------- Adding Flower ---------->");
        System.out.println("Input Flower Name: ");
        String name = in.next();
        System.out.println("Input Flower Colour: ");
        String colour = in.next();
        System.out.println("Input Flower Age (Days): ");
        int age = in.nextInt();
        System.out.println("Input Flower Price: ");
        double price = in.nextDouble();
        return new Flower(name, colour, age, price);
    }

    public static void showFlowers(Scanner in){
                    for (Flower flower : myFlowers) {
                        flower.getFullDetails();
                    }
    }
    public static void main(String[] args) {
        mainMenu();
    }
}

标签: javalinked-listjava.util.scanner

解决方案


看起来您的代码从未将任何花添加到您的myFlowers列表中。您的createFlower()方法只是返回新创建的Flower对象,但在您的 switch 语句中永远不会使用返回值。

您应该创建该createFlower()方法void并让它直接将花朵添加到您的列表中,或者让它返回花朵,并让 switch 语句中的处理代码将其添加到列表中。


推荐阅读