首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >以前的输入被较新的输入覆盖。

以前的输入被较新的输入覆盖。
EN

Stack Overflow用户
提问于 2016-11-22 04:39:40
回答 1查看 66关注 0票数 0

嗨,我真的需要帮助我的程序,我已经到处寻找解决方案,但似乎找不到我想要的。

我正在制作一个程序,用户在其中添加一个桌面,为其输入各种信息,然后将其添加到数组列表中。

下面是代码:

简介:

代码语言:javascript
复制
    Scanner scan = new Scanner(System.in);
    String input;
    boolean looper = true;
    DecimalFormat f = new DecimalFormat("#.00");
    ArrayList<Desktop> desktopList = new ArrayList<>();
    ArrayList<Laptop> laptopList = new ArrayList<>();

    while (looper) {
        System.out.println("");
        System.out.println("******************* Artificial Intelligence Co. *************************");
        System.out.println("1. Add Information for new Desktop");
        System.out.println("2. Add Information for new Laptop");
        System.out.println("3. Display all computer information");
        System.out.println("4. Quit");
        System.out.println("5. Credits");
        System.out.println("*************************************************************************");

开关语句和案例1:

代码语言:javascript
复制
switch (input) {
            case "1":
                System.out.println("");
                System.out.println("=========================================================================");
                System.out.println("Information for new Desktop");
                System.out.println("=========================================================================");

                Desktop xx = new Desktop();
                boolean loop = true;


                while (loop) {
                    System.out.print("What is the Computer ID: ");
                    xx.setComputerID(scan.nextLine().toUpperCase());

                    if ((xx.getComputerID().startsWith("D")) && (xx.getComputerID().length() == 4)) {
                        loop = false;
                    } else {
                        System.out.println("Computer ID should start with a letter \"D\". and have 4 characters.");
                        System.out.println("");
                    }
                }

                loop = true;
                while (loop) {
                    System.out.print("What is the Processor Speed: ");
                    xx.setCPUspeed(scan.nextLine().toUpperCase());
                    try {
                        if (StringisDouble(xx.getCPUspeed().substring(0, (xx.getCPUspeed().length() - 2))) ||
                                StringisDouble(xx.getCPUspeed().substring(0, (xx.getCPUspeed().length() - 3)))) {   //checks the value before GHZ or HZ if its a double
                            if (xx.getCPUspeed().endsWith("GHZ") || xx.getCPUspeed().endsWith("HZ")) {
                                loop = false;
                            } else {
                                System.out.println("CPU Speed input should end with \"GHZ\" or \"HZ\".");
                                System.out.println("");
                            }
                        } else {
                            System.out.println("CPU Speed input should contain a decimal or number followed by  a \"GHZ\" or a \"HZ\".");
                            System.out.println("");
                        }
                    } catch (StringIndexOutOfBoundsException e) {
                        System.out.println("CPU Speed input should contain a decimal or number followed by  a \"GHZ\" or a \"HZ\".");
                        System.out.println("");
                    }

                }

                loop = true;
                while (loop) {
                    System.out.print("What is the RAM: ");
                    xx.setRAM(scan.nextLine().toUpperCase());
                    try {
                        if (StringisInteger(xx.getRAM().substring(0, (xx.getRAM().length() - 2)))) {        //checks the value if it is numeric and ending with GB or MB
                            if (xx.getRAM().endsWith("GB") || xx.getRAM().endsWith("MB")) {
                                loop = false;
                            } else {
                                System.out.println("RAM input should have a numeric value and end with \"GB\" or \"MB\".");
                                System.out.println("");
                            }
                        } else {
                            System.out.println("RAM input should have a numeric value and end with \"GB\" or \"MB\".");
                            System.out.println("");
                        }
                    } catch (StringIndexOutOfBoundsException e) {
                        System.out.println("RAM input should have a numeric value and end with \"GB\" or \"MB\".");
                        System.out.println("");
                    }


                }

                loop = true;
                while (loop) {
                    System.out.print("What is the Harddisk size: ");
                    xx.setHarddisk(scan.nextLine().toUpperCase());
                    try {
                        if (StringisInteger(xx.getHarddisk().substring(0, (xx.getHarddisk().length() - 2)))) {        //checks the value if it is numeric and ending with GB or MB
                            if (xx.getHarddisk().endsWith("GB") || xx.getHarddisk().endsWith("TB")) {
                                loop = false;
                            } else {
                                System.out.println("Harddisk input should have a numeric value and end with \"GB\" or \"TB\".");
                                System.out.println("");
                            }
                        } else {
                            System.out.println("Harddisk input should have a numeric value and end with \"GB\" or \"TB\".");
                            System.out.println("");
                        }
                    } catch (StringIndexOutOfBoundsException e) {
                        System.out.println("Harddisk input should have a numeric value and end with \"GB\" or \"TB\".");
                        System.out.println("");
                    }
                }


                loop = true;
                while (loop) {
                    System.out.print("What is the Monitor: ");
                    xx.setMonitor(scan.nextLine().toUpperCase());
                    if (xx.getMonitor().equals("CRT") || xx.getMonitor().equals("LCD")) {
                        loop = false;
                    } else {
                        System.out.println("Please enter in CRT or LCD only.");
                        System.out.println("");
                    }
                }

                loop = true;
                while (loop) {
                    try {
                        System.out.print("What is the price: $");
                        xx.setPrice(Double.parseDouble(scan.nextLine()));
                        loop = false;
                    } catch (NumberFormatException e) {
                        System.out.println("Price input should be numeric.");
                        System.out.println("");
                    }
                }

                desktopList.add(xx);

                System.out.println("Information successfully added.");
                System.out.println("");
                System.out.println("");
                break;

案例3,用户可以看到他/她输入了什么:

代码语言:javascript
复制
                case "3":
                int DesktopCounter = 1;
                int LaptopCounter = 1;

                System.out.println("");
                if (desktopList.isEmpty()) {
                    System.out.println("No desktop added!");
                    System.out.println("");
                } else {
                    for (int i = 0; i < desktopList.size(); i++) {
                        System.out.println("");
                        System.out.println("Desktop " + DesktopCounter);
                        System.out.println("Computer ID: " + desktopList.get(i).getComputerID());
                        System.out.println("Processor Speed: " + desktopList.get(i).getCPUspeed());
                        System.out.println("RAM: " + desktopList.get(i).getRAM());
                        System.out.println("Harddisk:" + desktopList.get(i).getHarddisk());
                        System.out.println("Monitor: " + desktopList.get(i).getMonitor());
                        System.out.println("Price: $" + f.format(desktopList.get(i).getPrice()));
                        DesktopCounter++;
                    }
                }
                break;

桌面类:

代码语言:javascript
复制
       public class Desktop extends Computer //Child class of Computer
    {
        private static String Monitor;

        public Desktop()
        {
            ComputerID = "-- No ID specified --";
            CPUspeed = "-- No processor speed specified --";
            RAM = "-- No RAM specified-";
            Harddisk = "-- No Harddisk size specified --";
            Monitor = "-- No Monitor specified --";
            Price = 0.0;
        }



//Setters and Getters
    public String getMonitor()
    {
        return Monitor;
    }

    public   void setMonitor(String monitor)
    {
        Monitor = monitor;
    }


}

计算机类:

代码语言:javascript
复制
public class Computer //Parent class

{
protected static String ComputerID;
protected static String CPUspeed;
protected static String RAM;
protected static String Harddisk;
protected static double Price;


public Computer() //Initializer
{
    ComputerID = "-- No ID specified --";
    CPUspeed = "-- No processor speed specified --";
    RAM = "-- No amount RAM specified-";
    Harddisk = "-- No Harddisk size specified --";
    Price = 0.0;
}

public Computer(String computerID, String cpuspeed, String ram, String harddisk, double price) {
    ComputerID = computerID;
    CPUspeed = cpuspeed;
    RAM = ram;
    Harddisk = harddisk;
    Price = price;


}


//Getters and Setters
public String getComputerID() {
    return ComputerID;
}

public void setComputerID(String computerID) {
    ComputerID = computerID;
}

public String getCPUspeed() {
    return CPUspeed;
}

public void setCPUspeed(String cpuspeed) {
    CPUspeed = cpuspeed;
}

public String getRAM() {
    return RAM;
}

public void setRAM(String ram) {
    RAM = ram;
}

public String getHarddisk() {
    return Harddisk;
}

public void setHarddisk(String harddisk) {
    Harddisk = harddisk;
}

public double getPrice() {
    return Price;
}

public void setPrice(double price) {
    Price = price;
}

//End of getters and setters

}

现在假设我使用案例1添加了一个桌面,并输入了以下信息:

  • 计算机ID: D001
  • 处理器速度: 3.2GHZ
  • RAM: 512 RAM
  • 硬盘:80
  • 监测器: CRT

然后继续添加另一个具有以下内容的桌面:

  • 计算机ID: D123
  • 处理器速度: 4.4GHZ
  • RAM: 8GB
  • 硬盘: 1TB
  • 显示器:液晶显示器

当我使用case 3代码块显示信息时,它会输出:

  • 桌面1
  • 计算机ID: D123
  • 处理器速度: 4.4GHZ
  • RAM: 8GB
  • 硬盘: 1TB
  • 显示器:液晶显示器
  • 桌面2
  • 计算机ID: D123
  • 处理器速度: 4.4GHZ
  • RAM: 8GB
  • 硬盘: 1TB
  • 显示器:液晶显示器

右转时,Desktop 1应该显示它自己的唯一属性。

我很感谢你的帮助。

编辑:我通过使变量非静态来解决这个问题。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-11-22 05:15:19

发布额外代码后的更新

您的字段在DesktopComputerLaptop类中不应该是静态的。更改以下字段:

代码语言:javascript
复制
protected static String ComputerID;
protected static String CPUspeed;
protected static String RAM;
protected static String Harddisk;
protected static double Price;

代码语言:javascript
复制
protected String ComputerID;
protected String CPUspeed;
protected String RAM;
protected String Harddisk;
protected double Price;

静态字段对于类的每个对象都是相同的。因此,如果您有多台台式机,并且声明了Price静态,那么所有桌面都将共享相同的价格字段。这不是你想要的,显然所有的台式机都有另外的价格。

也变了

代码语言:javascript
复制
private static String Monitor;

代码语言:javascript
复制
private String Monitor;

您确实希望每台计算机都有一个单独的监视器,而不是为所有计算机共享相同的监视器。

旧答案

代码在这里运行得很好,所以一定有一些缺失的信息:问题一定在您没有发布的代码的某个部分:

代码语言:javascript
复制
******************* Artificial Intelligence Co. *************************
1. Add Information for new Desktop
2. Add Information for new Laptop
3. Display all computer information
4. Quit
5. Credits
*************************************************************************
3

Desktop 1
Computer ID: D123
Processor Speed: 2GHZ
RAM: 2GB
Harddisk:1TB
Monitor: CRT
Price: $100.00

Desktop 2
Computer ID: D002
Processor Speed: 2GHZ
RAM: 16GB
Harddisk:2TB
Monitor: CRT
Price: $500.00

一些想法:

  • 您没有发布Desktop类,如果Desktop类中的字段声明为静态,这将解释结果,确保它们不是静态的。
  • 看起来您在某个地方使用相同的变量,例如,在case语句中,Desktop xx = new Desktop();并不像上面代码中的那样
  • 您可能使用了错误的索引来获取列表中的项,您正在使用for-i循环循环并执行desktopList.get(i),如果您碰巧执行了desktopList.get(0)desktopList.get(someVariableThatIsAlwaysZero)操作,则始终会打印相同的结果。使用新的foreach语法更安全:for (Desktop desktop : desktopList){ ... }
  • 您必须学习如何使用调试器,一步一步地遍历代码,查看重复值插入的位置,如果没有插入重复的值,请查看打印语句的打印位置以及它们为什么打印相同的值;使用调试器查找问题非常重要。
  • 您的代码可能不同步。

我只会给出我的版本,所以您可能需要比较一下:

代码语言:javascript
复制
public static void main(String args[]) {

        Scanner scan = new Scanner(System.in);
        String input;
        boolean looper = true;
        DecimalFormat f = new DecimalFormat("#.00");
        ArrayList<Desktop> desktopList = new ArrayList<>();
        ArrayList<Laptop> laptopList = new ArrayList<>();

        while (looper) {
            System.out.println("");
            System.out.println("******************* Artificial Intelligence Co. *************************");
            System.out.println("1. Add Information for new Desktop");
            System.out.println("2. Add Information for new Laptop");
            System.out.println("3. Display all computer information");
            System.out.println("4. Quit");
            System.out.println("5. Credits");
            System.out.println("*************************************************************************");

            input = scan.nextLine();

            switch (input) {
                case "1":
                    System.out.println("");
                    System.out.println("=========================================================================");
                    System.out.println("Information for new Desktop");
                    System.out.println("=========================================================================");

                    Desktop xx = new Desktop();
                    boolean loop = true;


                    while (loop) {
                        System.out.print("What is the Computer ID: ");
                        xx.setComputerID(scan.nextLine().toUpperCase());

                        if ((xx.getComputerID().startsWith("D")) && (xx.getComputerID().length() == 4)) {
                            loop = false;
                        } else {
                            System.out.println("Computer ID should start with a letter \"D\". and have 4 characters.");
                            System.out.println("");
                        }
                    }

                    loop = true;
                    while (loop) {
                        System.out.print("What is the Processor Speed: ");
                        xx.setCPUspeed(scan.nextLine().toUpperCase());
                        try {
                            if (StringisDouble(xx.getCPUspeed().substring(0, (xx.getCPUspeed().length() - 2))) ||
                                    StringisDouble(xx.getCPUspeed().substring(0, (xx.getCPUspeed().length() - 3)))) {   //checks the value before GHZ or HZ if its a double
                                if (xx.getCPUspeed().endsWith("GHZ") || xx.getCPUspeed().endsWith("HZ")) {
                                    loop = false;
                                } else {
                                    System.out.println("CPU Speed input should end with \"GHZ\" or \"HZ\".");
                                    System.out.println("");
                                }
                            } else {
                                System.out.println("CPU Speed input should contain a decimal or number followed by  a \"GHZ\" or a \"HZ\".");
                                System.out.println("");
                            }
                        } catch (StringIndexOutOfBoundsException e) {
                            System.out.println("CPU Speed input should contain a decimal or number followed by  a \"GHZ\" or a \"HZ\".");
                            System.out.println("");
                        }

                    }

                    loop = true;
                    while (loop) {
                        System.out.print("What is the RAM: ");
                        xx.setRAM(scan.nextLine().toUpperCase());
                        try {
                            if (StringisInteger(xx.getRAM().substring(0, (xx.getRAM().length() - 2)))) {        //checks the value if it is numeric and ending with GB or MB
                                if (xx.getRAM().endsWith("GB") || xx.getRAM().endsWith("MB")) {
                                    loop = false;
                                } else {
                                    System.out.println("RAM input should have a numeric value and end with \"GB\" or \"MB\".");
                                    System.out.println("");
                                }
                            } else {
                                System.out.println("RAM input should have a numeric value and end with \"GB\" or \"MB\".");
                                System.out.println("");
                            }
                        } catch (StringIndexOutOfBoundsException e) {
                            System.out.println("RAM input should have a numeric value and end with \"GB\" or \"MB\".");
                            System.out.println("");
                        }


                    }

                    loop = true;
                    while (loop) {
                        System.out.print("What is the Harddisk size: ");
                        xx.setHarddisk(scan.nextLine().toUpperCase());
                        try {
                            if (StringisInteger(xx.getHarddisk().substring(0, (xx.getHarddisk().length() - 2)))) {        //checks the value if it is numeric and ending with GB or MB
                                if (xx.getHarddisk().endsWith("GB") || xx.getHarddisk().endsWith("TB")) {
                                    loop = false;
                                } else {
                                    System.out.println("Harddisk input should have a numeric value and end with \"GB\" or \"TB\".");
                                    System.out.println("");
                                }
                            } else {
                                System.out.println("Harddisk input should have a numeric value and end with \"GB\" or \"TB\".");
                                System.out.println("");
                            }
                        } catch (StringIndexOutOfBoundsException e) {
                            System.out.println("Harddisk input should have a numeric value and end with \"GB\" or \"TB\".");
                            System.out.println("");
                        }
                    }


                    loop = true;
                    while (loop) {
                        System.out.print("What is the Monitor: ");
                        xx.setMonitor(scan.nextLine().toUpperCase());
                        if (xx.getMonitor().equals("CRT") || xx.getMonitor().equals("LCD")) {
                            loop = false;
                        } else {
                            System.out.println("Please enter in CRT or LCD only.");
                            System.out.println("");
                        }
                    }

                    loop = true;
                    while (loop) {
                        try {
                            System.out.print("What is the price: $");
                            xx.setPrice(Double.parseDouble(scan.nextLine()));
                            loop = false;
                        } catch (NumberFormatException e) {
                            System.out.println("Price input should be numeric.");
                            System.out.println("");
                        }
                    }

                    desktopList.add(xx);

                    System.out.println("Information successfully added.");
                    System.out.println("");
                    System.out.println("");
                    break;

                case "3":
                    int DesktopCounter = 1;
                    int LaptopCounter = 1;

                    System.out.println("");
                    if (desktopList.isEmpty()) {
                        System.out.println("No desktop added!");
                        System.out.println("");
                    } else {
                        for (int i = 0; i < desktopList.size(); i++) {
                            System.out.println("");
                            System.out.println("Desktop " + DesktopCounter);
                            System.out.println("Computer ID: " + desktopList.get(i).getComputerID());
                            System.out.println("Processor Speed: " + desktopList.get(i).getCPUspeed());
                            System.out.println("RAM: " + desktopList.get(i).getRAM());
                            System.out.println("Harddisk:" + desktopList.get(i).getHarddisk());
                            System.out.println("Monitor: " + desktopList.get(i).getMonitor());
                            System.out.println("Price: $" + f.format(desktopList.get(i).getPrice()));
                            DesktopCounter++;
                        }
                    }
                    break;
            }
        }
    }

    private static boolean StringisInteger(String substring) {return true;}

    private static boolean StringisDouble(String substring) { return true; }

我的桌面类如下所示:

代码语言:javascript
复制
@Data
public class Desktop {

    private double price;
    private String computerID;
    private String CPUspeed;
    private String RAM;
    private String harddisk;
    private String monitor;

}

其中,@Data是一个Lombok注释,它生成setters和getter。正如你所看到的,这些字段不是静态的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40733880

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档