java - 为什么我不能用 java 8 解决最后 2 位乘法的问题?
问题描述
我在java中需要这些问题的帮助确定2个给定数字相乘的最后2位数字。
输入:a, b 整数> 0
输出:p整数> 0
示例:对于 a = 10 和 b = 11 p = 10(因为乘法结果 = 110)这是我的工作:
`import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.*;
public class Main {
public static void main(String[] args) {
BufferedReader in;
try {
in = new BufferedReader(new FileReader(new File(args[0])));
String line = null;
int a = 0;
int b = 0;
int result = 0;
line = in.readLine();
a = Integer.parseInt(line.split(" ")[0]);
b = Integer.parseInt(line.split(" ")[1]);
result=a*b;
System.out.println(result);
}catch (Exception e) {
e.printStackTrace();
}
}
} `
解决方案
用户in.readLine()
读取行。并在文件读取时记住readLine()
每行一个语句。如果你打电话readLine()
两次,这意味着你正在阅读第二行。
这是我的实现..我假设您的输入文件中有两行并且总是只有整数
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.Scanner;
public class ReadInt {
public static void main(String[] args) {
BufferedReader in;
try {
in = new BufferedReader(new FileReader(new File(args[0])));
int a = 0;
int b = 0;
// suppose every time reads two lines
for (int i = 0; i < 2; i++) {
if (i % 2 == 0) {
a = Integer.parseInt(in.readLine().trim());
} else {
b = Integer.parseInt(in.readLine().trim());
}
}
System.out.println("first integer is " + a);
System.out.println("second integer is " + b);
System.out.println("total value is " + a * b);
System.out.printf("Last two digits are " + (a * b) % 100);
} catch (Exception e) {
e.printStackTrace();
}
}
}