Java,编写一个类Test04_1.java,实例化1个Phone类,自定义它的品牌和价格(注意使用set方法赋值),然后调用detail()方法进行输出
时间: 2024-05-12 19:20:37 浏览: 11
```
public class Test04_1 {
public static void main(String[] args) {
Phone phone = new Phone();
phone.setBrand("Apple");
phone.setPrice(7999);
phone.detail();
}
}
class Phone {
private String brand;
private double price;
public void setBrand(String brand) {
this.brand = brand;
}
public void setPrice(double price) {
this.price = price;
}
public void detail() {
System.out.println("品牌:" + brand);
System.out.println("价格:" + price);
}
}
```
相关问题
Java,编写一个类Test04_2.java,实例化2个Phone类,分别自定义它们的品牌和价格,然后进行打电话操作,输出品牌和价格
public class Test04_2 {
public static void main(String[] args) {
Phone phone1 = new Phone("Apple", 6999.0);
Phone phone2 = new Phone("Samsung", 4999.0);
phone1.call();
System.out.println("品牌:" + phone1.getBrand());
System.out.println("价格:" + phone1.getPrice());
phone2.call();
System.out.println("品牌:" + phone2.getBrand());
System.out.println("价格:" + phone2.getPrice());
}
}
class Phone {
private String brand;
private double price;
public Phone(String brand, double price) {
this.brand = brand;
this.price = price;
}
public void call() {
System.out.println("打电话中...");
}
public String getBrand() {
return brand;
}
public double getPrice() {
return price;
}
}
用java 编写一个类test06.java,编程计算1!+2!+.....+10!;
当然可以!下面是一个用Java编写的test06.java类,用于计算1!+2!+...+10!的结果:
```java
public class test06 {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 10; i++) {
sum += factorial(i);
}
System.out.println("1!+2!+...+10!的结果为:" + sum);
}
public static int factorial(int num) {
int result = 1;
for (int i = 1; i <= num; i++) {
result *= i;
}
return result;
}
}
```
这个程序中,我们定义了一个静态方法factorial,用于计算给定数字的阶乘。然后,在main方法中使用循环计算1!到10!的结果,并将它们相加得到最终结果。最后,将结果打印输出。