Java学习4

三元运算符

格式:关系表达式?表达式1:表达式2;

计算规则:

首先计算  关系表达式,如果值为true ,表达式1的值就是运算结果。

如果为false,表达式2的值就是运算结果

//定义两个变量

int a =10;

int b =20;

//获取两个数据中的最大值

int max = a > b ? a : b;//如果a大于b为真,则把a赋值给max,为false是则把b赋值给max

//输出结果

System.out.println("max:"+max);

任务要求 两个物体分别为180kg,200kg。判断两个物体重量是否一样。

int a =180;

int b =200;

boolean weight = a==b ?true :false;

System.out.println("weight:"+weight);

150 210 165求最高值 

int a =150;

int b =210;

int c =165;

int weight = a>b ? a :b;

int weight2 = weight>c ?weight : c;

System.out.println("max:"+weight2);

数据输入

scanner 


Scanner使用步骤

import java.util.Scanner;

public class He {

public static void main(String[] args){

Scanner sc =new Scanner(System.in);

//输入数据

        int x = sc.nextInt();

//输出数据

        System.out.println("x:"+x);

}

}

输入体重

Scanner sc =new Scanner(System.in);

//输入数据

System.out.println("请输入三个数值:");

int a = sc.nextInt();

int b = sc.nextInt();

int c = sc.nextInt();

int g = a>b? a : b;

int max =g>c? g:c;

//输出数据

System.out.println("最大值:"+max);

流程控制 

顺序结构,分支结构(if,switch),循环结构(for,while,do...while)

顺序结构,按照代码先后顺序依次执行。

if语句


if语句格式1

int a =10;

int b =20;

int c =10;

//需求判断a和b的值是否相等,如果相等就在控制台输出 a等于b

  if (a==b){

System.out.println("a等于b");

}

if (a==c){

System.out.println("a等于c");

}


运行结果


if语句格式2

int a =10;

int b =20;

int c =10;

//需求判断a和b的值是否相等,如果相等就在控制台输出 a等于b

  if (a==b){

System.out.println("a等于b");

}else{

System.out.println("a不等于b");

}

if (a==c){

System.out.println("a等于c");

}


if...else语句结果

判断奇偶数

Scanner number =new Scanner(System.in);

System.out.println("请输入一个整数");

int num = number.nextInt();

if (num%2==0){

System.out.println(num+"是偶数");

}else {

System.out.println(num+"是奇数");

}


判断奇偶数


if语句格式3

周一至周日:

Scanner number =new Scanner(System.in);

System.out.println("请输入一个星期数(1-7):");

int week =number.nextInt();

if (week==1){

System.out.println("星期一");

}else if (week ==2){

System.out.println("星期二");

}else if (week ==3){

System.out.println("星期三");

}else if (week ==4){

System.out.println("星期四");

}else if (week ==5){

System.out.println("星期五");

}else if (week ==6){

System.out.println("星期六");

}else if (week ==7){

System.out.println("星期天");

}


结果

考试成绩 90-100 A 80-89 B  70-79 C 60-69 D

Scanner number =new Scanner(System.in);

System.out.println("请输入分数:");

int score =number.nextInt();

if (score>=90&&score<=100){

System.out.println("A");

}else if (score>=80&&score<=89){

System.out.println("B");

}else if (score>=70&&score<=79){

System.out.println("C");

}else if (score>=60&&score<=69){

System.out.println("D");

}else if (score>100||score<0){

System.out.println("请重新输入分数");

}

switch语句


switch语句格式


执行流程

Scanner number =new Scanner(System.in);

System.out.println("请输入数字1-7:");

int week = number.nextInt();

switch (week){

case 1:

System.out.println("星期一");

break;

case 2:

System.out.println("星期二");

break;

case 3:

System.out.println("星期三");

break;

case 4:

System.out.println("星期四");

break;

case 5:

System.out.println("星期五");

break;

case 6:

System.out.println("星期六");

break;

case 7:

System.out.println("星期天");

break;

default:

System.out.println("你输入的星期有误!");

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容