输入负值后需要再次提示

时间:2012-10-20 02:56:42

标签: java validation java.util.scanner

我正在尝试构建一个新代码,现在问题是在用户输入负值后,这将打印一些内容并再次提示输入值。在第69行后我应该添加什么?

这是我的代码:

import java.util.Scanner;

public class bchimedochir_Math
{

    public static void main(String args[])
    {
        Scanner input = new Scanner(System.in);

        double userInput;
        double value;
        double pow;
        double sqrt;
        double log;
        double floor;
        double ceil;
        double abs;
        double sqrtE;

        System.out.print("How many numbers would you like to process:");
        userInput = input.nextDouble();  
        sqrtE = Math.sqrt(Math.E);  

        if (userInput > 0)
        {
            while (true)
            {        
            System.out.println();
            System.out.print("Please enter a number: ");
            value = input.nextDouble(); 
            sqrt = Math.sqrt(value);
            log = Math.log(value);
            ceil = Math.ceil(value);
            floor = Math.floor(value);
            pow = Math.pow(value, value);
            abs = Math.abs(value);


                System.out.println ("Using truncated integer value for exponent of power method.");
                System.out.printf("Math.pow(%.4f,%.4f)=%.4f\n", ceil, ceil, pow);
                if (value < 0 )
                {
                System.out.println ("Cannot use negative number for square root, using absolute value instead.");
                System.out.printf("Math.sqrt(%4f)=%.4f\n", abs, sqrt);
                }
                else 
                {
                System.out.printf("Math.sqrt(%4f)=%.4f\n", value, sqrt);
                }
                if (value < 0 )
                {
                System.out.println ("Cannot use negative number for log method, using absolute value instead.");
                System.out.printf("Math.log(%.4f)=%.4f\n", abs, log);
                }
                else 
                {
                System.out.printf("Math.log(%.4f)=%.4f\n", value, log);
                }
                System.out.printf("Math.floor(%.4f)=%.4f\n", value, floor);
                System.out.printf("Math.ceil(%.4f)=%.4f\n", value, ceil);
                System.out.printf("Math.abs(%.4f)=%.4f\n", value, abs);
                userInput = userInput - 1;
            }
            System.out.printf("\n\nThe square root of e is: \nMath.e = %.4f \n", sqrtE);
        }

        if (userInput < 0)
        {
            System.out.print("You must enter a number greater than or equal to 0.\n");          
        }
        if (userInput == 0)
        {
            System.out.print("No numbers to process.\n");   
            System.out.printf("\n\nThe square root of e is: \nMath.e = %.4f \n", sqrtE);            
        }

    }
}

3 个答案:

答案 0 :(得分:0)

使用while循环,使用if()。

中给出的条件

“你想要处理多少个数字:”的答案是一个整数值,不能是双倍的。

答案 1 :(得分:0)

我认为你正在努力实现这个目标:

    while(userInput <0){
       System.out.print("You must enter a number greater than or equal to 0.\n"); 
       userInput = input.nextInt();
     }

如果您想限制尝试次数:

  int MAX_ATTEMPTS = 10;
  int attempts = 0;
  while(userInput <0 && attempts < MAX_ATTEMPTS ){
   System.out.println("You must enter a number greater than or equal to 0.\n"); 
   userInput = input.nextInt();
   attempts++;
  }

  if(userInput <0 ){
     System.out.println("You didn't enter correct value in "+
                                                       MAX_ATTEMPTS +" attempts");
  }

答案 2 :(得分:0)

只需创建一个读取正值的方法。

public int readPositiveInt() {
   int userInput = input.nextInt();
   if(userInput<0) {
       System.out.println("You must enter a number greater than or equal to 0.");
       return readPositiveInt();
   }
   return userInput;
}