[英]Carry over variable value to new method
我是Java的新手,並且似乎無法使我的方法正常工作,而不必將變量重置為'null'或新的Object值。 我需要的是讓用戶輸入轉入下一個方法。
public static void addItem ()
{
Scanner console = new Scanner(System.in);
String desc, id, str="";
double price = 0, setUpPrice = 0, unitCost = 0, inventoryCost = 0;
int stock = 0, demand = 0;
Product product = new Product();
System.out.print("Please enter product description between 3 to 10 characters...: ");
desc = console.next();
desc = desc.toLowerCase();
product.setName(desc);
/*if (desc.length < 3 || desc.length > 10)
{
System.out.print("This Input is incorrect, Please try again.");
}*/
System.out.print("Please enter price in $ : ");
price = console.nextDouble();
System.out.print("Please enter set up price. $ : ");
setUpPrice = console.nextDouble();
System.out.print("Please enter unit- cost. $ : ");
unitCost = console.nextDouble();
System.out.print("Please enter the inventory cost. $ : ");
inventoryCost = console.nextDouble();
System.out.print("Please enter the amount in stock : ");
stock = console.nextInt();
System.out.print("Please enter the demand of the product : ");
demand = console.nextInt();
// need code here to repeat the fuction again if y is pressed.
// if n is pressed need to return to inventory management interface.
}
public static void prodData ()
{
Product product = new Product();
System.out.println("The information for the product is : ");
System.out.println("Product description : "+product.getName());
}
將您的產品變量聲明為全局變量,並按以下方式訪問
public static Product product;
public static void addItem () {
product = new Product();
Scanner console = new Scanner(System.in);
String desc, id, str="";
double price = 0, setUpPrice = 0, unitCost = 0, inventoryCost = 0;
int stock = 0, demand = 0;
System.out.print("Please enter product description between 3 to 10 characters...: ");
desc = console.next();
desc = desc.toLowerCase();
product.setName(desc);
/*if (desc.length < 3 || desc.length > 10)
{
System.out.print("This Input is incorrect, Please try again.");
}*/
System.out.print("Please enter price in $ : ");
price = console.nextDouble();
System.out.print("Please enter set up price. $ : ");
setUpPrice = console.nextDouble();
System.out.print("Please enter unit- cost. $ : ");
unitCost = console.nextDouble();
System.out.print("Please enter the inventory cost. $ : ");
inventoryCost = console.nextDouble();
System.out.print("Please enter the amount in stock : ");
stock = console.nextInt();
System.out.print("Please enter the demand of the product : ");
demand = console.nextInt();
// need code here to repeat the fuction again if y is pressed.
// if n is pressed need to return to inventory management interface.
}
public static void prodData () {
System.out.println("The information for the product is : ");
System.out.println("Product description : "+product.getName());
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.