[英]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.