2014-09-06 61 views
0

我正在处理此代码,其中弹出一个菜单并且您输入一个选项以输入计算机或显示添加的计算机。然而,我唯一的问题是,当它显示它给我一个空的CPU类型和它的速度。这是假设像这样显示在toString方法上显示错误

\ nBrandName:\ tDell \ n CPU:\ t \ tpentium3,500HZ \ n 内存:\ t \ t398M \ n 价格:\ t \ t $ 1,390.00 \ n \ n

,但它显示像这样

\ nBrandName:\ tDell \ n CPU:\ t \ tnullHZ \ n 内存:\ t \ t398M \ n 价格:\ t \ t $ 1,390.00 \ n \ n

这里是我的代码有三个类一个主Assignment4类一个CPU类和一个计算机类,我相信我的错误是在我的电脑课的某个地方。

这里是我的Assignment4类

// Description: Assignment 4 class displays a menu of choices to a user 
//  and performs the chosen task. It will keep asking a user to 
//  enter the next choice until the choice of 'Q' (Quit) is entered. 

import java.io.*; 
import java.util.*; 

public class Assignment4 
{ 
public static void main (String[] args) 
{ 
    // local variables, can be accessed anywhere from the main method 
    char input1 = 'Z'; 
    String inputInfo; 
    String brandName; 
    double price; 
    int memory; 
    String cpuType; 
    int cpuSpeed; 
    String line = new String(); 

    // instantiate a Computer object 
    Computer computer1 = new Computer(); 

    printMenu(); 

    //Create a Scanner object to read user input 
    Scanner scan = new Scanner(System.in); 


    do // will ask for user input 
    { 
    System.out.println("What action would you like to perform?"); 
    line = scan.nextLine(); 

    if (line.length() == 1) 
     { 
     input1 = line.charAt(0); 
     input1 = Character.toUpperCase(input1); 

     // matches one of the case statement 
     switch (input1) 
     { 
     case 'A': //Add Computer 
      System.out.print("Please enter the computer information:\n"); 
      System.out.print("Enter a brand name:\n"); 
      brandName = scan.nextLine(); 
      computer1.setBrandName(brandName); 

      System.out.print("Enter a computer price:\n"); 
      price = Double.parseDouble(scan.nextLine()); 
      computer1.setPrice(price); 

      System.out.print("Enter a computer memory:\n"); 
      memory = Integer.parseInt(scan.nextLine()); 
      computer1.setMemory(memory); 

      System.out.print("Enter a cpu type:\n"); 
      cpuType = scan.nextLine(); 
      System.out.print("Enter a cpu speed:\n"); 
      cpuSpeed = Integer.parseInt(scan.nextLine()); 
      computer1.setCPU(cpuType, cpuSpeed); 
      break; 
     case 'D': //Display computer 
      System.out.print(computer1); 
      break; 
     case 'Q': //Quit 
      break; 
     case '?': //Display Menu 
      printMenu(); 
      break; 
     default: 
      System.out.print("Unknown action\n"); 
      break; 
     } 
     } 
    else 
     { 
     System.out.print("Unknown action\n"); 
     } 
    } while (input1 != 'Q' || line.length() != 1); 
    } 


    /** The method printMenu displays the menu to a user**/ 
     public static void printMenu() 
    { 
     System.out.print("Choice\t\tAction\n" + 
        "------\t\t------\n" + 
        "A\t\tAdd Computer\n" + 
        "D\t\tDisplay Computer\n" + 
        "Q\t\tQuit\n" + 
        "?\t\tDisplay Help\n\n"); 
     } 
    } 

这里是我的CPU类

public class CPU 
    { 

    private String type = "?"; 
     private int speed= 0;; 

    public CPU(String type, int speed) 
    { 
      this.type = type; 
      this.speed = speed; 
     } 

     public String getType() 
     { 
      return type; 
     } 

     public int getSpeed() 
    { 
     return speed; 
    } 

    public void setType(String type) 
    { 
     this.type = type; 
    } 

    public void setSpeed(int speed) 
    { 
     this.speed = speed; 
    } 

    public String toString() 
    { 
     String result = this.type + "," + this.speed + "HZ"; 

     return result; 
    } 

     } 

,最后我的电脑课

 public class Computer 
    { 
     private String brandName; 
      private int memory; 
      private double price; 
      CPU Cpu; 


     public Computer() 
     { 
      brandName = "?"; 
      memory = 0; 
      price = 0.0; 
      CPU Cpu = new CPU("?", 0); 
     } 

     public String getBrandName() 
     { 
      return brandName; 
     } 

     public CPU getCPU() 
     { 
     return Cpu; 
     } 


    public int getMemory() 
     { 
      return memory; 
     } 

     public double getPrice() 
    { 
      return price; 
     } 

     public void setBrandName(String BrandName) 
     { 
      brandName = BrandName; 
     } 

     public void setCPU(String cpuType, int cpuSpeed) 
     { 

      CPU cpu = new CPU(cpuType, cpuSpeed); 

     } 

    public void setMemory(int memoryAmount) 
     { 
     memory = memoryAmount; 
     } 

     public void setPrice(double price) 
    { 
     this.price = price; 
     } 

     public String toString() 
     { 
      String output = "\n"+"BrandName:"+"\t"+brandName+"\n"+ 
        "CPU:\t\t"+Cpu+"HZ\n"+ 
        "Memory:\t\t"+memory+"M\n"+ 
        "Price:\t\t"+"$"+price+"\n\n"; 
      return output; 
     } 

    } 
+0

查收这行'公共CPU(字符串类型,int speed)'如果'type'和/或'speed'为空 – Arvind 2014-09-06 02:34:11

+0

我改变了值并仍然显示为空?? – jrsk8chivas 2014-09-06 02:38:11

回答

1

你的方法setCPU创建一个新的CPU变量,当方法结束时会被破坏。它应该改变实例变量Cpu,以便保留信息。

+0

好吗?怎么样?我不知道如何设置信息留下来? – jrsk8chivas 2014-09-06 02:36:38

0

你在这里做出改变:

计算机构造:

CPU Cpu = new CPU("?", 0); `to` Cpu = new CPU("?", 0); 

电脑的setCPU(字符串CPUTYPE,INT CPUSPEED)

CPU cpu = new CPU(cpuType, cpuSpeed); `to` 

Cpu.setType(cpuType); 
Cpu.setSpeed(cpuSpeed); 
+0

哦,好吧,这很有道理谢谢!有效! :D – jrsk8chivas 2014-09-06 02:50:16

+0

@ jrsk8chivas,请接受答案,因为它为你工作 – Arvind 2014-09-06 02:52:24

+0

最后一个快速问题..以这种格式打印的价格“$ 1,200.00”我该怎么做? – jrsk8chivas 2014-09-06 03:04:09