gpt4 book ai didi

java - if 条件错误

转载 作者:行者123 更新时间:2023-12-02 07:10:42 25 4
gpt4 key购买 nike

我有三个变量 int value1、value2 和一个 String 运算符。我想检查 if 条件。假设 value1=4,value2=3 并且在运算符='>'中。那么我如何检查示例的 if 条件 -

int value1=4;
int value2=3;

String operator=">";
if (value1+operator+value2)
{
System.out.println("hello");
}
i know the this is wrong but how to correct it..

最佳答案

您将无法直接使用字符串。您必须首先检查它是什么类型的运算符,然后执行显式数学运算。

public boolean applyOperator(String op, int a, int b) {
if (">".equals(op)) {
return a > b;
} // the others are an exercise to the reader.
}

// later in the code

if(applyOperator(operator, value1, value2)) {
System.out.println("Hello world!");
}

关于java - if 条件错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15562793/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com