import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int sum = 0; Express[] express = new Express[n]; for (int i = 0; i < n; i++) { String name = scanner.next(); if (name == "SL") { express[i] = new SLExpress(scanner.nextInt()); sum += express[i].getTotal(); System.out.println(sum); } else if(name == "DD") { express[i] = new DDExpress(scanner.nextInt()); sum += express[i].getTotal(); } } System.out.print(sum); } } abstract class Express { private int weight; Express(int weight) { this.weight = weight; } public int getWeight() { return this.weight; } public abstract int getTotal(); } class SLExpress extends Express { SLExpress(int weight) { super(weight); } public int getTotal() { int total = 0; if(super.getWeight() <= 1) total = 12; else total = 12 + (super.getWeight() - 1)*2; return total; } } class DDExpress extends Express { DDExpress(int weight) { super(weight); } public int getTotal() { int total = 0; if(super.getWeight() <= 1) total = 5; else total = 5 + (super.getWeight() - 1)*1; return total; } }
时间: 2024-04-27 16:19:51 浏览: 18
这段代码实现了一个简单的快递计价程序,其中用到了抽象类和多态的概念。程序中定义了一个Express抽象类,其中包含一个weight属性表示快递重量和一个getTotal()抽象方法用于计算运费。SLExpress和DDExpress类都继承自Express类,分别实现了不同的计价标准。
在main方法中,首先读入快递数量n,然后用一个Express类型的数组来保存每个快递。在循环中,先读入快递公司名称name和重量weight,根据不同的快递公司名称创建不同的快递对象并将其保存到数组中。然后调用getTotal()方法计算运费并累加到sum变量中。最后输出sum即为所有快递的总运费。
需要注意的是,在比较字符串时应该使用equals()方法而不是==运算符。因为==运算符比较的是对象的引用是否相等,而equals()方法比较的是对象的值是否相等。因此,应该将name == "SL"和name == "DD"改为name.equals("SL")和name.equals("DD")。
相关问题
import java.util.Scanner; public class Test1 { 晋涛 public static void main(String[] args) { Sy
import java.util.Scanner;
public class Test1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入您的姓名:");
String name = scanner.nextLine();
.out.println("您好," + name + "!");
}
}
这段代码是一个简单的Java程序,它使用了Java标准库中的Scanner类来实现用户输入和输出。首先,我们通过创建Scanner对象来获取用户的输入。然后,使用nextLine()方法读取用户输入的姓名,并将其存储在name变量中。最后,通过System.out.println()方法将问候语和用户的姓名输出到控制台。
import java.io.*; import java.util.Arrays; import java.util.Scanner; public class FileTest { public static void main(String[] args) throws IOException { // 接收给定的一行字符串 Scanner scanner = new Scanner(System.in); String line = scanner.nextLine();
// 创建文件对象,并检查文件是否存在 File file = new File(line); if (!file.exists()) { System.out.println("文件不存在!"); return; } // 读取文件内容 BufferedReader reader = new BufferedReader(new FileReader(file)); String content = ""; String temp; while ((temp = reader.readLine()) != null) { content += temp + "\n"; } // 关闭文件读取器 reader.close(); // 输出文件内容 System.out.println("文件内容:\n" + content); } }
这段代码是一个读取文件并输出文件内容的Java程序。首先,通过Scanner接收用户输入的文件路径,然后判断文件是否存在。如果文件不存在,则输出提示信息并结束程序。如果文件存在,则创建文件读取器并读取文件内容,将读取到的内容存储在一个字符串变量中。最后,关闭文件读取器并输出文件内容。
相关推荐
![text/x-java](https://img-home.csdnimg.cn/images/20210720083646.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)