Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- int multiFirst, multiSecond, c,aFirst, bFirst, numFirst=0, numSecond=0, denomFirst=0, denomSecond=0;
- boolean isIncorrect;
- Scanner scan = new Scanner(System.in);
- do {
- isIncorrect = false;
- System.out.println("Введите числитель первой дроби: ");
- try {
- numFirst = Integer.parseInt(scan.nextLine());
- }
- catch (Exception e) {
- System.err.println("Неверный ввод данных!");
- isIncorrect = true;
- }
- } while (isIncorrect);
- do {
- isIncorrect = false;
- System.out.println("Введите знаменатель первой дроби: ");
- try {
- denomFirst = Integer.parseInt(scan.nextLine());
- }
- catch (Exception e) {
- System.err.println("Неверный ввод данных!");
- isIncorrect = true;
- }
- } while (isIncorrect);
- do {
- isIncorrect = false;
- System.out.println("Введите числитель второй дроби: ");
- try {
- numSecond = Integer.parseInt(scan.nextLine());
- }
- catch (Exception e) {
- System.err.println("Неверный ввод данных!");
- isIncorrect = true;
- }
- } while (isIncorrect);
- do {
- isIncorrect = false;
- System.out.println("Введите знаменатель второй дроби: ");
- try {
- denomSecond = Integer.parseInt(scan.nextLine());
- }
- catch (Exception e) {
- System.err.println("Неверный ввод данных!");
- isIncorrect = true;
- }
- } while (isIncorrect);
- scan.close();
- multiFirst = numFirst * numSecond;
- multiSecond = denomFirst * denomSecond;
- if(multiSecond == 0){
- System.out.println("Нуль - знаменатель!");
- }
- if (multiFirst == 0) {
- System.out.println(multiFirst + "/" + multiSecond + "=0");
- }
- aFirst = multiFirst;
- bFirst = multiSecond;
- if (multiSecond > multiFirst) {
- c = multiFirst;
- multiFirst = multiSecond;
- multiSecond = c;
- }
- while (multiFirst % multiSecond != 0) {
- c = multiSecond;
- multiSecond = multiFirst % multiSecond;
- multiFirst = c;
- }
- System.out.print(aFirst + "/" + bFirst + "=" + aFirst / multiSecond);
- if (bFirst / multiSecond !=1) {
- System.out.println("/" + bFirst / multiSecond);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement