我的代码是:
import java.util.Scanner;
class mainClass {
public static void main (String [] args) {
secondaryClass SCO = new secondaryClass();
Scanner scanner = new Scanner(System.in);
String randomtext = scanner.nextLine();
if(randomtext.equals("What is the time"))
{
SCO.giveTime();
}
else if (randomtext.equals("Whats the time"))
{
SCO.giveTime();
}
}
}
我想知道是否可以将 if else 语句替换为以下内容:
import java.util.Scanner;
class mainClass {
public static void main (String [] args) {
secondaryClass SCO = new secondaryClass();
Scanner scanner = new Scanner(System.in);
String randomtext = scanner.nextLine();
if(randomtext.equals("What is the time" || "Whats the time"))
{
SCO.giveTime();
}
}
}
顺便说一句,SCO是我第二课的对象,它完美地输出了时间。