Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.Scanner;
- public class Main {
- public static Scanner stream = new Scanner(System.in);
- static double ask(double minimum, double maximum){
- for(;;){
- try{
- System.out.println("Введите число не меньше " + minimum + " и не больше " + maximum);
- double number = stream.nextDouble();
- if (number < minimum){
- throw new TooLowValue(minimum);
- }
- if (number > maximum){
- throw new TooHighValue(maximum);
- }
- return number;
- }catch (MyException e){
- System.out.println(e.getMessage());
- }
- }
- }
- public static void main(String[] args) {
- System.out.println("Введите числа - нижнюю и верхнюю границу: ");
- double first = stream.nextDouble();
- double second = stream.nextDouble();
- double min = first < second ? first : second;
- double max = first > second ? first : second;
- double[] numbers = new double[10];
- for(int i = 0; i < 10; i++){
- for(;;){
- try{
- System.out.println("Введите число не меньше " + min + " и не больше " + max);
- double number = stream.nextDouble();
- if (number < min){
- throw new TooLowValue(min);
- }
- if (number > max){
- throw new TooHighValue(max);
- }
- numbers[i] = number;
- break;
- }catch (MyException e){
- System.out.println(e.getMessage());
- }
- }
- }
- }
- }
- class MyException extends NumberFormatException{
- public MyException(String Message){
- super(Message);
- }
- }
- class TooHighValue extends MyException {
- public TooHighValue(double top){
- super("Ваше число больше " + top + "!");
- }
- }
- class TooLowValue extends MyException {
- public TooLowValue(double bottom){
- super("Ваше число меньше " + bottom + "!");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement