|
|||||||
Поиск по диапазонам при помощи case и тернарного оператора
Время создания: 08.09.2017 08:10
Текстовые метки: code
Раздел: Java - Tutorial - Syntaxis - case
Запись: xintrea/mytetra_db_mcold/master/base/1504847420d59zm5fyxp/text.html на raw.githubusercontent.com
|
|||||||
|
|||||||
/* Используя оператор switch, написать программу,
* которая выводит на экран сообщения о принадлежности
* некоторого значения k интервалам
* (-10k, 0], (0, 5], (5, 10], (10, 10k]
*/
public class HelloWorld {
public static final int LESSER = 0; public static final int BIGGER = 1;
public static void main(String[] args) {
int k = 5;
switch (k < 1 ? LESSER : BIGGER) { case LESSER: switch (k < -10_000 ? LESSER : BIGGER) { case LESSER: System.out.println(k + " is out of range"); break; case BIGGER: System.out.println(k + " is in range (-10k, 0]"); break; } break; case BIGGER: switch (k < 6 ? LESSER : BIGGER) { case LESSER: System.out.println(k + " is in range (0, 5]"); break; case BIGGER: switch (k < 11 ? LESSER : BIGGER) { case LESSER: System.out.println(k + " is in range (5, 10]"); break; case BIGGER: switch (k < 10_000 ? LESSER : BIGGER) { case LESSER: System.out.println(k + " is in range (10, 10k]"); break; case BIGGER: System.out.println(k + " is out of range"); break; } } } } } }
--------------------
/**
* Created by mutagen on 06.09.13.
*/
public class SwitchKungFu { public static void main(String[] args) { System.out.println(isInRange(2, new Range(0, 5))); System.out.println(isInRange(6, new Range(0, 5))); }
public static String isInRange(int i, Range range) { switch (checkRange(range, i)) { case 1: return "Value " + i + " within " + range.toString(); case 0: return "Value " + i + " not in " + range.toString(); } return ""; }
static int checkRange(Range r, int i) { if (i > r.getLow() && i < r.getHigh()) { return 1; } else { return 0; } }
static class Range { private int low; private int high;
Range(int low, int high) { this.low = low; this.high = high; }
public int getLow() { return low; }
public int getHigh() { return high; }
@Override public String toString() { return "range [" + low + ", " + high + ']'; } } }
----------------
import java.util.regex.Matcher; import java.util.regex.Pattern;
/**
* Created by mutagen on 06.09.13.
*/
public class SwitchKungFu { public static void main(String[] args) { System.out.println(new Range("(0,5]").isInRange(2)); System.out.println(new Range("(0, 5]").isInRange(0)); System.out.println(new Range("(0,5]").isInRange(5)); System.out.println(new Range("[0,5]").isInRange(0)); }
static class Range { private static Pattern pattern = Pattern.compile("(.)(\\d+),\\S*(\\d+)(.)"); private int low; private int high; private boolean lowInclude = false; private boolean highInclude = false; private String range;
Range(String range) { this.range = range; init(); }
private void init() { Matcher matcher = pattern.matcher(this.range); if (matcher.find()) { String sign = matcher.group(1); lowInclude = Boolean.valueOf("[".equals(sign)); low = Integer.valueOf(matcher.group(2)); high = Integer.valueOf(matcher.group(3)); sign = matcher.group(4); highInclude = Boolean.valueOf("]".equals(sign)); } }
public String isInRange(int i) { switch (checkRange(i)) { case 1: return "Value " + i + " within " + this.toString(); case 0: return "Value " + i + " not in " + this.toString(); } return ""; }
private int checkRange(int i) { if (lowInclude && highInclude) { return (i >= low && i <= high) ? 1 : 0; } else if (lowInclude && !highInclude) { return (i >= low && i < high) ? 1 : 0; } else if (!lowInclude && highInclude) { return (i > low && i <= high) ? 1 : 0; } else { return (i > low && i < high) ? 1 : 0; } }
@Override public String toString() { return "range " + this.range; } } }
--------------
import java.util.Scanner;
public class MadSwitch { public static void main(String [] args) { Scanner scan = new Scanner(System.in);
while ( true ) { System.out.print("Number: "); if ( ! scan.hasNextInt() ) break;
int n = scan.nextInt();
switch ( Boolean.compare( ( n <= -10000 ), true ) ) { case 0 : System.out.println("Less than or equals -10K"); break; default : switch ( Boolean.compare( ( n > 0 ), false ) ) { case 0 : System.out.println("Inside (-10K; 0]"); break; default : switch ( Boolean.compare( ( n > 5 ), false ) ) { case 0: System.out.println("Inside (0; 5]"); break; default : switch ( Boolean.compare( ( n > 10 ), false ) ) { case 0 : System.out.println("Inside (5; 10]"); break; default : switch ( Boolean.compare( ( n > 10000 ), false ) ) { case 0: System.out.println("Inside (10; 10K]"); break; default : System.out.println("Over 10K"); } } } } } } } } |
|||||||
Так же в этом разделе:
|
|||||||
|
|||||||
|