java - 变量仅在第一次运行时增加?
问题描述
我希望 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;
推荐阅读
- python-3.x - 即使我在 Ubuntu 上安装了 Locust,“找不到 Locust 命令”也会返回
- python - Pyspark:仅从嵌套的 json 数据中读取某些字段
- oracle - 我们可以在合并语句中使用别名吗
- python - /update_order/ 重复键值处的 IntegrityError 违反了唯一约束“app_order_pkey”。详细信息:键 (id)=(54) 已存在
- c# - 在 EPPlus 4.5.3 中阅读形状和文本框
- python-3.x - python - 如何在python中验证给定的CHANNEL ID对于Telegram API是对还是错
- flutter - Flutter 网页风味
- python - 如何获取熊猫中重复行的列表?
- vba - 如何修改标题文本并保持自动编号 - VBA,word
- python - Django request.POST 返回一个空字典