我是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());
}
答案 0 :(得分:0)
将您的产品var声明为全局并按如下方式访问
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());
}