在数组中添加对象并将它们保存在txt文件中

拉克萨里

我是 Java 新手,我想我想尝试一下。我正在制作一个程序,它更像是从 2007 年到 2017 年 iphone 的时间表,每年都有 iphone 名称、内存大小、描述和利润。我能够显示所有这些信息,但我现在希望用户能够输入新事件,例如新 iPhone,内存大小和利润。到目前为止,所有信息都像这样在控制台中打印:

Name:Iphone (2007)
Year:2007
Minimum Memeory Size avalible:4
Maximum MemorySize avaliable:16
iphone Details:It is the first generation of iPhone that was
 announced on January 9, 2007
Profit made in (billions $):2.0

但现在我希望用户输入一个新手机,例如

enter the iphone year: 
enter the name of the iphone: 
enter minimum memory size: 

这是我的第一堂课的样子:

import java.io.*;
public class iphoneDescriptions {

   String nameOfPhone;
   int year;
   int MinMemorySize;
   int MaxMemorySize;
   String details;
   double profit;
   String Creator;
  //String iphonemaker;


   public iphoneDescriptions(String nameOfPhone) {
      this.nameOfPhone = nameOfPhone;
   }


  // public void iphoneDescriptions(String string) {
       //this.nameOfPhone = nameOfPhone;
//}


public void iphoneYear(int iphoneYear) {
      year = iphoneYear;
   }


   public void MaxMemorySize(int iphoneMaxMemorySize) {
       MaxMemorySize = iphoneMaxMemorySize;
       }
   public void MinMemorySize(int iphoneMinMemorySize) {
       MinMemorySize = iphoneMinMemorySize;
       }


   public void iphoneDetails(String iphoneDetails) {
       details = iphoneDetails;
   }

 public void Creator (String iphonemaker) {
      Creator = iphonemaker;
 }


   public void ProfitMade(double ProfitMade) {
      profit = ProfitMade;
   }





   /* Print the Employee details */
   public void printiphoneDescriptions() {
      System.out.println("Name:"+ nameOfPhone );
      System.out.println("Year:" + year );
      System.out.println("Minimum Memeory Size avalible:" + MinMemorySize);
      System.out.println("Maximum MemorySize avaliable:" + MaxMemorySize );
      System.out.println("iphone Details:" + details);
      System.out.println("Profit made in (billions $):" + profit);
    // System.out.println("Name of the creator:" + Creator);

   }

public void setIphone(String nameOfPhone) {
     this.nameOfPhone = nameOfPhone;
}

public String getIphone() {
    return nameOfPhone;

}
public void setCreator (String iphonemaker) {
     this.Creator = iphonemaker;
}

public String getCreator() {
    return Creator;

}


public void setiphoneDetails(String  nameOfPhone) {
     this.nameOfPhone = nameOfPhone;
}

public String getiphoneDetails() {
    return nameOfPhone;

}

//public void setCreator1 (String  iphonemaker) {
     //this.iphonemaker = iphonemaker;
//}

//public String getCreator1() {
    //return Creator;

}

和我的第二堂课:

import java.io.*;
public class Iphone {

   public static void main(String args[]) {
      /* Create two objects using constructor */
      iphoneDescriptions Iphone1 = new iphoneDescriptions("Iphone (2007)");

Iphone1.iphoneYear(2007);
      Iphone1.MinMemorySize(4);    
      Iphone1.MaxMemorySize(16);
      Iphone1.iphoneDetails("It is the first generation of iPhone that was\r\n announced on January 9, 2007");
      Iphone1.ProfitMade(2.0);
      Iphone1.printiphoneDescriptions();
}
}
冰人斯坦

用户输入所需信息的一种简单方法是使用 Scanner 类。使用以下内容实例化扫描仪对象:

Scanner kb = new Scanner(System.in);

然后获取您需要的信息。

System.out.println("enter the iphone year");
int year = kb.nextInt();
kb.nextLine(); // this is to consume the newline left from the previous call to nextInt();
System.out.println("enter the name of the phone");
String phonename = kb.nextLine();
System.out.println("enter minimum memory size");
String memory = kb.nextLine();

然后只需使用您的方法使用此信息实例化新的 Iphone。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何从文件中获取值并将它们批量保存在数组中?

如何将值存储在数组中并将它们保存在文件中

在数组中查找重复值并将它们保存在单独的数组中

从文件中读取对象并将它们添加到数组中

如何使用python从文件夹中的pdf中提取文本并将它们保存在数据框中?

在数组中添加对象的键值对

从 var_dump 中提取值并将它们保存在数据库中

如何以 JSON 格式从多个对象中获取值并将它们存储在数组中?

计算对象属性的总和并将它们存储在数组中

从文件夹中获取特定文件并将它们保存在列表中

获取按钮并将它们存储在数组中

如何计算组中的记录数并将它们保存在 csv 文件中?

无法使用 python 替换 csv 中的字符并将它们保存在新文件中

C#:在数组中查找具有相同 id 的对象并将它们的数量值相加。将这些添加到新对象

如何停止 journalctl 显示审计日志并将它们保存在文件中?

如何打开多个 Json 文件并将它们保存在多个变量中

加载图像并将它们保存在项目的文件夹中

访问列表并将它们保存到文件中

如何在数组中添加对象成员?

如何在数组中添加对象

遍历NSMutableArray以在数组中添加对象

在React中在数组内添加对象的正确方法

直接在数组中添加对象

将 csv 文件中的值分组并将它们保存在另一个 csv 文件中

在txt文件中查找单词并将它们保存到一个新文件java

从目录(在共享路径上)获取所有文件,并将它们保存在wordpress uploads文件夹中

将对象的数组列表保存在txt文件中

在数组中查找重复值并将它们打印到android中的列表视图

在多个大图像上叠加一个小图像并将它们保存在不同的文件夹中