1. if (条件表达式){ //表达式结果为真就执行代码块的;否则直接跳过不执行
2. if(条件表达式){ //表达式结果为真就执行后面的代码块;否则执行else后的代码块
......
/*
分支结构中的if -else(条件判断结构)
第一种:三种结构
if(条件表达式){
}
第二种,二选一
if(条件表达式){
}else{
代码块;
}
第三种多选一
if(条件表达式){
代码块;
}else if(条件表达式){
代码块2;
}else if (条件表达式){
执行代码块3;
}
....
else{
执行代码n;
}
*/
class IfTest
{
public static void main(String[] args)
{
int heartBeats = 79;
if (heartBeats <60 || heartBeats > 100)
{
System.out.println("需要做进一步检查");
}
System.out.println("检查结束");
//举例二选一
int age = 23;
if ( age<18 )
{
System.out.println("你还可以看动画片");
}else{
System.out.println("你可以看成人了");
}
//多选一
if (age <0)
{
System.out.println("您输入的数据非法");
}else if(age<18){
System.out.println("青少年时期");
}else if(age <35){
System.out.println("青壮年");
}else if(age <60)
{
System.out.println("中年时期");
}else if(age > 120){
System.out.println("老年时期");
}else {
System.out.println("你是要成仙");
}
}
}
---------- java ----------
检查结束
你可以看成人了
青壮年
输出完成 (耗时 0 秒) - 正常终止
/*
如何从键盘获取不同的值;使用Scanner类
实现步骤
1.导包:import java.util.Scanner;
2.Scanner 的实例化
3.调用方法获取指定类型变量
*/
import java.util.Scanner;
class ScannerTest
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
int num = scan.nextInt();
System.out.println(num);
}
}
/*
如何从键盘获取不同的值;使用Scanner类
实现步骤
1.导包:import java.util.Scanner;
2.Scanner 的实例化
3.调用方法获取指定类型变量
*/
import java.util.Scanner;
class ScannerTest
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("请输入姓名:");
String name = scan.next();
System.out.println(name);
System.out.println("请输入年龄:");
int age = scan.nextInt();
System.out.println(age);
System.out.println("请输入你的体重:");
double weight = scan.nextDouble();
System.out.println(weight);
System.out.println("性别:");
boolean islove =scan.nextBoolean();
System.out.println(islove);
System.out.println("男/女");
String gender = scan.next();
char genderChar = gender.charAt(0);
System.out.println(genderChar);
}
}
/*
如何从键盘获取不同的值;使用Scanner类
实现步骤
1.导包:import java.util.Scanner;
2.Scanner 的实例化
3.调用方法获取指定类型变量
*/
import java.util.Scanner;
class ScannerTest
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("请输入成绩:");
int score = scan.nextInt();
if (score == 100)
{
System.out.println("奖励一辆车");
} else if (score > 80 && score <= 99 )
{
System.out.println("奖励一部手机");
} else if (score >= 60 && score <80)
{
System.out.println("奖励一台游戏机");
} else {
System.out.println("哪里也不能去在家学习");
}
}
}
import java.util.Scanner;
class IfTest2
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("请输入第一个整数:");
int num1 = scan.nextInt();
System.out.println("请输入第二个整数:");
int num2 = scan.nextInt();
System.out.println("请输入第三个整数:");
int num3 = scan.nextInt();
if (num1 >= num2)
{
if (num3 >= num1)
{
System.out.println(num2 + "," + num1 + "," + num3);
}
else if (num3 <= num2)
{
System.out.println(num3 + "," + num2 + ","+ num1);
}
else
{
System.out.println(num2 + "," + num3 + "," + num1);
}
}else
{
if (num3> num2)
{
System.out.println(num1 + ","+ num2 + "," +num3);
}
else if (num3 <= num1)
{
System.out.println(num3 + "," + num1 + ","+num2);
}
else
{
System.out.println(num1 + "," +num3 + "," + num2);
}
}
}
}
import java.util.Scanner;
class IfEtst
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("请输入身高:");
int height = scan.nextInt();
System.out.println("请输入你的财富:(千万)");
double wealth = scan.nextDouble();
System.out.println("请输入你是否帅:(true/false)");
boolean isHandsome = scan.nextBoolean();
if (height >= 180 && wealth >=1 && isHandsome)
{
System.out.println("我非他不嫁!");
}else if (height >= 180 || wealth >=1 || isHandsome)
{
System.out.println("嫁了吧,比上不足,比下有余!");
}else {
System.out.println("不嫁!");
}
}
}
import java.util.Scanner;
class IfEtst
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("请输入身高:");
int height = scan.nextInt();
System.out.println("请输入你的财富:(千万)");
double wealth = scan.nextDouble();
System.out.println("请输入你是否帅:(是/否)");
String isHandsome = scan.next();
if (height >= 180 && wealth >=1 && isHandsome.equals("是"))//#判断变量里的值是否与括号里的值相同
{
System.out.println("我非他不嫁!");
}else if (height >= 180 || wealth >=1 || isHandsome.equals("是"))
{
System.out.println("嫁了吧,比上不足,比下有余!");
}else {
System.out.println("不嫁!");
}
}
}
import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
// 获取char 类型变量,Scanner没有相关方法,只能获取一个字符串
System.out.println("请输入您的性别:");
String gender = scan.next();
char genderChar = gender.charAt(0);//转换成char类型的操作;获取索引为0 为真上的字符
System.out.println(genderChar);
}
}
测试
请输入您的性别:
男
男