我正在处理此代码,其中弹出一个菜单,您可以选择进入计算机或显示添加的计算机。然而,我唯一的问题是当它显示它给我一个空的CPU类型及其速度。假设像这样显示
\ nBrandName:\ tDell \ n CPU:\吨\ tpentium3,500HZ \ n 存储器:\吨\ t398M \ n 价:\吨\吨$ 1,390.00 \ n \ n
但它显示如下
\ nBrandName:\ tDell \ n CPU:\吨\ tnullHZ \ n 存储器:\吨\ t398M \ n 价:\吨\吨$ 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 :(得分:1)
您在方法CPU
中创建一个新的setCPU
变量,该方法在方法结束时被销毁。它应该改为更改实例变量Cpu
,以便保留信息。
答案 1 :(得分:0)
您在此处进行了更改:
计算机构造函数:
CPU Cpu = new CPU("?", 0); `to` Cpu = new CPU("?", 0);
计算机的setCPU(String cpuType,int cpuSpeed)
CPU cpu = new CPU(cpuType, cpuSpeed); `to`
Cpu.setType(cpuType);
Cpu.setSpeed(cpuSpeed);