在 If 循环中使用 Coupon.equals 进行字符串比较。

huangapple go评论70阅读模式
英文:

Comparing Strings in If loop using Coupon.equals

问题

我正在从JSON中读取数据,并尝试与我想要的验证进行比较。
它能够运行,但是否有更好的方法呢?

我希望它能够验证从“0.025”、“0.03”、“0.035”、“0.04”、“0.045”、“0.05”等直到1.00的值。

代码:

String Coupon = (String) data.get("coupon");

if (Arrays.asList("0.025", "0.03", "0.035", "0.04", "0.045", "0.05").contains(Coupon)) {
    System.out.println("找到有效数据");
}
英文:

I am reading data from a JSON and am attempting to compare to the validations I want to have.
It works, but is there a better way to do it?

I want it do validate values from "0.025", "0.03", "0.035", "0.04", "0.045", "0.05", etc.. until 1.00

code:

 String Coupon = (String) data.get("coupon");
	          
if(Coupon.equals("0.025")||Coupon.equals("0.03")||(Coupon.equals("0.035")||Coupon.equals("0.04"))||(Coupon.equals("0.045")||Coupon.equals("0.05"))){
    System.out.println("Valid Data Found");  
}

答案1

得分: 2

使用一个Set来校验针对一组固定数据的最简单方法可能是检查你的优惠券是否包含在有效数据中。

例如:

public static void main(String[] args) {
    String inputString = "0.03";
    Set<String> validCoupons = new HashSet<>(
        Arrays.asList("0.025", "0.03", "0.035", "0.04", "0.045", "0.05"));
    if (validCoupons.contains(inputString)) {
        System.out.println("发现有效优惠券!");
    }
}
英文:

Easiest way for you to verify against a fixed set of data is probably to use a Set and check if your coupon is contained within the valid data.

For example:

public static void main(String[] args) {
    String inputString = &quot;0.03&quot;;
    Set&lt;String&gt; validCoupons = new HashSet&lt;&gt;(
        Arrays.asList(&quot;0.025&quot;, &quot;0.03&quot;, &quot;0.035&quot;, &quot;0.04&quot;, &quot;0.045&quot;, &quot;0.05&quot;));
    if (validCoupons.contains(inputString)) {
        System.out.println(&quot;Valid coupon found!&quot;);
    }
}

答案2

得分: 0

你可以这样做

    for (double i = 0.025; i <= 1;) {
        String temp = String.valueOf(i);
        if (Coupon.equals(temp)) {
            System.out.println("找到有效数据");
        }
        System.out.println(String.valueOf(temp));
        BigDecimal num1 = new BigDecimal(temp);
        BigDecimal num2 = new BigDecimal("0.005");
        BigDecimal addResult = num1.add(num2);
        String addStr = String.valueOf(addResult);
        i = Double.parseDouble(addStr);
    }
英文:

You can do like this:

for (double i = 0.025; i &lt;= 1;) {
        String temp=String.valueOf(i);
        if (Coupon.equals(temp)) {
            System.out.println(&quot;Valid Data Found&quot;);
        }
        System.out.println(String.valueOf(temp));
        BigDecimal num1 = new BigDecimal(temp);
        BigDecimal num2 = new BigDecimal(&quot;0.005&quot;);
        BigDecimal addResult = num1.add(num2);
        String addStr=String.valueOf(addResult);
        i=Double.parseDouble(addStr);
    }

huangapple
  • 本文由 发表于 2020年9月25日 14:58:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/64059290.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定