++,——运算符:对变量做加一或减一的操作
*单独操作的时候,++和——不管放在前面还是后面,结果是一样的。
*但是,如果参与了运算操作的时候,如果++或——在变量的后面的时候,先拿变量参与运算操作,后变量进行++或——。
*如果++或——在变量的前面的时候,先变量做++或——,后拿变量参与运算操作。
package cn.cp;
public class OperatorDemo04 {
public static void main(String[] args){
int x = 5;
System.out.println(x); //5
x++;
++x;
System.out.println("x:"+x); //7
int y = ++x;
System.out.println("x:"+x); //8
System.out.println("y:"+y); //8
int z = x++;
System.out.println(x); //9
System.out.println(z); //8
int q = x--;
System.out.println(x); //8
System.out.println(q); //9
}
}
2.
3.
public static void main(S
tring[] args){
int x = 5;
int y = 8;
int z = x += y;
System.out.println(z);
3.
package cn.cp;
/**
* 逻辑 && || &
*
& | ^ !
*
&& ||
* 0
< x < 10
* @author Administrator
*/
public class OperatorDemo05 {
public static void main(String[] args){
int x = 10;
int y = 20;
int z = 30;
System.out.println((x > y) && (y > x)); //false && 与 (有一个错的就是错的)
System.out.println((x > y) || (y < z)); //true || 或 (只要有正确的就是正确的)
System.out.println((x > y) & (y > x)); //false
System.out.println((x > y) | (y < z)); //true
}
}
●逻辑运算符&&与&的区别? (&&与 &单与 ||或 |单
或)
A:&&与&的区别?
a:最终结果一样
b:&&具有短路效果。左边是false,右边不执行
&是无论左边是false还是true,右边都会执行
B:||与|的区别?
a:最终结果一样
b:||具有短路效果,左边是true,右边不执行
|是无论左边是false还是true,右边都会执行
System.out.println((x > y) && (y > x)); //false && 与 (有一个错的就是错的)
System.out.println((x > y) || (y < z)); //true || 或 (只要有正确的就是正确的)
System.out.println((x > y) & (y > x));
//false
System.out.println((x > y) | (y < z)); //true
System.out.println((x++
> 10) && (++y < 20)); //false
System.out.println("x:" + x); //11
System.out.println("y:" + y); //20
System.out.println((++x
> 10) && (y++ < 20) && (z-- < 30)); //false
System.out.println(x); //12
System.out.println(y); //21
System.out.println(z); //30
System.out.println((x-- < 10) & (y++
< 20)); //false
System.out.println(x); //11
System.out.println(y); //22
System.out.println(z); //30
5.
(1)键盘录入两个数据,比较这两个数据是否相等
(2)键盘录入三个数据,比较这三个数据中的最大值
package cn.cp;
public class OperatorDemo06 {
public static void main(String[] args){
int x = 3;
int y = 5;
//执行的逻辑顺序 3>5 ? 3:5
int z = (x > y) ? x : y;
System.out.println(z);
boolean flag = (x == y) ? true : false;
System.out.println(flag);
int q =6;
int temp = (x >y) ? x : y;
int max = (temp > q) ? temp :q;
System.out.println(max);
}
}
运行结果:
5
false
6
6
.
public class IfDemo {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int a=sc.nextInt();
System.out.println("输入a的值");
int b=sc.nextInt();
System.out.println("输入b的值");
if (a == b) {
System.out.println("这两个数相等");
}
}
}
7. 键盘录入一个整数,用if语句判断,
如果这个数是偶数则打印这个数是偶数;如果录入的数是奇数,则打印这个数是奇数。
public class OperatorDemo06 {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int a=sc.nextInt();
if (a%2 ==0){
System.out.println("a是偶数");
}else{
System.out.println("a是奇数");
}
}
}
运行结果:
12
a是偶数
- if语句实现判断学生等级
键盘录入学生考试成绩,根据成绩判断该学生属于哪个级别
90-100[包含90和100] 优秀
80-90[包含80不包含90] 好
70-80[包含70不包含80] 良
60-70[包含60不包含70] 及格
60分以下[不包含60] 不及格
package
cn.cp;
import
java.util.Scanner;
public class IfElseIfDemo {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("请输入学生成绩");
int a=sc.nextInt();
if((90<=a) && (a<=100)){
System.out.println("优秀");
}else if((80<=a) && (a<90)){
System.out.println("好");
}else if((70<= a) && (a<80)){
System.out.println("良");
}else if((60<= a) && (a<70)){
System.out.println("及格");
}else {
System.out.println("不及格");
}
}
}
运行结果:
8.2
package cn.cp;
/**
- 王者荣耀段位
*/
import java.util.Scanner;
public class IfElseIfDemo {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("输入成绩");
int rank=sc.nextInt();
if (rank < 0 || rank > 100 ){
System.out.println("输入段位不符合要求,请输入(0-100)");
}else if (rank < 60){
System.out.println("黑铁");
}else if( (rank >=60) && ( rank<70) ){
System.out.println("黄金");
}else if( (rank >= 70) && (rank < 80) ){
System.out.println("钻石");
}else if( (rank >= 80) && (rank < 90) ){
System.out.println("星耀");
}else if( (rank >= 90) && (rank < 100) ){
System.out.println("最强王者");
}else
System.out.println("666");
}
}
运行结果:
9.判断某年是否为闰年;用户从控制台输入需要判断的年份值,由程序使用if-else判断该年是否为闰年,并将判断结果输出到控制台。
public class IfElseIfDemo {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("输入年份");
int x=sc.nextInt();
if (x < 0 ){
System.out.println("输入的年份不符合要求,请输入>0的年份");
}else if ( x%4 == 0){
System.out.println("闰年");
}else {
System.out.println("非闰年");
}
}
}
画乌龟
package cn.cp;
import javax.swing.JFrame;
/** * JFrame—窗体-----相框
- JPanel—画布-----相片
-
- 导包:
-
- 1.手动导包 import
-
- 2.自动导包 ctrl+shift+o
-
- 3.点击红色的叉号
- */
public class TurtlePro {
public static void main (String[] args){
//1.创建一个窗体对象
JFrame frame = new JFrame();
//2.给窗体添加标题
frame.setTitle(“李光洙”);
//3.给窗体添加大小
frame.setSize(1400,800);
//4.窗体居中显示
frame.setLocationRelativeTo(null);
//类名.静态常量 减少内存的占用
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//画布
TJPanel tj = new TJPanel();
//添加画布到面板上
frame.add(tj);
//5.显示窗体 f
rame.setVisible(true);
}
}
package cn.cp;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.JPanel;
public class TJPanel extends JPanel{
//乌龟的初始位置
int x = 300; i
nt y = 200;
//乌龟脚的初始位置
int k1 = 300, t1 = 200; //左上角
int k2 = 300, t2 = 200; //右上角
int k3 = 300, t3 = 200; //左下角
int k4 = 300, t4 = 200; //右下角
//Graphics g:画笔
@Override
public void paint(Graphics g) {
// TODO Auto-generated method stub
super.paint(g);
//修改底部背景色
this.setBackground(Color.CYAN);
g.setColor(Color.GREEN);
g.fillOval(x, y, 70, 100);
//加眼睛
g.setColor(Color.BLACK);
g.fillOval(x + 10, y + 30, 10, 15);
g.fillOval(x + 50, y + 30, 10, 15);
//加舌头
g.setColor(Color.RED);
g.fillOval(x + 30, y, 8, 6);
//画脚 左上
g.setColor(Color.GRAY);
g.fillOval(k1 - 85, t1 + 95, 60, 70);
//画脚 右上
g.fillOval(k2 + 85, t2 + 95, 60, 70);
//画脚 左下
g.fillOval(k3 - 85, t3 + 235, 60, 70);
//画脚 右下
g.fillOval(k1 + 85, t4 + 235, 60, 70);
//加乌龟壳
g.setColor(Color.ORANGE);
g.fillOval(x - 66, y + 66, 200, 300);
//壳加文字
g.setColor(Color.RED);
//设置字体大小
Font font = new Font (Font.DIALOG,Font.BOLD,30);
g.setFont(font);
g.drawString(“阿牛”, x -20 , y + 180);
/*//给画笔设置颜色
g.setColor(Color.RED);
//画矩形
g.drawRect(20, 150, 400, 200);
//画实心
g.fillRect(120,200,160,80);
g.setColor(Color.GREEN);
//画圆和椭圆
g.drawOval(30, 250, 100, 80);
g.fillOval(130, 300,100,100);
//画线段
g.drawLine(80, 80, 300, 350);
*/
}
}