使用if return代替if else,return 会自动跳出该执行方法。
import java.util.Scanner;
public class PartB5
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
int number = keyboard.nextInt();
if(number == 1)
{
System.out.println("猜对了");
return;
}
System.out.println("猜错了");
}
}
上述代码相当于
import java.util.Scanner;
public class PartB5
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
int number = keyboard.nextInt();
if(number == 1)
{
System.out.println("猜对了");
}
else
{
System.out.println("猜错了");
}
}
}