如何使用多个扫描仪

时间:2015-02-06 19:16:08

标签: java java.util.scanner

我有一个程序,假设可以获取有关计算机速度,计算机有多少内存等信息(基本上是计算机库存系统)。据我所知,该计划没有什么问题。我唯一的问题是我不想使用多个扫描仪,因为我已经读过它被认为是不好的做法。场景是我有主要方法,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;
}

}

3 个答案:

答案 0 :(得分:1)

您可以在主类中使用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.

答案 1 :(得分:1)

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

你可以扩展这个以使用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;
}

答案 2 :(得分:0)

将级别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;
    }
}