gpt4 book ai didi

java - 如何解决使用多个扫描仪的问题

转载 作者:行者123 更新时间:2023-11-30 03:34:09 25 4
gpt4 key购买 nike

我有一个程序,应该获取有关计算机速度、计算机有多少内存等信息(基本上是一个计算机库存系统)。据我所知,该程序没有任何问题。我唯一的问题是我不想使用多个扫描仪,因为我读到它被认为是不好的做法。场景是我有 main 方法,public static void main,充当菜单系统(用户只需输入他想要的选择)。还有其他方法会询问信息,例如您的计算机的速度有多快,您的计算机有多少内存,甚至您是否想从库存系统中删除计算机。这些方法中的每一种都有一个扫描仪对象,我想知道如何将其精简为一个可以与所有数据交互的扫描仪。

更新:这是该程序的完整程序。

package computerinventory;

import java.util.Scanner;
import java.util.ArrayList;

public class ComputerInventory
{
private static Scanner read = new Scanner(System.in);

public static void main(String[] args)
{
ComputerInventory process = new ComputerInventory();
ArrayList<Computer> computer = new ArrayList<>();

int option;

do
{
System.out.println("1.) Add computers to your inventory.");
System.out.println("2.) Display your Inventory.");
System.out.println("3.) Remove Computers from your inventory.");
System.out.println("4.) Quit the program. ");
option = read.nextInt();

switch(option)
{
case 1:
process.addComputers(computer);
System.out.println("");
break;
case 2:
process.displayInventory(computer);
System.out.println("");
break;
case 3:
process.removeComputer(computer);
break;
case 4:
System.out.println("\nThank you for using my program.");
return;
default:
System.out.println("\nThis choice doesn't exist in the menu.");
}
}
while(true);
}

public void addComputers(ArrayList<Computer> computer)
{
double computerSpeed;
int hardDrive;
double ram;
boolean functional;
double cost;

System.out.println("\nHow fast is this computer in Ghz?");
computerSpeed = read.nextDouble();

System.out.println("\nHow big is the HardDrive in GB?");
hardDrive = read.nextInt();

System.out.println("\nHow much ram does this computer has. ");
ram = read.nextDouble();

System.out.println("\nTrue or false, does this computer work?");
functional = read.nextBoolean();

System.out.println("\nHow much does this computer cost? ");
cost = read.nextDouble();

Computer com = new Computer(computerSpeed, hardDrive, ram, functional, cost);
computer.add(com);
}

public void displayInventory(ArrayList<Computer> computer)
{
for (Computer computer1 : computer)
{
System.out.println(computer1);
}
}

public double totalCost()
{
return 0;
}

public void removeComputer(ArrayList<Computer> computer)
{

}
}







package computerinventory;


public class Computer
{
private double computerSpeed;
private int hardDrive;
private double ram;
private boolean functional;
private double cost;

public Computer(double computerSpeed, int hardDrive, double ram, boolean functional, double cost)
{
this.computerSpeed = computerSpeed;
this.hardDrive = hardDrive;
this.ram = ram;
this.functional = functional;
this.cost = cost;
}

public Computer()
{

}

public void setComputerSpeed(double computerSpeed)
{
this.computerSpeed = computerSpeed;
}

public void setHardDrive(int hardDrive)
{
this.hardDrive = hardDrive;
}

public void setRam(double ram)
{
this.ram = ram;
}

public void setFunctional(boolean functional)
{
this.functional = functional;
}

public void setCost(double cost)
{
this.cost = cost;
}

@Override
public String toString()
{
return "\nSpeed is " + computerSpeed + " GHz.\n" + "hardDrive is " + hardDrive
+ " GigaBytes.\n" + "RAM is " + ram + "GigaBytes.\n" + "Status is " + functional
+ "\n" + "The cost of this computer " + cost;
}

}

最佳答案

您可以在主类中使用 Scanner 方法,并可能在主类中提示输入。

public static void main(String[] args)
{
// Scanner object created here
// Ask for information here, variables a and b
// ArrayList that is suppose to contain all the information.

// menu here with four choices
}

public void doSomething(ArrayList<ClassName> obj, int a, int b)
{
// Add paramater variables to the existing array list
}

// More methods as you go down with a scanner object.

关于java - 如何解决使用多个扫描仪的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28373114/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com