只检查正整数。没有负整数,没有非数字字符串

时间:2014-04-28 07:34:40

标签: java

我必须编写一个java程序来计算两个正整数的最大公约数。程序必须仅检查正整数。我的问题是当我输入一个负整数然后输入一个非数字字符串时,我的程序停止运行。贝娄是我的代码:

import java.util.Scanner;
class GCD {
    public static void main (String[] args){
        Scanner sc = new Scanner(System.in);
        int a, b, m, n, remainder;
        System.out.print("Enter a positive integer: ");
        while (!sc.hasNextInt()){
            System.out.print("Please enter a positive integer: ");
            sc.next();
        }
        a = sc.nextInt();
        while (a <= 0){
            System.out.print("Please enter a positive integer: ");
            a = sc.nextInt();
        }


        System.out.print("Enter another positive integer: ");
        while (!sc.hasNextInt()){
            System.out.print("Please enter a positive integer: ");
            sc.next();
        }
        b = sc.nextInt();
        while (b <=0){
            System.out.print("Please enter a positive integer: ");
            b = sc.nextInt();
        }

        m = a;
        n = b;
        while (n != 0){
            remainder = m%n;
            m = n;
            n = remainder;
        }
        System.out.println("The GCD of " +a+ " and " +b+ " is " +m);
    }
}

4 个答案:

答案 0 :(得分:1)

试试这个:

import java.util.Scanner;
public class A {
    public static void main (String[] args){
        int a, b, m, n, remainder;
        a = validInput();
        b = validInput();
        m = a;
        n = b;
        while (n != 0){
            remainder = m%n;
            m = n;
            n = remainder;
        }
        System.out.println("The GCD of " +a+ " and " +b+ " is " +m);
    }

    static int validInput() {
        Scanner sc = new Scanner(System.in);
        while(true){
            System.out.print("Please enter a positive integer: ");
            String tmp = sc.next();
            if (tmp.matches("^\\d+$")) {
                return Integer.parseInt(tmp);
            }
        }
    }
}

我建议你让你的程序更加模块化,因为你可以在这样一个简单的程序中看到它的好处。

答案 1 :(得分:0)

/* prompt a */
String a = sc.next();

/* prompt b */
String b = sc.next();

if (isValid(a) && isValid(b)) {
    int ia = Integer.parseInt(a);
    int ia = Integer.parseInt(b);

    /* calculations and such */
}

boolean isValid(String num) {
    try {
        int i = Integer.parseInt(num);
        if (i < 0) {
            return false;
        }
    } catch (NumberFormatException e) {
        return false;
    }
    return true;
}

答案 2 :(得分:0)

在你第一次调用next()时,但是在你的第二次调用nextInt()时。如果你第一次输入负整数,你将使用nextInt()进入下一个。因此,如果用户输入的字符串不是数字,则会出现异常,因为扫描程序无法获取键或其他内容的值。更聪明的方法是捕获异常并将其用于无穷无尽的时间:

while(true)
   System.out.print("Please enter a positive Number: ");
   try{
      a = sc.nextInt();
      if(a>-1){
         break;
      }
   }catch(Exception ignore){
   }
}

此代码将一直运行,直到用户输入正数。如果他输入的不是数字,那么异常就会被忽略,而while会继续,如果数字不是正数(在这种情况下大于-1),while将不会中断。

答案 3 :(得分:0)

即使我不尝试,它也会工作。当你在编码中看起来很新时,我只有2个建议: - 当你编写代码时,尝试使用函数。通常,您不应该复制/粘贴。 - 尝试将全名设为您的变量,特别是如果您在论坛上分享您的代码,那么人们更容易理解您所做的事情并帮助您:)

     import java.util.regex.Pattern;
     import java.util.Scanner;
     class GCD {
        public static void main (String[] args){
          Scanner sc = new Scanner(System.in);
          int a, b, m, n, remainder;
          a=askInt();
          b=askInt();
          m = a;
          n = b;
          while (n != 0){
          remainder = m%n;
          m = n;
          n = remainder;
          }
          System.out.println("The GCD of " +a+ " and " +b+ " is " +m);
       }

     private int askInt(){
          System.out.print("Enter a positive integer: ");
          String tampon = sc.nextLine();
          while(!Pattern.matches("\p{Digit}",tampon)){
              System.out.print("Please enter a positive integer: ");
              String tampon = sc.nextLine();
          }
          return Integer.valueOf(tampon);
     }
  }