独立实现标准字符串库的strcmp函数,即字符串比较函数,从键盘输入两个字符串,按字典序比较大小,前者大于后者输出1,前者小于后者输出-1,两者相等输出0。
样例输入:
apple one
样例输出:
-1
样例输入:
hello he
样例输出:
1
样例输入:
hello hello
样例输出:
0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package adv95; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String a = in.next(); String b = in.next(); in.close(); if (a.compareTo(b) < 0) { System.out.println("-1"); } else if (a.compareTo(b) > 0) { System.out.println(1); } else { System.out.println(0); } } } |
❤ 点击这里 -> 订阅《PAT | 蓝桥 | LeetCode学习路径 & 刷题经验》by 柳婼