【问题标题】:How to get system (BlueJ) to allocate unique number sequentially [closed]如何让系统(BlueJ)按顺序分配唯一编号[关闭]
【发布时间】:2011-05-18 14:41:30
【问题描述】:

/**文件类*/

public class File {
   // The File name.       
   private String name;
   // The date file created.     
   private String date ;
   // The type of file - audio, image, video, doc.     
   private String type;
   // The size of the file - 2MB, 2KB etc.     
   private String size; 


/**
 * Create a file 
 */

public File(String Filename)
{
name = Filename;
date = ("MM/dd/yyyy");
type = ();
size = ();
}


/**
 * Return the name of a File.
 */

public String getName()
{
return name;
}


/**
 * Update system number when called to the output terminal 
 */

public void Systemnumber ()
   name.increment();
   if(name.Filename() == 0) { // it jus rolled over !
      i.increment();
   }
   updateDisplay();
}


/**
 * Print File description to the output terminal 
 */

public void updateDisplay ()
public String s = String.format ("%02d", i); // gives you "001" 
for (int i = 001; i < 1000; i++) {String sequence = String.format("%02d", i); }
{      
return  (i + "", '$this.Name + " " + $this.date + " " + $this.type + " " +$this.size; }  


/**
 * This method is called everytime Increment the return value one, rolling over to zeor if the limit is reached 
 */

public void updateDisplay ()
{ displayString = name.
(
value = (value +1) % limit;
)
}

【问题讨论】:

标签: java tostring bluej


【解决方案1】:

看到你在使用BlueJ,我认为这是家庭作业,所以我不会这样回答这个问题,只是给一些指示。

您需要在 File 对象中存储一个计数器,但是,该计数器需要在对象的实例之间共享。此计数器可在构建时用于获取每个单独的 File 实例的编号,然后可以为相关实例存储该编号。


您在新更新中缺少的是序列号。您应该将它存储在一个静态变量中(以便它在 File 的所有实例之间共享),然后将其当前值分配给构造函数中的成员变量,然后再递增静态变量以进行下一次实例化。

【讨论】:

  • 谢谢Rich,它肯定是功课,你的指点已被记下,但我不明白,请你进一步建议我应该读什么,谢谢
  • @Ali 嗨,我现在不能详细回答,但大约 7 小时后可以回答。如果为时已晚,请告诉我...
  • 嗨 Rich 我使用 Luc 的代码在下面粘贴了一个修改后的代码,请告诉我。我当然可以等待你的建议。谢谢
  • public class File { // 文件名。私有字符串名称; // 创建的日期文件。私人时间戳日期; // 文件类型 - 音频、图像、视频、文档。私有字符串类型; // 文件大小 - 2MB, 2KB 等 private String size; /** * 创建一个文件 / public File(string Filename) {name = Filename;日期 = ("MM/dd/yyyy");类型 = 0;大小 = 0; } /* * 返回文件名*/ public string getName() {return name;} public String toString() {return $this.Name + " " + $this.date + " " + $this.type + " " +$this.size; } } }
  • 我已将您的代码添加到您的原始问题中(您可以根据需要多次编辑您的问题),但缺少的是序列号。
【解决方案2】:

您需要添加一个私有静态整数,您可以随意调用它。我会称之为numberOfFileInstances。在您的构造函数中,您需要将 numberOfFileInstances 加一。

这是我的例子:

public class File {

    private static int numberOfFileInstances = 0;

    public File() {
        File.numberOfFileInstances++;
    }

}

由于您使用 BlueJ,您将很容易看到,每次创建新文件对象时,numberOfFileInstance 都会增加 1。在 BlueJ 中初始化 2(或任何您希望大于 1 的数字)文件对象并双击该对象以调出检查器。单击“显示静态字段”按钮,您应该会看到私有 int numberOfFileInstance 以及您初始化的对象的数量。

【讨论】:

  • 澄清 numberOfFileInstances 是私有的原因是为了没有其他对象可以修改计数。
【解决方案3】:

如果您需要一个序列计数器,您可能需要考虑使用一个静态整数,为每个添加的文件递增。

【讨论】:

    【解决方案4】:

    我不确定我是否明白你想要什么,但听起来很简单:

    public String toString()  { 
        return this.Name + " " + this.date + " " + this.type + " " +this.size;
    } 
    

    【讨论】:

    • 如果他想打印多个文件,他需要了解如何执行序列号(在他的示例中为001)。
    • 谢谢卢克。请在下面查看我修改后的代码并提出建议,谢谢
    • public class File { // 文件名。私有字符串名称; // 创建的日期文件。私人时间戳日期; // 文件类型 - 音频、图像、视频、文档。私有字符串类型; // 文件大小 - 2MB, 2KB 等 private String size; /** * 创建一个文件 / public File(string Filename) {name = Filename;日期 = ("MM/dd/yyyy");类型 = 0;大小 = 0; } /* * 返回文件名*/ public string getName() {return name;} public String toString() {return $this.Name + " " + $this.date + " " + $this.type + " " +$this.size; } } }
    • 您应该编辑您的问题。代码很难阅读。
    • @Luc 我已经为 Ali 编辑了问题
    猜你喜欢
    • 1970-01-01
    • 2012-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-16
    • 2018-07-04
    • 2019-06-06
    • 1970-01-01
    相关资源
    最近更新 更多