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

正の値を読み取るメソッドを作成するだけです。

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 に答える