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

Java 仅检查正整数。没有负整数,没有非数字字符串,java,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;

我必须编写一个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);
    }
}
import java.util.Scanner;
GCD级{
公共静态void main(字符串[]args){
扫描仪sc=新的扫描仪(System.in);
整数a,b,m,n,余数;
System.out.print(“输入正整数:”);
而(!sc.hasnetint()){
System.out.print(“请输入一个正整数:”);
sc.next();
}
a=sc.nextInt();
while(a
/*提示a*/
字符串a=sc.next();
/*提示b*/
字符串b=sc.next();
如果(有效(a)和有效(b)){
int ia=Integer.parseInt(a);
int ia=整数。parseInt(b);
/*计算等等*/
}
布尔值有效(字符串数){
试一试{
int i=Integer.parseInt(num);
if(i<0){
返回false;
}
}捕获(数字格式){
返回false;
}
返回true;
}
试试这个:

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

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

在第一次调用next()时,您使用nextInt()。如果您第一次输入一个负整数,则在使用nextInt()时,您将转到下一个。因此,如果用户输入的字符串不是数字,则会出现异常,因为扫描仪无法获取键或其他内容的值。更聪明的方法是捕获异常并将其无限期地使用,如下所示:

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

此代码将一直运行,直到用户输入一个正数。如果用户输入的不是数字,则异常将出现并被忽略,如果该数字不是正数(在本例中大于-1),则while将继续while不会中断。

即使我不尝试,它也应该可以工作。我只有两条建议,因为您在编码方面看起来很新: -当你编写代码时,试着使用函数。通常,你不应该复制/粘贴。 -试着给你的变量加上全名,特别是如果你在论坛上分享你的代码,人们会更容易理解你做了什么,并帮助你:)


顺便说一句,零既不是负数,也不是正数。请参见:[使用
readInt
方法。[1]:为什么使用字符串?!扫描仪有readInt()方法。因此,它会自行测试输入是否正确,如果大于0,您只需查找其值。@Basti>当您以
字符串
形式获取输入时,您可以使用名为正则表达式的强大工具根据各种任意条件检查它,而当您尝试使用
readInt()时
您应该确定很多事情。在读取简单正值的情况下,您可以使用此方法,在抛出时捕获其抛出的异常(inputmissmatch)
while(true)
   System.out.print("Please enter a positive Number: ");
   try{
      a = sc.nextInt();
      if(a>-1){
         break;
      }
   }catch(Exception ignore){
   }
}
     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);
     }
  }