我是 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] 删除。
我来说两句