SQL运算符“大于”显示不正确的结果

时间:2016-09-28 17:24:33

标签: sql sql-server integer

我使用此网站来学习SQL w3schools。使用SQL语句后:

/*
 * Generate all possible mathematical operations to the console with the numbers 
 * 0-12, where every result is (>= 0 && <= 12).
 *  - Mathematical operations are '+', '-', '*' and '/'.
 *  - The rule 'dot before line' shouldn't be used, instead the operations will 
 *    be executed from left to right.
 *  - Every among result must be between (>= 0 && <= 12) and a whole number.
 *  - Only different numbers are allowed for the operations (an operation have 
 *    2 numbers). For example 2+3 is allowed, 3*3 not.
 * 
 * A solution with recursive methods would be preferred. I want the output for
 * the length 2-10.
 * 
 * Example output with different length:
 *  - Length 3: 2+3(=5)*2(=10)
 *  - Length 5: 2+3(=5)*2(=10)+2(=12)/4(=3)
 */

我收到的结果是import java.util.ArrayList; import java.util.List; public class Generator { private static final List<Double> numbers = new ArrayList<>(); private static final List<String> operations = new ArrayList<>(); static { numbers.add(0.0); numbers.add(1.0); numbers.add(2.0); numbers.add(3.0); numbers.add(4.0); numbers.add(5.0); numbers.add(6.0); numbers.add(7.0); numbers.add(8.0); numbers.add(9.0); numbers.add(10.0); numbers.add(11.0); numbers.add(12.0); operations.add("+"); operations.add("-"); operations.add("*"); operations.add("/"); } private int lineCounter = 0; public Generator() { this.init(); } private void init() { } public void generate() { // Length 2 ########################################################### boolean okay = false; int lineCounter = 0; StringBuilder sbDouble = new StringBuilder(); for (Double first : numbers) { for (Double second : numbers) { for (String operation : operations) { if (first == second) { continue; } if (operation.equals("/") && (first == 0.0 || second == 0.0)) { continue; } double result = perform(first, operation, second); okay = this.check(result, operation); if (okay) { ++lineCounter; sbDouble = new StringBuilder(); this.computeResultAsString(sbDouble, first, operation, second, result); System.out.println(sbDouble.toString()); } } } } System.out.println("Compute with length 2: " + lineCounter + " lines"); // Length 2 ########################################################### // Length 3 ########################################################### okay = false; lineCounter = 0; sbDouble = new StringBuilder(); for (Double first : numbers) { for (Double second : numbers) { for (String operation1 : operations) { if (first == second) { continue; } if (operation1.equals("/") && (first == 0.0 || second == 0.0)) { continue; } double result1 = perform(first, operation1, second); okay = this.check(result1, operation1); if (okay) { for (Double third : numbers) { for (String operation2 : operations) { if (second == third) { continue; } if (operation2.equals("/") && third == 0.0) { continue; } double result2 = perform(result1, operation2, third); okay = this.check(result2, operation2); if (okay) { ++lineCounter; sbDouble = new StringBuilder(); this.computeResultAsString(sbDouble, first, operation1, second, result1); this.computeResultAsString(sbDouble, operation2, third, result2); System.out.println(sbDouble.toString()); } } } } } } } System.out.println("Compute with length 3: " + lineCounter + " lines"); // Length 3 ########################################################### // Length 4 ########################################################### okay = false; lineCounter = 0; sbDouble = new StringBuilder(); for (Double first : numbers) { for (Double second : numbers) { for (String operation1 : operations) { if (first == second) { continue; } if (operation1.equals("/") && (first == 0.0 || second == 0.0)) { continue; } double result1 = perform(first, operation1, second); okay = this.check(result1, operation1); if (okay) { for (Double third : numbers) { for (String operation2 : operations) { if (second == third) { continue; } if (operation2.equals("/") && third == 0.0) { continue; } double result2 = perform(result1, operation2, third); okay = this.check(result2, operation2); if (okay) { for (Double forth : numbers) { for (String operation3 : operations) { if (third == forth) { continue; } if (operation3.equals("/") && forth == 0.0) { continue; } double result3 = perform(result2, operation3, forth); okay = this.check(result3, operation3); if (okay) { ++lineCounter; sbDouble = new StringBuilder(); this.computeResultAsString(sbDouble, first, operation1, second, result1); this.computeResultAsString(sbDouble, operation2, third, result2); this.computeResultAsString(sbDouble, operation3, forth, result3); System.out.println(sbDouble.toString()); } } } } } } } } } } System.out.println("Compute with length 4: " + lineCounter + " lines"); // Length 4 ########################################################### } private boolean check(double result, String operation) { switch (operation) { case "+": case "-": case "*": { if (result > 0 && result <= 12) { return true; } break; } case "/": { if ( (Math.floor(result) == result) && (result >= 0 && result <= 12) ) { return true; } break; } } return false; } private double perform(double first, String operation, double second) { double result = 0.0; switch (operation) { case "+": { result = first + second; break; } case "-": { result = first - second; break; } case "*": { result = first * second; break; } case "/": { result = first / second; break; } } return result; } private void computeResultAsString(StringBuilder sbDouble, String operation, double second, double result) { sbDouble.append(operation); sbDouble.append(second); sbDouble.append("(="); sbDouble.append(result); sbDouble.append(")"); } private void computeResultAsString(StringBuilder sbDouble, double first, String operation, double second, double result) { sbDouble.append(first); sbDouble.append(operation); sbDouble.append(second); sbDouble.append("(="); sbDouble.append(result); sbDouble.append(")"); } public static void main(String[] args) { final Generator generator = new Generator(); generator.generate(); } } == WA1 1DP; S-958 22和其他不合适的结果(从我的观点来看,因为它们不是整数)。请帮助我了解这里使用哪个逻辑来执行此过滤器。感谢。

1 个答案:

答案 0 :(得分:1)

您的邮政编码应存储为字符串。因此,比较应该是字符串:

SELECT *
FROM Customers
WHERE postalcode > '05021'