【问题标题】:Java: How to write a condition for operators? [closed]Java:如何为运算符编写条件? [关闭]
【发布时间】:2016-11-24 18:52:19
【问题描述】:

enter image description here 我需要一些关于 if 条件的帮助。我知道基本的工作原理:如果 x == 1,则返回一些东西。但是,当我需要为不同的运算符定义某些情况时,我应该怎么做。就像如果运算符是 + 那么结果表示总和。

所以基本上我需要将链接中的条件转换为递归方法的基本情况。我们了解到,我们总是将 if 用于基本情况。我知道如何使用更小或更大的,但我不知道运营商。

【问题讨论】:

  • '如果运算符是+,那么结果表示和' - 我不明白你的意思
  • if 循环在 Java 中不是有效的控制流结构。
  • 请避免使用问题相关部分的链接。使信息成为问题的一部分(例如复制文本)。链接消失了,并不是每个人都可以在某些环境中访问任何 url。链接还有其他问题。

标签: java operators


【解决方案1】:

仅供参考:if() 不是循环 while() 是。 if() 语句作为真或假工作,如果语句为真,则执行某个代码,否则执行其他代码。

例如:

if(1==1){// yourcode } // Always as true
// or

String hello="hi there";
if(hello.contains("hi there")){ // Your code which if the statement happen to be true }
    else { // Not true}

int x=3, s=1, i=2;
if(x==(s+i)){ // Your code which if the statement happen to be true }
    else { // Not true}

您还可以在网上找到很多教程,帮助您更好地了解所有运算符!

【讨论】:

    【解决方案2】:

    你是想说明它是积极的还是消极的?如果是这样,您将执行以下操作...

    if(x >= 0){  //this operator is saying if x is greater than or equal to 0 
                 // you can remove the equal sign to have it just greater or
                 // switch it to less than.
    
        //if positive
    
    
     }else{
          //all other numbers, which would just be negative numbers
     }
    

    【讨论】:

    • 我编辑了帖子并添加了一张照片
    猜你喜欢
    • 2021-02-21
    • 2021-09-15
    • 1970-01-01
    • 2015-01-28
    • 1970-01-01
    • 2016-10-27
    • 2015-02-15
    • 1970-01-01
    • 2013-12-20
    相关资源
    最近更新 更多