繁体   English   中英

如何解决使用多个扫描仪

[英]How to work around using multiple scanners

我有一个程序,该程序可以获取有关计算机的速度,计算机的内存大小等信息(基本上是计算机清单系统)。 据我所知,该程序真的没有错。 我唯一的问题是我不想使用多台扫描仪,因为我已阅读它被认为是不好的做法。 场景是我有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.

在这里,我使用一台扫描仪来收集所有数据,如果您愿意,我可以应您的要求发布更新,并将扫描仪也传递给该方法。 同样的做法几乎适用于任何数据类型。

您可以对此进行扩展以使用ArrayLists,这也非常容易。
试试这个代码:

    public static void main (String [] args) {
    Scanner input = new Scanner (System.in);
    int data1;
    int data2;
    int data3;

    System.out.println ("Enter data1: ");
    data1 = input.nextInt(); //Can use .nextDouble/float/long/short/byte/line/char... ();
    System.out.println ("Enter data2: ");
    data2 = input.nextInt();
    data3 = manipData (data1, data2);

    System.out.println (data1 + " + " + data2 + " = " + data3);

    input.close(); // Be sure to close your scanner
}

public static int manipData (int data1, int data2) {
    return data1 += data2;
}

在级别级别将Scanner声明为static字段。 这样,您就可以为该类的所有方法使用一个Scanner实例,并且对其他类也可用。

public class X {
    public static Scanner scanner;

    public static void main(String[] args) {
        scanner = new Scanner(System.in);
        //...
        foo();
        //...
        scanner.close();
    }

    public static void foo(...) {
        System.out.println("Please enter the value of x:");
        int x = scanner.nextInt();
        System.out.println("The result of working with X: " + realMethod(x));
    }

    public static int realMethod(int x) {
        int result = ...; //some operations done with an X parameter
        return result;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM