Java 某一方法的定位
所以我有一个基本上是产品库存/商店/购物车的程序。用户输入一个产品编号,并将其添加到购物车中,基本上就是这样。问题是我的驱动程序包含了很大比例的功能代码。我希望下面的部分在另一节课上,但我不知道该怎么做。有什么帮助吗Java 某一方法的定位,java,Java,所以我有一个基本上是产品库存/商店/购物车的程序。用户输入一个产品编号,并将其添加到购物车中,基本上就是这样。问题是我的驱动程序包含了很大比例的功能代码。我希望下面的部分在另一节课上,但我不知道该怎么做。有什么帮助吗 if (entry == P0N) { //initial product number...must be as many as there are products System.out.println ("HOW MANY?"); int cart = Key
if (entry == P0N) { //initial product number...must be as many as there are products
System.out.println ("HOW MANY?");
int cart = Keyboard.readInt();
if (cart > P0Q) {
System.out.println ("EXCEEDS AVAILABLE QUANITITY!\nPLEASE TRY AGAIN!"); //will send user to the main menu
} else if (cart <= P0Q) {
P0C += cart;
P0Q -= cart;
MyCart.add ("Gloves\t\t\t"+cart);
}
}
if(entry==P0N){//初始产品编号…必须与产品数量相同
System.out.println(“多少?”);
int cart=Keyboard.readInt();
如果(购物车>P0Q){
System.out.println(“超出可用数量!\n请重试!”;//将用户发送到主菜单
}否则,如果(cart您不应该在这样一个简单的程序上使用键盘类,请从基本JavaAPI(ahem*Scanner*ahem)开始
除此之外,您的小“程序”既不复杂也不独立;我不明白您为什么需要将代码放在不同的类中,除了主类之外,您甚至不需要任何其他类。只需执行以下操作:
public class Inventory {
public static void main(String[] args) {
//declare your variables
if (entry == P0N) { //initial product number...must be as many as there are products
System.out.println ("HOW MANY?");
int cart = Keyboard.readInt();
if (cart > P0Q) {
System.out.println ("EXCEEDS AVAILABLE QUANITITY!\nPLEASE TRY AGAIN!"); //will send user to the main menu
} else if (cart <= P0Q) {
P0C += cart;
P0Q -= cart;
MyCart.add ("Gloves\t\t\t"+cart);
}
}
}
公共类目录{
公共静态void main(字符串[]args){
//声明变量
如果(条目==P0N){//初始产品编号…必须与产品数量相同
System.out.println(“多少?”);
int cart=Keyboard.readInt();
如果(购物车>P0Q){
System.out.println(“超出可用数量!\n请重试!”;//将用户发送到主菜单
}否则如果(cart抱歉,我的问题可能不清楚。我已经有一个运行的程序,它总共有7个类。这个类,我的驱动程序,比我在问题中发布的要复杂得多。如果可能的话,我发布的部分正是我想移动到另一个类的部分。你可以轻松地将它移动到另一个类。你学习过setter/g吗etter方法?通过执行public void myMethod{使用不同的setter/getter来使用已封装的变量,使此程序成为返回void的方法。