java - 变量仅在第一次运行时递增?

标签 java arrays variables program-entry-point auto-increment

我希望每次运行菜单时 propertyNo 都会增加,并带有添加属性的选项。目前,它会在单次运行中为每个属性递增,但下一次运行它会恢复并再次启动计数器。我最终得到了具有相同 propertyNo 的各种属性,我试图创建一个独特的字段。

任何帮助将不胜感激。

属性

//instance variables
private int propertyNo;
String street;
String town;
String county;
String propertyType;
int noBeds;
double price;
private static int nextPropNum; 
/Default constructor
public Property()
{
    street = "";
    town = "";
    county = "";
    propertyType = "";
    noBeds = 0;
    price = 0.0;
}

public Property (String s, String t, String c, String type, int beds, double p)

{
    propertyNo = nextPropNum;
    nextPropNum++;
    street = s;
    town = t;
    county = c;
    propertyType = type;
    noBeds = beds;
    price = p;
} 

读取和写入文件程序

//variables
  int propertyNo;
  String s;
  String t;
  String c;
  String type;
  String selection = "";
  int beds;
  double p;
  int option = 0;
  char ans;
  boolean proFound = false;
  try
  {
     System.out.println("1. Add a Property(s) to File");
     System.out.println("2. View all Properties on File and display total number");
     System.out.println("3. View Properties by Town");
     System.out.println("4. View Properties by Type");
     System.out.println("5. View Properties within a Maximum price");
     System.out.println("0. Exit");
     System.out.print("Please select an option:");
     option = keyboardIn.nextInt();
     keyboardIn.nextLine();
        if(option < 0 || option > 5)
        {
           System.out.println("Please select a valid menu option");
        }
  }      
  catch(InputMismatchException e)
  {
     System.out.print("Menu option does not exist - please enter a menu value between 0 - 5. ");
  }
  switch(option)
  {
     case 1:
     try
     {
     do
     { 
        System.out.print("Enter the Street name: ");
        s = keyboardIn.nextLine();
        System.out.print("Enter the Town name: ");
        t = keyboardIn.nextLine();
        System.out.print("Enter the County: ");
        c = keyboardIn.nextLine();
        System.out.print("Enter a property type. Flat, Bungalow, Detached, Semi-Detached. "); 
        selection = keyboardIn.nextLine();
           if("flat".equals(selection) || "Flat".equals(selection) || "bungalow".equals(selection) || "Bungalow".equals(selection) || "semi-detached".equals(selection) || "Semi-Detached".equals(selection) || "Detached".equals(selection) || "detached".equals(selection))
           {
              type = selection;
           }
           else
           {
              System.out.print("Error, you did not select a valid property type.");
              break;
           }
        System.out.print("Enter the Number of Bedrooms: ");
        beds = keyboardIn.nextInt();
           if(beds <= 0)
           {
              System.out.print("No of Bed can not be 0 or a negative value. Please restart ");
              break;
           }

        System.out.print("Enter the price of the property: ");
        p = keyboardIn.nextDouble();
           if(p <= 0)
           {
              System.out.print("Price can not be 0 or a negative value. Please restart ");
              break;
           }

        proplist.add(new Property(s, t, c, type, beds, p));

最佳答案

您应该将变量设为静态并用 0 对其进行初始化。 与实例变量相反,静态变量由所有类实例共享。

只需将其更改为:

protected static int propertyNo=0;

并访问它:

Main.propertyNo += 1;

关于java - 变量仅在第一次运行时递增?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61987502/

相关文章:

java - 如何在另一个函数中定义的函数中使用变量?

python - 在 Python 请求中使用 JSON 变量

java - 在 JPA 中使用带有串联 id 的字符串列值作为外键

Java Linux 从 PrinterJob 打印空白页

c - 在哪里释放C代码

java - 如何从某个位置开始迭代二维数组?

javascript - 将数组转换为字符串时允许逗号跟随

c# - 仅仅声明一个简单类型的变量会分配内存吗?

java - 从一个jsp获取值到另一个jsp

java - 我怎样才能让这个方法在我的循环中更新GUI?