【问题标题】:Instance variable reset实例变量重置
【发布时间】:2013-10-17 00:33:14
【问题描述】:

在方法 checker() 和 runFirst 之间,变量“running”和“productChoice”被重置,我不知道为什么。我尝试过如何声明 RetailSalesProgram,但似乎没有任何效果。我只需要知道如何使变量不重置。

import javax.swing.JOptionPane;
public class RetailSalesProgram
{
private String customerName = "Default";
private int customerChoice;
private boolean running;
private double productChoice, a, b, c ,d ,e, total;
   // sets values
   public RetailSalesProgram()
   {
      running = true;
      a= 2.98;
      b= 4.50;
      c= 9.98;
      d= 3.15;
      e= 2.29;
   }
   / takes the methods and puts them into main
   public void runFirst()
   {
      RetailSalesProgram rsp = new RetailSalesProgram();
      rsp.greet();
      do
      {
      rsp.shop();
      rsp.checker();
      rsp.totaling();
      System.out.println(running);
      System.out.println(total);
      }
      while (running == true);


   }

   //the initial greeting before they shop
   private void greet()
   {
      customerName = JOptionPane.showInputDialog(null, " Welcome to my store!\n Please enter your name");
      JOptionPane.showMessageDialog(null, "Hello " + customerName + "!");
   }
   //this is the menu for the shop
   private void shop()
   {
      customerChoice = Integer.parseInt(JOptionPane.showInputDialog(null, " 1. Can of beans  $2.98\n2. Calculator  $4.50\n3. Yoga mat  $9.98\n4. Bottle of Gatorade  $$3.15\n5. Birthday card  $2.29\n6. Exit program"));
      while (customerChoice < 1 || customerChoice > 6)
      {
         JOptionPane.showMessageDialog(null, "That was not a valid entry");
         customerChoice = Integer.parseInt(JOptionPane.showInputDialog(null, " 1. Can of beans  $2.98\n2. Calculator  $4.50\n3. Yoga mat  $9.98\n4. Bottle of Gatorade  $$3.15\n5. Birthday card  $2.29\n6. Exit program"));
      }
      System.out.println(customerChoice);
   }
   // This is where im having problems. It is suppose to change the instance variables.
   private void checker()
   {
   switch (customerChoice)
      {
      case 1: 
               productChoice = a;
               break;
      case 2: 
               productChoice = b;
               break;
      case 3: 
               productChoice = c;
               break;
      case 4: 
               productChoice = d;
               break;
      case 5: 
               productChoice = e;
               break;
      case 6: 
               running = false;
               break;
      } 
                     System.out.println(total);
                     System.out.println(running);
   }
   // This just takes a total of the product.
   private void totaling()
   {
      productChoice += total;
   }


//////////////////////////////////////////////////////////////////////////
// will run everything for runFirst()   
   public static void main(String[]args)
   {
   RetailSalesProgram rsp = new RetailSalesProgram();
   rsp.runFirst();
   }
}

【问题讨论】:

    标签: java variables instance reset


    【解决方案1】:

    您有两个 rsp 变量。一个是字段,一个是局部变量。

    private RetailSalesProgram rsp; // one variable
       // sets values
       public RetailSalesProgram()
       {
          RetailSalesProgram rsp = new RetailSalesProgram(); // unrelated variable.
    

    您很可能根本不需要此字段,因为您已经有一个 RetailSalesProgram

    【讨论】:

    • 哎呀,为了让它工作而到处乱吃剩下的东西。我摆脱了这两个并将它放在我最初拥有它的地方。在 runFirst 方法的最开始。它仍然不会保存变量。我认为它的原因是我在 runFirst 和主要中提出和反对。不过,我不知道另一种方法。
    • @user2888524 我的意思是你似乎不需要rsp我会删除它们。
    • 您在代码中指出的 2 我已经删除了,但它仍然不起作用。因为我在 runFirst 中创建了一个 rsp,但我必须在 main 中也导致它的静态。你知道我可以用什么方法来引用同一个对象吗?我想他们介意休息一下,因为它宣布了两次?
    • @user2888524 你能从你的代码中完全删除rsprsp.吗?
    • 所以我只保留了 RetailSalesProgram rsp = new RetailSalesProgram();在 main 方法中,因为它是静态的。其余的我摆脱了rsp。并没有为他们制作新物品。它起作用了,它们现在指的是同一个对象。感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 2014-10-06
    • 1970-01-01
    • 2019-02-01
    • 2016-05-08
    • 1970-01-01
    • 2014-09-21
    • 1970-01-01
    • 2014-09-16
    相关资源
    最近更新 更多