12.15编写一个程序,实现从命令行参数输入两个字符串类型的数值,并计算输出两个数值的和

编写一个程序,实现从命令行参数输入两个字符串类型的数值,并计算输出两个数值的和。

package Text6;

import java.util.Scanner;

//编写一个程序,实现从命令行参数输入两个字符串类型的数值,并计算输出两个数值的和。
public class Zuoye1 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("输入两个整数:");
		String n = sc.nextLine();
		String n1 = sc.nextLine();
		
		int s = Integer.parseInt(n);
		int c = Integer.parseInt(n1);
		int sum = s+c;
		System.out.println(sum);
	}
}
	}
}
  • 4
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用 Java 语言实现程序,可以实现上述要求: ```java import java.io.*; import java.util.*; public class Exercise12_15 { public static void main(String[] args) throws IOException { // 创建文件对象 File file = new File("Exercise12_15.txt"); // 如果文件不存在,创建文件 if (!file.exists()) { file.createNewFile(); } // 生成 100 个随机整数 List<Integer> numbers = new ArrayList<>(); Random random = new Random(); for (int i = 0; i < 100; i++) { numbers.add(random.nextInt(1000) + 1); } // 将整数写入文件 try (PrintWriter output = new PrintWriter(file)) { for (int i = 0; i < numbers.size(); i++) { output.print(numbers.get(i) + " "); } } // 从文件中读取数据 List<Integer> sortedNumbers = new ArrayList<>(); try (Scanner input = new Scanner(file)) { while (input.hasNextInt()) { sortedNumbers.add(input.nextInt()); } } // 对整数列表进行排序 Collections.sort(sortedNumbers); // 输出排序后的结果 System.out.println(sortedNumbers); } } ``` 程序首先创建文件对象,并判断文件是否存在,如果不存在则创建文件。接着程序使用 `java.util.Random` 类生成 100 个随机整数,并将这些整数写入文件中。程序使用 `PrintWriter` 类实现文件写入操作。 接着程序从文件中读取数据,并将数据存储到一个整数列表中。程序使用 `Scanner` 类实现文件读取操作。然后程序对整数列表进行排序,使用 `java.util.Collections` 类中的 `sort()` 方法实现排序操作。最后程序输出排序后的结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值