关于检查非数字数字的java程序

时间:2015-02-19 02:54:02

标签: java while-loop numbers numeric

基本上,如果我把非数字编号,它应该显示,因为我把非数字编号再次尝试使用数字编号。我知道我应该使用isDigit,但我不知道该放在哪里或如何正确使用它。 当我说非数字编号时,它可以是英文字母或除整数之外的任何其他内容。

public static void main(String[] args)
{    
    int again = 1;
    while (again == 1)                          
    {

        Scanner scan = new Scanner( System.in );

        double average = 0;
        int total = 0;

        System.out.println( "Enter your digits to calculate average and total digits. ");
        String score = scan.nextLine();


        total = Summation(score);
        average = (double)total / score.length();

        System.out.print ("TOTAL : " + total + "\n");
        System.out.print ("AVERAGE : " + average + "\n");

       System.out.println( "Play again? Press 1 to play ");
       again = scan.nextInt( );
    }
    System.out.println( "Bye~~ ");
}

private static int Summation(String score)
{   

    char[] charArr = score.toCharArray();
    int[] intArr = new int[score.length()];
    int total = 0;
    int j = 0;

    for (int i = charArr.length - 1; i >= 0; i--)
    {
        System.out.println(charArr[i]); // Display the digits in a reversed order
    }

    for (int i = 0; i < charArr.length; i++)
    {
        // Add every individual digit to find the sum
        intArr[i] = Character.getNumericValue(charArr[i]);
        // Converting to intArray from charArray
        total = intArr[i] + total;
    }

    return total; // Return the total number
}

2 个答案:

答案 0 :(得分:1)

您可以使用正则表达式来检查String是否只是数字。像

这样的东西
private static boolean isNumber(String score) {
    return score.matches("^\\d+$");
}

测试String以数字开头和结尾。

或者,您可以通过测试String包含带有\D模式的非数字来编写相同的函数,例如

private static boolean isNumber(String score) {
    return !score.matches("\\D");
}

这些和其他字符类记录在Pattern Javadoc

答案 1 :(得分:0)

由于你的输入之间没有空格,你可以在try和in catch中使用Integer.parseint,你可以显示消息&#34;再次使用数字输入&#34;然后继续捕获循环。 扫描仪有一点问题,请参阅代码。

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
    public static Scanner scan = new Scanner( System.in ); //problem fixed
    public static void main (String[] args) throws java.lang.Exception
    {
         int again=1;
    while (again ==1)                           
    {



        double average = 0;
        int total = 0;

        System.out.println( "Enter your digits to calculate average and total digits. ");
        String score = scan.nextLine();
        System.out.println(score);
        try
        {
            Integer.parseInt(score);
        }
        catch(NumberFormatException e)
        {
            System.out.println("Try with numeric number");

            continue;
        }
        total = Summation(score);
        average = (double)total/score.length();

        System.out.print ("TOTAL : " + total +"\n");
        System.out.print ("AVERAGE : " + average + "\n");

       System.out.println( "Play again? Press 1 to play ");
       again = scan.nextInt( );
    }
    System.out.println( "Bye~~ ");
}   
private static int Summation(String score)
{
 char[] charArr = score.toCharArray();
    int[] intArr= new int[score.length()];
    int total=0;
    int j=0;




    for (int i=charArr.length-1; i>=0; i--)
    {
        System.out.println(charArr[i]);                                     // Display the digits in a reversed order
    }

    for (int i=0; i<charArr.length;i++)
    {                                                                       // Add every individual digit to find the sum
        intArr[i] = Character.getNumericValue(charArr[i]);                  // Converting to intArray from charArray
        total = intArr[i] + total;
    }




    return total;                                                           // Return the total number
}

}