Advertisement
Guest User

Untitled

a guest
Mar 17th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1.  
  2. import java.util.Scanner;
  3.  
  4. public class App {
  5.  
  6. public static void main(String[] args){
  7. App app = new App();
  8. app.start();
  9. }
  10.  
  11. public void start(){
  12. Scanner sc = new Scanner(System.in);
  13. System.out.println("Здравствуйте! Пожалуйста, введите два числа через пробел или "
  14. + "напишите \"выход\" для выхода из программы.");
  15. String str;
  16. while(true){
  17. str = sc.nextLine();
  18. switch(str){
  19. case"выход":{
  20. System.out.println("До свидания!");
  21. System.exit(0);
  22. }
  23. default:{
  24. try{
  25. String[] strs = str.split(" ");
  26. if(strs.length!=2) throw new Exception();
  27. int i1 = Integer.parseInt(strs[0]);
  28. int i2 = Integer.parseInt(strs[1]);
  29. System.out.println("НОД этих чисел равен " +calculateGCD(i1, i2));
  30.  
  31. }
  32. catch(Exception e){
  33. System.out.println("Пожалуйста, введите два целых положительных числа через пробел."
  34. + "Пример:25 17");
  35. continue;
  36. }
  37. }
  38. }
  39. }
  40. }
  41.  
  42. public static int calculateGCD(int i1, int i2) throws Exception{
  43. if(i1<=0|i2<=0) throw new Exception();
  44. while(i1!=i2){
  45. if(i1>i2){
  46. i1 = i1 - i2;
  47. }
  48. else if(i2>i1){
  49. i2 = i2 - i1;
  50. }
  51. }
  52. return i1;
  53. }
  54.  
  55.  
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement