简体   繁体   English

条件运算符(!=)(If-else语句)

[英]Conditional Operators(!=)(If-else Statement)

Desired Output: 所需输出:

Data Type: int
Variable Name: a
Initial Value: 0
Conditional Operator: !=
Conditional Value: 5
Increment/Decrement: ++
Interval: 1
for(int a=0;a!=5;a+=1)
{
}
for(int a=1;a!=5;a+=1)
{
}
for(int a=2;a!=5;a+=1)
{
}
for(int a=3;a!=5;a+=1)
{
}
for(int a=4;a!=5;a+=1)
{
}

If you change the interval to 2, it should output "Infinite Loop! Try Again" and if you change the interval to 1, it should output the desired output stated above. 如果将间隔更改为2,则应输出“无限循环!再试一次”;如果将间隔更改为1,则应输出上述所需的输出。

My program's output: 我程序的输出:

Data Type: int
Variable Name: a
Initial Value: 0
Conditional Operator: !=
Conditional Value: 5
Increment/Decrement: ++
Interval: 1
Infinite Loop! Try Again!

My code: 我的代码:

else if(inc_dec.equals("++") && conditionalOperator.equals("!="))
 {
   for(float c=initialValue;c!=conditionalValue;c+=interval)
     {
       if(initialValue == conditionalValue)
         System.out.print("for("+dataType+" "
         +varName+"="+c+";"+varName+conditionalOperator+conditionalValue+";"
         +varName+"+="+interval+"){\n}\n");
       else break;
     }
       System.out.println("Infinite Loop! Try Again!");
  }

PS The code should only use if-only statements PS该代码应仅使用if-only语句

I fixed it! 我修好了它!

else if(inc_dec.equals("++") && conditionalOperator.equals("!="))
                    {
                     for(float c=initialValue;c!=conditionalValue;c+=interval)
                     {
                      if(conditionalValue%interval == 0)
                         System.out.print("for("+dataType+" "+varName+"="+c+";"+varName+conditionalOperator+conditionalValue+";"+varName+"+="+interval+"){\n}\n");
                      else break;
                     }
                    }

Initial value of a = 0 and if you increment variable a value by 2 then the condition a!=5 will never be true. a = 0初始值,如果将变量a值增加2则条件a!=5将永远不会为真。 Your a becomes 0,2,4,6,8...infinity (But it will never become 5). 您的a变为0、2、4、6、8 ...无穷大(但永远不会变成5)。 Hence you are going into infinite loop. 因此,您将陷入无限循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM