Нужно снова запрашивать после ввода отрицательного значения

Я пытаюсь создать новый код, и теперь проблема заключается в том, что после того, как пользователь ввел отрицательное значение, он что-то напечатает и снова предложит ввести значение. Что я должен добавить после 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);            
        }

    }
}

person Bolor Ch    schedule 20.10.2012    source источник
comment
ответ на вопрос «Сколько чисел вы хотите обработать?» будет целым числом, не может быть двойным.   -  person Jayy    schedule 20.10.2012


Ответы (3)


используйте цикл while с условием, заданным в if().

Также ответ на вопрос «Сколько чисел вы хотите обработать:» будет целым числом, не может быть двойным.

person Jayy    schedule 20.10.2012

Просто создайте метод для чтения положительных значений.

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;
}
person vfcosta    schedule 20.10.2012

Я думаю, вы пытаетесь достичь этого:

    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");
  }
person Yogendra Singh    schedule 20.10.2012