Java 当用户要求重复程序时,如何获得总值?

Java 当用户要求重复程序时,如何获得总值?,java,oop,Java,Oop,我是一个绝对的初学者,我正在为一家网上商店写一个程序,我有三个部门,我从妇女部门开始。我询问用户是否要添加其他产品,如果是,则我尝试重复该程序并添加每次重复的值,只打印总数,但我的代码没有按照我需要的方式工作 public class WomenDep extends Shop{ int a,c,d,NoOfSizes=3,Counter; double b,Total; Prices P1 = new Prices(); double []arr = new d

我是一个绝对的初学者,我正在为一家网上商店写一个程序,我有三个部门,我从妇女部门开始。我询问用户是否要添加其他产品,如果是,则我尝试重复该程序并添加每次重复的值,只打印总数,但我的代码没有按照我需要的方式工作

public class WomenDep extends Shop{
    int a,c,d,NoOfSizes=3,Counter;
    double b,Total;
    Prices P1 = new Prices();
    double []arr = new double[NoOfSizes];
    @Override
     public void GetDet(){
        System.out.println("Pls Select Product No., \n(1)--> Tshirt\n(2)--> Short\n(3)--> Jeans\n"
                + "(4)--> Dress\n(5)--> Skirts");
        a = input.nextInt();
        System.out.println("Pls Select required Size No., \n(1)-->S\n(2)-->M\n(3)-->L");
        b = input.nextDouble();
        System.out.println("Enter Number of Pieces");
        c = input.nextInt();
        this.Pieces=c;
         System.out.println("Do you want to add another product to your cart?\n(1)-->Yes\n(2)-->No");
        d = input.nextInt();
    }

    @Override
     public double [] SearchArr(){
         if(this.a==1){
            this.ProductName = "Tshirt";
            for (int i = 0; i<NoOfSizes; ++i)
                arr[i]=P1.WTshirt[i];
            }
         if(this.a==2){
            this.ProductName = "Short";
         for (int i = 0; i<NoOfSizes; ++i)
                arr[i]=P1.WShort[i];
            }
         if(this.a==3){
            this.ProductName = "Jeans";
         for (int i = 0; i<NoOfSizes; ++i)
                arr[i]=P1.WJeans[i];
            }
         if(this.a==4){
            this.ProductName = "Dress";
         for (int i = 0; i<NoOfSizes; ++i)
                arr[i]=P1.WDress[i];
            }
         if(this.a==5){
            this.ProductName = "Skirts";
         for (int i = 0; i<NoOfSizes; ++i)
                arr[i]=P1.WSkirts[i];
         }
          return arr;
     }

    @Override
     public double CalPrice(){
         if(b==1){
                Price = arr [0];}
         if(b==2){
                Price = arr [1];}
         else if(b==3){
                Price = arr [2];}
         return Price;
     }

    @Override
     public void TotalPrice(){
         TPrice =Price * Pieces;
         System.out.println("this is total Price "+TPrice);
     }

    @Override
     public void Recal(){ 
            do{
            this.GetDet();
            this.SearchArr();
            this.CalPrice();
            this.TotalPrice();
            }while(d==1);
        }
     }
public class Prices extends Shop{
    public double [] WTshirt = {60.00,100.5,120};//S,M,L
    public double [] WShort = {50,110,130.5};//S,M,L
    public double [] WJeans = {150.99,180,200};//S,M,L
    public double [] WDress = {350,400,450.99};//S,M,L
    public double [] WSkirts = {350.5,499.99,450.5};//S,M,L
}
  public static void main(String[] args) {
     WomenDep a = new WomenDep();
     a.Recal();

    }
公共类WomenDep扩展商店{
int a,c,d,NoOfSizes=3,计数器;
双b,总计;
价格P1=新价格();
double[]arr=新的double[NoOfSizes];
@凌驾
public void GetDet(){
System.out.println(“请选择产品编号,\n(1)-->Tshirt\n(2)-->Short\n(3)-->Jeans\n”
+“(4)——>连衣裙\n(5)——>裙子”);
a=输入.nextInt();
System.out.println(“请选择所需尺寸编号,\n(1)-->S\n(2)-->M\n(3)-->L”);
b=输入。下一个双倍();
System.out.println(“输入件数”);
c=input.nextInt();
这个。碎片=c;
System.out.println(“是否要将其他产品添加到购物车?\n(1)-->是\n(2)-->否”);
d=input.nextInt();
}
@凌驾
公共双[]搜索arr(){
if(this.a==1){
this.ProductName=“Tshirt”;

对于(inti=0;i来说,计算价格的方法只适用一次

  • 更改对this.CalPrice()的调用以存储函数返回的值
  • 创建Price作为全局变量,以存储更新后的价格

    public void Recal(){ 
    do{
        this.GetDet();
        this.SearchArr();
        double Price = this.CalPrice();
        this.TotalPrice();
    }while(d==1);
    }
    
    public void TotalPrice(){
     TPrice = TPrice + this.Price * Pieces;
    System.out.println("this is total Price "+TPrice);
    }
    
  • 更改TotalPrice函数以更新价格

  • 全局创建TPrice变量,以获取最新更新的价格

    public void Recal(){ 
    do{
        this.GetDet();
        this.SearchArr();
        double Price = this.CalPrice();
        this.TotalPrice();
    }while(d==1);
    }
    
    public void TotalPrice(){
     TPrice = TPrice + this.Price * Pieces;
    System.out.println("this is total Price "+TPrice);
    }
    
  • 完整的代码,我已经删除了扩展类部分,请查看并检查问题是否已解决

    import java.util.Scanner;
    
    public class WomenDep{
    int a,c,d,NoOfSizes=3,Counter;
    double b,Total;
    Prices P1 = new Prices();
    double []arr = new double[NoOfSizes];
    private int Pieces;
    double Price;
    double TPrice;
    Scanner input = new Scanner(System.in);
    private String ProductName;
    
    public void GetDet(){
        System.out.println("Pls Select Product No., \n(1)--> Tshirt\n(2)--> Short\n(3)--> Jeans\n"
                + "(4)--> Dress\n(5)--> Skirts");
        a = input.nextInt();
        System.out.println("Pls Select required Size No., \n(1)-->S\n(2)-->M\n(3)-->L");
        b = input.nextDouble();
        System.out.println("Enter Number of Pieces");
        c = input.nextInt();
        this.Pieces=c;
        System.out.println("Do you want to add another product to your cart?\n(1)-->Yes\n(2)-->No");
        d = input.nextInt();
    }
    
    
    public double [] SearchArr(){
        if(this.a==1){
            this.ProductName = "Tshirt";
            for (int i = 0; i<NoOfSizes; ++i)
                arr[i]=P1.WTshirt[i];
        }
        if(this.a==2){
            this.ProductName = "Short";
            for (int i = 0; i<NoOfSizes; ++i)
                arr[i]=P1.WShort[i];
        }
        if(this.a==3){
            this.ProductName = "Jeans";
            for (int i = 0; i<NoOfSizes; ++i)
                arr[i]=P1.WJeans[i];
        }
        if(this.a==4){
            this.ProductName = "Dress";
            for (int i = 0; i<NoOfSizes; ++i)
                arr[i]=P1.WDress[i];
        }
        if(this.a==5){
            this.ProductName = "Skirts";
            for (int i = 0; i<NoOfSizes; ++i)
                arr[i]=P1.WSkirts[i];
        }
        return arr;
    }
    
    
    public double CalPrice(){
    
        if(b==1){
            Price = arr [0];}
        if(b==2){
            Price = arr [1];}
        else if(b==3){
            Price = arr [2];}
        return Price;
    }
    
    
    public void TotalPrice(){
         TPrice = TPrice + this.Price * Pieces;
        System.out.println("this is total Price "+TPrice);
    }
    
    
    public void Recal(){ 
        do{
            this.GetDet();
            this.SearchArr();
            double Price = this.CalPrice();
            this.TotalPrice();
        }while(d==1);
    }
     class Prices{
        public double [] WTshirt = {60.00,100.5,120};//S,M,L
        public double [] WShort = {50,110,130.5};//S,M,L
        public double [] WJeans = {150.99,180,200};//S,M,L
        public double [] WDress = {350,400,450.99};//S,M,L
        public double [] WSkirts = {350.5,499.99,450.5};//S,M,L
    }
    
    
    public static void main(String[] args) {
    WomenDep a = new WomenDep();
    a.Recal();
    
    }
    }
    
    import java.util.Scanner;
    公共阶级妇女{
    int a,c,d,NoOfSizes=3,计数器;
    双b,总计;
    价格P1=新价格();
    double[]arr=新的double[NoOfSizes];
    私人智力作品;
    双倍价格;
    双TPrice;
    扫描仪输入=新扫描仪(System.in);
    私有字符串产品名称;
    public void GetDet(){
    System.out.println(“请选择产品编号,\n(1)-->Tshirt\n(2)-->Short\n(3)-->Jeans\n”
    +“(4)——>连衣裙\n(5)——>裙子”);
    a=输入.nextInt();
    System.out.println(“请选择所需尺寸编号,\n(1)-->S\n(2)-->M\n(3)-->L”);
    b=输入。下一个双倍();
    System.out.println(“输入件数”);
    c=input.nextInt();
    这个。碎片=c;
    System.out.println(“是否要将其他产品添加到购物车?\n(1)-->是\n(2)-->否”);
    d=input.nextInt();
    }
    公共双[]搜索arr(){
    if(this.a==1){
    this.ProductName=“Tshirt”;
    
    对于(In i=0;请考虑命名约定。在java CAMELSE中非常方便。对于类和变量的下拉框都有UpCeCelEx箱。常量通常以大写字母写。1。选择一个您认为是解决问题的最佳答案。2。eside可将答案从灰显切换为填充。