简体   繁体   中英

why i can not solve the problem of last 2 digits multipllicatio with java 8?

i need help for these problem in java Determine the last 2 digits of the multiplication of 2 given numbers.

Input: a, b integers> 0

Output: p integer> 0

Example: For a = 10 and b = 11 p = 10 (because the multiplication result = 110) this is my work:

`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();
     }
 }

} `

user in.readLine() to read line. and remember one readLine() statement for per line, when file reading. if you call readLine() twice that means you are reading the 2nd line.

Here is my implementation .. I assumed there are two lines in your input file and always there are only integers

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();
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM