.equals的引入
区别判断
import java.util.*;
public class EqualsTest {
public static void main(String[] args) {
Scanner scan = new Scanner( System.in);
System.out.println("请输入是/否");
String hand = scan.next();
System.out.println(hand.equals("是")); //判断字符名称是不是相等
System.out.println(hand == "是"); //hand == "是"; //判断是不是一个东西
}
}
例子
/*
大家都知道,男大当婚,女大当嫁。那么女方家长要嫁女儿,当然要提出一定的条件:
高:180cm以上;富:财富1千万以上;帅:是。
如果这三个条件同时满足,则:“我一定要嫁给他!!!”
如果三个条件有为真的情况,则:“嫁吧,比上不足,比下有余。”
如果三个条件都不满足,则:“不嫁!”
提示:
Sysout(“身高: (cm))
scanner.nextInt();
Sysout(“财富: (千万))
scanner.nextDouble();
Sysout(“帅否: (true/false)) (是/否)
scanner.nextBoolean(); scanner.next(); “是”.equals(str)
*/
import java.util.*;
public class IfTest3 {
public static void main(String[] args) {
/*
Scanner scan = new Scanner(System.in);
System.out.println("请输入身高\n身高:");
int height = scan.nextInt();
System.out.println("请输入财富(千万):\n财富:");
double wealth = scan.nextDouble();
System.out.println("长得帅吗?(true/flase)");
boolean hand = scan.nextBoolean();
if(height > 180 && wealth > 1 && hand) {
System.out.println("我一定要嫁给他!");
}else if(height > 180 || wealth > 1 || hand) {
System.out.println("嫁了吧,比上不足,比下有余。");
}else
System.out.println("不嫁");
*/
//另一种写法 把true换成"是" 用类名.equals(字符)
Scanner scan = new Scanner(System.in);
System.out.println("请输入身高\n身高:");
int height = scan.nextInt();
System.out.println("请输入财富(千万):\n财富:");
double wealth = scan.nextDouble();
System.out.println("长得帅吗?(是/否)");
String hand = scan.next();
if(height > 180 && wealth > 1 && hand.equals("是")) {
System.out.println("我一定要嫁给他!");
}else if(height > 180 || wealth > 1 || hand.equals("是")) {
System.out.println("嫁了吧,比上不足,比下有余。");
}else
System.out.println("不嫁");
}
}