变量仅在第一次运行时增加?

问题描述 投票:-2回答:1

我希望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;
} 

ReadandWriteToFileprogram

//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));
java arrays variables main auto-increment
1个回答
0
投票

您应将变量设为静态,并将其初始化为0。静态变量由所有类实例共享,而不是实例变量。只需将其更改为:

protected static int propertyNo=0;
© www.soinside.com 2019 - 2024. All rights reserved.