0

我正在尝试构建一个新代码,现在问题是用户输入负值后,这将打印一些内容并再次提示输入值。我应该在第 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);            
        }

    }
}
4

3 回答 3

0

使用带有 if() 中给出的条件的 while 循环。

此外,“您要处理多少个数字:”的答案将是一个整数值,不能是双精度数。

于 2012-10-20T03:00:30.093 回答
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");
  }
于 2012-10-20T03:00:45.183 回答
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;
}
于 2012-10-20T03:06:48.810 回答