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

Java 如何解决使用多个扫描仪的问题,java,java.util.scanner,Java,Java.util.scanner,我有一个程序,用来记录计算机的速度、计算机的内存容量等信息(基本上是一个计算机清单系统)。据我所知,这个节目没有什么问题。我唯一的问题是,我不想使用多个扫描仪,因为我已经读到它被认为是不好的做法。场景是我有一个主方法,publicstaticvoidmain,作为一个菜单系统(用户只需输入他想要的选项)。还有其他一些方法可以询问信息,比如你的计算机有多快,你的计算机有多少ram,甚至你是否想从你的库存系统中删除一台计算机。每种方法都有一个scanner对象,我想知道如何将其精简为一个可以与所有数

我有一个程序,用来记录计算机的速度、计算机的内存容量等信息(基本上是一个计算机清单系统)。据我所知,这个节目没有什么问题。我唯一的问题是,我不想使用多个扫描仪,因为我已经读到它被认为是不好的做法。场景是我有一个主方法,publicstaticvoidmain,作为一个菜单系统(用户只需输入他想要的选项)。还有其他一些方法可以询问信息,比如你的计算机有多快,你的计算机有多少ram,甚至你是否想从你的库存系统中删除一台计算机。每种方法都有一个scanner对象,我想知道如何将其精简为一个可以与所有数据交互的scanner

更新:这里是一个完整的程序的程序

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;
}

}
包装清单;
导入java.util.Scanner;
导入java.util.ArrayList;
公共类计算机目录
{
专用静态扫描仪读取=新扫描仪(System.in);
公共静态void main(字符串[]args)
{ 
ComputerInventory process=新的ComputerInventory();
ArrayList计算机=新的ArrayList();
int选项;
做
{
System.out.println(“1.)将计算机添加到您的库存中。”);
System.out.println(“2.)显示您的库存。”);
System.out.println(“3.)从您的库存中删除计算机。”);
System.out.println(“4.)退出程序。”);
option=read.nextInt();
开关(选件)
{
案例1:
过程。添加计算机(计算机);
System.out.println(“”);
打破
案例2:
处理。显示库存(计算机);
System.out.println(“”);
打破
案例3:
进程。远程计算机(计算机);
打破
案例4:
System.out.println(“\n感谢您使用我的程序。”);
返回;
违约:
System.out.println(“\n此选项在菜单中不存在。”);
}
}
虽然(正确);
}
公用计算机(ArrayList计算机)
{
双计算机速度;
int硬盘;
双闸板;
布尔泛函;
双重成本;
System.out.println(“\n这台计算机以Ghz为单位有多快?”);
computerSpeed=read.nextDouble();
System.out.println(“\n以GB为单位的硬盘有多大?”);
硬盘驱动器=read.nextInt();
System.out.println(“\n这台计算机有多少ram。”);
ram=read.nextDouble();
System.out.println(“\n错误或错误,这台计算机工作吗?”);
functional=read.nextBoolean();
System.out.println(“\n这台计算机多少钱?”);
成本=read.nextDouble();
计算机com=新计算机(计算机速度、硬盘、ram、功能、成本);
computer.add(com);
}
公共库存(ArrayList计算机)
{   
用于(计算机1:计算机)
{
系统输出打印LN(计算机1);
}
}
公共双重总成本()
{
返回0;
}
公共无效删除计算机(ArrayList计算机)
{
}
}
包装计算机库存;
公共类计算机
{
私人双计算机速度;
专用int硬盘;
专用双ram;
私有布尔泛函;
私人双重成本;
公用计算机(双计算机速度、int硬盘、双ram、布尔函数、双成本)
{
这个.computerSpeed=computerSpeed;
this.hardrive=硬盘驱动器;
this.ram=ram;
这是功能性的;
成本=成本;
}
公共计算机()
{
}
公共无效设置计算机速度(双倍计算机速度)
{
这个.computerSpeed=computerSpeed;
}
公共无效设置硬盘驱动器(内部硬盘驱动器)
{
this.hardrive=硬盘驱动器;
}
公共无效设置ram(双ram)
{
this.ram=ram;
}
公共void setFunctional(布尔函数)
{
这是功能性的;
}
公共成本(双倍成本)
{
成本=成本;
}
@凌驾
公共字符串toString()
{
返回“\n速度为“+computerSpeed+”GHz。\n“+”硬盘驱动器为“+硬盘驱动器”
+千兆字节。\n“+”RAM是“+RAM+”千兆字节。\n“+”状态是“+功能”
+“\n”+“此计算机的成本”+成本;
}
}

您可以在主类中使用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.
publicstaticvoidmain(字符串[]args)
{
//在此处创建的扫描仪对象
//在这里询问变量a和b的信息
//假定包含所有信息的ArrayList。
//这里的菜单有四种选择
}
公共无效剂量测定(ArrayList obj,int a,int b)
{
//将paramater变量添加到现有数组列表中
}
//当你使用扫描器对象时,会有更多的方法。

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

您可以在此基础上展开以使用ArrayList,这也非常简单。
请尝试以下代码:

    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;
}

扫描仪
在级别类中声明为
静态
字段。这样,就有一个
扫描器
实例可用于该类的所有方法,也可用于其他类

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;
    }
}

实际上,您可以将扫描仪作为函数参数传递。我经常这样做,它节省了内存空间,使它更干净。确保在main结束时关闭扫描仪,而不是在函数本身中!我知道,但我从人们那里听说,这也被认为是不好的做法,因为你必须跟踪扫描仪对象的状态。没错,有时可以而且应该以效率/简单的名义做出妥协。是的,有时我对自己说,效率是你最不想考虑的事情。有时你更关心的是如何让程序正常工作。如果你想看的话,我已经用完整的代码更新了主要帖子。请