我希望每次运行带有添加属性的选项的菜单时,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;
}


ReadandWriteToFile程序

//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/

10-13 23:49