2016-11-22 76 views
0

嗨我真的需要我的程序幫助,我已經搜索了高和低的解決方案,但似乎無法找到我在找什麼。以前的輸入被新輸入覆蓋

我正在製作一個程序,用戶添加一個桌面,爲其輸入各種信息,然後將其添加到數組列表中。

下面的代碼:

簡介:

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("*************************************************************************"); 

switch語句和案例1:

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,在用戶獲取看到他/她進入什麼:

   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; 

桌面級:

 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; 
    } 


} 

計算機類:

  • 計算機ID:

    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以下信息桌面中輸入:D001

  • 處理器速度:3.2GHZ
  • RAM:512MB
  • 硬盤:80GB
  • 顯示器:CRT

,然後繼續添加其他桌面這些:

  • 計算機ID:D123
  • 處理器速度: 4.4GHZ
  • RAM:8GB
  • 硬盤: 1TB
  • 顯示器:LCD

當我顯示使用外殼3碼塊中的信息時,其輸出:

  • 桌面1
  • 計算機ID:D123
  • 處理器速度:4 。4GHZ
  • 內存:8GB
  • 硬盤:1TB
  • 顯示器:液晶


  • 桌面2
  • 計算機ID:D123
  • 處理器速度:4.4GHZ
  • RAM:8GB
  • 硬盤:1TB
  • 顯示器:液晶

當權,桌面1應該顯示自己的獨特屬性。

我將不勝感激任何幫助。

編輯:我通過讓我的變量非靜態來解決這個問題。經過額外的代碼

+0

你可以把完整的代碼,而不是代碼片段,這樣很容易找到變量/引用正在初始化和循環打破。 –

回答

0

更新被張貼

你的領域不應該是靜態DesktopComputerLaptop類。更改這些字段:

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

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

靜態字段是一類的每個對象相同。因此,如果您有多個桌面,並且您聲明價格靜態,則所有桌面將共享相同的價格字段。這不是你想要的,顯然所有的臺式機都有另一個價格。

而且改變

private static String Monitor; 

private String Monitor; 

你真的想擁有計算機每一個單獨的顯示器,而不是共享同一個顯示器的所有計算機。

老答案

代碼運行完全在這裏,所以一定是有缺失的信息:這個問題必須在代碼的某些部分,你沒有張貼:

******************* 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類中的字段聲明爲static c,這將解釋結果,確保它們沒有被聲明爲靜態的
  • 它看起來像你在某處使用相同的變量,例如,Desktop xx = new Desktop();不在case語句如在上述
  • 你可能使用了錯誤的索引來獲取列表中的項目的代碼,你是一個對-i循環的循環,做desktopList.get(i)如果你碰巧做desktopList.get(0)desktopList.get(someVariableThatIsAlwaysZero)您將始終打印相同的結果。使用新的foreach語法更安全:for (Desktop desktop : desktopList){ ... }
  • 您必須學會使用調試器,逐步瀏覽代碼並查看插入重複值的位置,並且如果沒有插入重複值,請參閱打印位置語句正在打印以及爲什麼它們打印相同的值;使用調試器是非常重要的發現問題
  • 你的代碼可能是不同步的

我只是給你我的版本的作品,所以你可能想比較:

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; } 

我桌面類看起來是這樣的:

@Data 
public class Desktop { 

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

} 

@Data是龍目註釋生成getter和setter方法。正如你所看到的,這些字段不是靜態的。

+0

嗨,感謝您的幫助,但我仍然無法獲得正確的輸出,即使我已經使setter非靜態。我還添加了Desktop類和Computer類。 –

+0

'私有靜態字符串監視器; '這些字段不應該是靜態的,刪除static關鍵字:'private String monitor' –

+0

我編輯了我的答案,希望它對你有效 –