2016-11-22 5 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; 

  • コンピュータID:デスクトップ・クラス:

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

    コンピュータクラス:私はで入力した次の情報をケース1を使用してデスクトップを追加する場合、今言う

    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 
    

    }

    :D001

  • プロセッサ速度:3.2GHZ
  • RAM:512メガバイト
  • ハードディスクの:80ギガバイト
  • モニター:その後、CRT

そして、これらを別のデスクトップを追加するために進みます

  • コンピュータID:D123
  • プロセッサ速度: 4.4GHZ
  • RAM:8GB
  • ハードディスク: 1TB
  • モニター:LCD

私はケース3のコードブロックを使用して情報を表示すると、それは出力:

  • をデスクトップ1
  • コンピュータID:D123
  • プロセッサ速度:4 。4GHzの
  • RAM:8ギガバイト
  • ハードディスクの:1TB
  • モニター:LCD


  • デスクトップ2
  • コンピュータID:D123
  • プロセッサ速度:4.4GHZ
  • RAM:8GB
  • ハードディスクの:1TB
  • モニター:右によるLCD

、デスクトップ1は、独自の属性を表示する必要があります。

私は助けていただきありがとうございます。

EDIT:私の変数を非静的にすることでこの問題を解決しました。余分なコードの後

+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 String Monitor; 

private static 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クラスのフィールドがstati宣言されている場合

  • あなたは、Desktopクラスを投稿しませんでしたC、その結果を説明し、静的と宣言されていないことを確認してください。
  • あなたはどこか同じ変数を使用しているようです。Desktop xx = new Desktop();は上記のコードのcase文にはありません
  • あなたはリストの項目を取得するために間違ったインデックスを使用していますが、for-iループでループしていて、desktopList.get(0)を行うとdesktopList.get(i)desktopList.get(someVariableThatIsAlwaysZero)あなたは常に同じ結果を印刷します。 for (Desktop desktop : desktopList){ ... }
  • あなたはデバッガの使い方を学び、コードをステップバイステップで実行し、重複する値がどこに挿入されているかを確認し、重複した値が挿入されていない場合は、printステートメントは印刷され、なぜ同じ値を印刷するのか?デバッガを使用すると、問題を見つけることが本当に重要である
  • あなたのコードが同期してあるかもしれない

比較したいかもしれませんので、私はただ、あなたに働く私のバージョンを提供します:

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はセッターとゲッターを生成ロンボク注釈である

@Data 
public class Desktop { 

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

} 

:デスクトップクラスは次のようになります。ご覧のように、フィールドは静的ではありません。

+0

こんにちは、おかげさまで、ありがとうございますが、私はセッターを非静的にしても正しい出力を得ることができません。また、DesktopクラスとComputerクラスも追加しました。 –

+0

'プライベート静的String Monitor; 'これらのフィールドは静的であるべきではありません。静的キーワードを削除してください:' private String monitor' –

+0

私は自分の答えを編集しました。 –

関連する問題