2019-5-7
Java day02
package com.tedu.day02;
/**
*逻辑&& || & |
* & | ^ !
* && ||
* &&与&
* 最终结果一样
* &&短路效果,左边是false,右边不执行
* &无论左边是false还是true都会执行
* ||与|
* 最终结果一样
* ||具有短路效果,左边是true,右边不执行
* |无论左边是false还是true,右边都会执行
*/
public class OperatorDemo05 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int x = 10;
int y = 20;
int z = 30;
System.out.println((x > y) || (y < z));
}
}
判断年份、月份,判断该年该月有多少天。
package com.tedu.day02;
import java.util.Scanner;
/**
* 根据用户输入的年份、月份,判断该年该月有多少天。
* 思路:
* 1.使用Scanner用法;
* 三步导包
* 2.定义
* 年份(years)、月份(month)、天(days)
* 3涉及到平闰年的判断,2月份是单独考虑的;使用switch进行
* */
public class PanDuanNianYue {
public static void main(String[] args) {
// TODO Auto-generated method stub
int days = 0;
Scanner sc = new Scanner(System.in);
System.out.print("输入年份: ");//输入年份
int year = sc.nextInt();
System.out.print("输入月份: ");//输入月份
int month = sc.nextInt();
switch(month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days=31;//1,3,5,7,8,10,12为31天
break;
case 4:
case 6:
case 9:
case 11:
days=30;//4,6,9,11为30天
break;
case 2:
if(year%4==0 && year%100!=0 || year%400==0)//判断是否为闰年
days=29;
else
days=28;
break;
default:
System.out.println("月份输入错误!");//月份输入错会报错
}
System.out.println(year+"年"+month+"月有"+days+"天");
sc.close();
}
}
到1-100之间的所有的偶数
package com.tedu.day02;
//import java.util.Scanner;
/*
* 思路
* 1.新增一个变量
* 实现求和:sum
* 2.获取到1-100之间的所有的偶数;
* 使用for循环来解决;
* 遍历数据,进行对数据的判断:
* 如果是偶数的话,就执行三步;
* 3.对获取到数据进行累加
* */
public class ForDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
//Scanner sc = new Scanner(System.in);
//int a =sc.nextInt();
//System.out.println(a);
//sc.close();
int sum=0;
for ( int i=0; i <=100; i++){
if(i%2==0)
sum = sum+i;
}
System.out.println("sum="+sum);
}
}
逻辑判断
package com.tedu.day02;
/**
*逻辑&& || & |
* & | ^ !
* && ||
* &&与&
* 最终结果一样
* &&短路效果,左边是false,右边不执行
* &无论左边是false还是true都会执行
* ||与|
* 最终结果一样
* ||具有短路效果,左边是true,右边不执行
* |无论左边是false还是true,右边都会执行
*/
public class OperatorDemo05 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int x = 10;
int y = 20;
int z = 30;
System.out.println((x > y) || (y < z));
System.out.println((x > y) &&(y < z));
System.out.println((x > y) | (y < z));
System.out.println((x > y) & (y < z));
System.out.println((x++ > 10) || (++y < 20));
System.out.println("x:"+ x );
System.out.println((x > y) || (y < z));
}
}
小乌龟
TJPane.java
package com.tedu.day02;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.JPanel;
public class TJPanel extends JPanel{
//Grapgics g
int x = 300;
int y = 200;
int k1 = 300,t1 = 200;
int k2 = 300,t2 = 200;
int k3 = 300,t3 = 200;
int k4 = 300,t4 = 200;
public void paint(Graphics g) {
// TODO Auto-generated method stub
super.paint(g);
this.setBackground(Color.GRAY);
//给画笔设置颜色
/*g.setColor(Color.cyan);
//画矩形
g.drawRect(30, 30, 160, 180);
//实心
g.setColor(Color.pink);
g.fillRect(100, 200, 50, 100);
g.setColor(Color.yellow);
//画圆
g.fillOval(300, 300, 100, 150);
g.setColor(Color.pink);
g.drawOval(500, 500, 100, 100);
g.drawOval(570, 500, 100, 100);
g.drawOval(640, 500, 100, 100);
g.drawOval(710, 500, 100, 100);
//直线
g.drawLine(132, 456, 45, 45);*/
//绿龟
g.fillOval(x, y,0, 100);
//加头
g.setColor(Color.black);
g.fillOval(x, y, 70, 100);
//加眼睛
g.setColor(Color.black);
g.fillOval(x+30, y+30,10, 15);
g.fillOval(x+50, y+30,10, 15);
//加舌头
g.setColor(Color.cyan);
g.fillOval(x+30, y, 8, 6);
//加龟壳
g.setColor(Color.green);
g.fillOval(x-66, y+66, 200 ,230 );
//画脚
g.setColor(Color.cyan);
g.fillOval(k1+85, t1+75, 60, 70);
g.setColor(Color.cyan);
g.fillOval(k2-85, t2+75, 60, 70);
g.setColor(Color.cyan);
g.fillOval(k3+85, t3+235, 60, 70);
g.setColor(Color.cyan);
g.fillOval(k4-85, t4+235, 60, 70);
//加文字
g.setColor(Color.cyan);
//利用font创建字体
Font font =new Font(Font.DIALOG,Font.BOLD,30);
g.setFont(font);
g.drawString("阿拉蕾", x-20, y+180);
}
// public static void main(String[] args) {
// // TODO Auto-generated method stub
//
// }
}
TurtlePro.java
package com.tedu.day02;
import javax.swing.JFrame;
/**
* JFrame----窗体---相框
* JPanel----画布---相片
* 导包:
* 1.手动导包 import……
* 2.自动导包 ctrl+shift+o
* 3.点击红色的叉号
*
* */
public class TurtlePro {
public static void main(String[] args) {
// TODO Auto-generated method stub
//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.显示窗体
frame.setVisible(true);
}
}
此博客聚焦Java学习,涵盖判断指定年份和月份的天数、找出1 - 100间所有偶数以及逻辑判断等内容,还提及TJPane.java和TurtlePro.java文件。
167

被折叠的 条评论
为什么被折叠?



