Tag: if语句

Java:if-return-if-return vs if-return-elseif-return

问一个无关的问题 ,我有这样的代码: public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; // Check property values } 我得到了一条评论,声称这不是最优的,相反(如果我理解正确的话)应该这样做: public boolean equals(Object obj) { if (this == obj) return true; else if (obj == null) return false; else if (getClass() != obj.getClass()) return […]

Java客户端python服务器套接字编程

我在java中有一个客户端程序,它向python服务器发送消息“Hello”。 Java代码 import java.io.*; import java.net.*; public class MyClient { public static void main(String[] args) { try{ Socket soc=new Socket(“localhost”,2004); DataOutputStream dout=new DataOutputStream(soc.getOutputStream()); dout.writeUTF(“Hello”); dout.flush(); dout.close(); soc.close(); }catch(Exception e){ e.printStackTrace();} } Python服务器代码 import socket # Import socket module soc = socket.socket() # Create a socket object host = “localhost” # Get local machine name port […]

设计模式在java中使用数百个if else实现业务规则

我必须使用数百行以下代码实现某些业务规则 if this then this else if then this . . // hundreds of lines of rules else that 我们是否有任何设计模式可以有效地实现这一点或重用代码,以便它可以应用于所有不同的规则。 我听说过规范模式,它创建了类似下面的内容 public interface Specification { boolean isSatisfiedBy(Object o); Specification and(Specification specification); Specification or(Specification specification); Specification not(Specification specification); } public abstract class AbstractSpecification implements Specification { public abstract boolean isSatisfiedBy(Object o); public Specification and(final Specification specification) […]

JAVA; 如果没有,预计不会发表其他声明

嗨,我想知道我的代码有什么问题,我得到标题中说明的错误。 这有什么问题? 提前致谢。 为什么我需要这么多细节,我觉得我已经很好地描述了它。 import java.util.Scanner; public class CombinationLock { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print(“Please enter three uppercase letters.”); System.out.println(“Hit enter after each letter.”); String str1 = in.nextLine(); String str2 = in.nextLine(); String str3 = in.nextLine(); System.out.println(“Would you like to open the lock?”); if(Yes) Scanner lol = new […]

在循环中使用if语句? – 处理

假设我必须在for循环中使用if语句,并且for循环在某个条件下触发,而if语句仅在for循环达到某个阶段时触发。 例如,条件是一个计数器,当某个事件发生时,例如球从屏幕上掉下来,它会计数。 每当球越过屏幕时,逐个绘制圆圈。 当第一行中的圆圈到达屏幕的末尾时,圆圈开始出现在第一行下方的第二行。 但是第二行对我来说不起作用,我用if语句实现了它。 float BallY = 50; // y value of the ball float BallX = 260; // x value of the ball float ScoreX = 52; float ScoreY = 40; int counter; void setup() { size(512, 348); //width and height of screen counter = 0; } void draw() { frameRate(600); background(255); fill(0); […]

带有单个else语句的多个if语句

我正在复习考试,虽然我们有答案密钥,但我不明白正确的答案。 if (score >= 90) grade = ‘A’; if (score >= 80) grade = ‘B’; if (score >= 70) grade = ‘C’; if (score >= 60) grade = ‘D’; else grade = ‘F’; 答案密钥表示“只有当等级<70时,此代码才能正常工作”。 我知道else语句与最后一个if语句相关联,但我仍然感到困惑。 我在这里先向您的帮助表示感谢。

在if语句中创建对象并在以后使用它

我正在为Infix表示法编写一个Parser。 在if语句中,我声明了变量newchild。 否则我希望它抛出exception。 但是当我超出范围时,编译器不再知道变量。 我无法在if语句之前声明它,因为根据我们所处的情况,变量被赋予不同的数据类型。 我该怎么做才能解决这个问题? public class ParserForInfixNotation { public Node parse(List tokenList) { Stack myStack = new Stack(); int i =1; while(i i+3){ //die Klammer muss mindestens 2 Stellen enthalten anzahlklammern–; j++; } else if(tokenList.get(j) == “(“){ anzahlklammern++; j++; } else if ((Character.isDigit(tokenList.get(j).charAt(1))) || tokenList.get(j) == “+” || tokenList.get(j) == “*” || tokenList.get(j) […]

如果在圆括号内的赋值情况下条件评估为真或假,该怎么办?

我在学习OCA / OCP for Java时发现了这个令人惊讶的事情。 下面是if(测试条件)部分让我感到惊讶的第一段代码。 public class BooleanIf { public static void main(String[] args) { boolean b = false; System.out.println(Boolean.valueOf(b = true)); if (b = true) System.out.println(“true”); else System.out.println(“false”); } 现在这令人惊讶的输出是“真实的”。 我了解到必须有一个关系条件, if (a > b)或if (a != b)同样返回true或false。 我想知道它是如何在这种情况下返回真实的。 它调用Boolean.valueOf()吗?

在java中打印阶乘计算过程

嗨,我需要打印析因计算的过程。 例如,如果用户输入为5,系统应打印出“5 * 4 * 3 * 2 * 1 = 120” 我有这个代码: public static void factorial() { Scanner sc = new Scanner(System.in); int factorial = 1; int count; System.out.println(me+”, This is option 2: Calculate a factorial”); System.out.print(“Enter your number: “); int number = sc.nextInt(); System.out.println(); if (number>0) { for (count=1; count<=number; count++) factorial = […]

关于“if”声明的快速问题

我在这里做的这个小方法并没有返回正确的成本价值。 我想知道我是否错误地使用了if语句。 由于我是Java的新手,这是我第一次使用带有字符串的if语句。 我没有书或老师,只是自己学习。 任何帮助将非常感激。 编辑:发布完整的代码 import java.util。*; 公共类UseCarRental { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println(“Thank you for choosing ICT car Rentals\n” + “Pleae enter your full name:”); String renterName = input.nextLine(); System.out.println(“Please enter your zip code:”); int renterZipcode = input.nextInt(); input.nextLine(); System.out.println(“Please enter the size car you would […]