没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
120 套 Java 典型案例库
1 / 293
120 套 Java 典型案例库
120 套 Java 典型案例库
2 / 293
案例 001:编写程序,判断给定的某个年份是否是闰年
闰年的判断规则如下:
(1)若某个年份能被 4 整除但不能被 100 整除,则是闰年。
(2)若某个年份能被 400 整除,则也是闰年。
import java.util.Scanner;
class Bissextile{
public static void main(String[] arge){
System.out.print("请输入年份");
int year; //定义输入的年份名字为“year”
Scanner scanner = new Scanner(System.in);
year = scanner.nextInt();
if (year<0||year>3000){
System.out.println("年份有误,程序退出!");
System.exit(0);
}
if ((year%4==0)&&(year%100!=0)||(year%400==0))
System.out.println(year+" is bissextile");
else
System.out.println(year+" is not bissextile ");
}
}
案例 002:Java 俄罗斯方块案例
#include <windows.h>
#include <stdio.h>
#include <time.h>
#define CELL 20
#define ROWS 25
#define COLS 15
//升级所需分数值
#define SCORE_LEVEL_INC 80
#define ID_TIMER 1
/////////////////全局变量/////////////////////////////
HWND hwnd; //保存窗口句柄
int score=0; //分数
int level=0; //级数
int interval_unit=25; //随级数递增的时间间隔增量
int interval_base=300; //时间间隔基量
int old_interval; //保存当前的时间间隔,用于加速操作
120 套 Java 典型案例库
3 / 293
int cur_left,cur_top; //记录方块当前的位置
int width_block,height_block; //方块的宽带和高度
bool isPause=false; //暂停标识
UINT timer_id=0; //保存计时器 ID
static byte *block=NULL; //方块,方块为随机大小,采用动态分配内存方式,所以
这里是指针变量
byte g_panel[ROWS][COLS]={0};
////////////////////////////////////////////////////
LRESULT CALLBACK WndProc ( HWND,UINT,WPARAM,LPARAM );
void DrawPanel ( HDC hdc ); //绘制表格
void RefreshPanel ( HDC hdc ); //刷新面板
void DoDownShift ( HDC hdc ); //下移
void DoLeftShift ( HDC hdc ); //左移
void DoRightShift ( HDC hdc ); //右移
void DoAccelerate ( HDC hdc ); //加速
void DoRedirection ( HDC hdc ); //改变方向
void ClearRow ( HDC hdc ); //消行
bool ExportBlock(); //输出方块,
//该函数会直接修改全局变量 block,width_block,height_block,
//cur_left 和 cur_top
bool IsTouchBottom ( HDC hdc ); //判断是否到达底部
int main()
{
HINSTANCE hInstance=GetModuleHandle ( NULL );
TCHAR szAppName[]=TEXT ( "teris" );
MSG msg;
WNDCLASS wc;
wc.style=CS_HREDRAW|CS_VREDRAW;
wc.lpfnWndProc=WndProc;
wc.cbClsExtra=0;
wc.cbWndExtra=0;
wc.hInstance=hInstance;
wc.hIcon=LoadIcon ( NULL,IDI_APPLICATION );
wc.hCursor=LoadCursor ( NULL,IDC_ARROW );
wc.hbrBackground= ( HBRUSH ) GetStockObject ( WHITE_BRUSH );
wc.lpszMenuName=NULL;
wc.lpszClassName=szAppName;
if ( !RegisterClass ( &wc ) )
{
120 套 Java 典型案例库
4 / 293
printf ( "RegisterClass occur errors!" );
return 0;
}
hwnd=CreateWindow ( szAppName,TEXT ( "Teris Demo" ),
WS_OVERLAPPEDWINDOW,
0,0,0,0,
NULL,
NULL,
hInstance,
NULL );
ShowWindow ( hwnd,SW_SHOW );
UpdateWindow ( hwnd );
while ( GetMessage ( &msg,NULL,0,0 ) )
{
TranslateMessage ( &msg );
DispatchMessage ( &msg );
}
return msg.wParam;
}
void DrawPanel ( HDC hdc ) //绘制游戏面板
{
int x,y;
RECT rect;
for ( y=0; y<ROWS; y++ )
{
for ( x=0; x<COLS; x++ )
{
//计算方块的边框范围
rect.top=y*CELL+1;
rect.bottom= ( y+1 ) *CELL-1;
rect.left=x*CELL+1;
rect.right= ( x+1 ) *CELL-1;
FrameRect ( hdc,&rect, ( HBRUSH ) GetStockObject ( BLACK_BRUSH ) );
}
}
}
void DoDownShift ( HDC hdc ) //下移
{
if ( NULL==block ) return;
//判断是否到达底部
120 套 Java 典型案例库
5 / 293
if ( IsTouchBottom ( hdc ) ) //到底部
{
//消行处理
ClearRow ( hdc );
ExportBlock(); //输出下一个方块
}
cur_top++;
RefreshPanel ( hdc );
}
void DoLeftShift ( HDC hdc ) //左移
{
int x,y;
if ( NULL==block ) return;
if ( 0==cur_left ) return;
if ( cur_top<0 ) return; //方块没有完整显示前,不能左移
for ( y=0; y<height_block; y++ )
{
for ( x=0; x<width_block; x++ ) //从左边开始扫描,获取该行最左边的实
心方格块
{
if ( * ( block+y*width_block+x ) )
{
//判断当前方格在面板上面左边一个方格是否为实心,是就代表不能再左
移
if ( g_panel[cur_top+y][cur_left+x-1] ) return;
break; //只判断最左边的一个实心方格,之后直接扫描下一行
}
}
}
cur_left--;
RefreshPanel ( hdc );
}
void DoRightShift ( HDC hdc ) //右移
{
int x,y;
if ( NULL==block ) return;
if ( COLS-width_block==cur_left ) return;
if ( cur_top<0 ) return; //方块完整显示前不能右移
剩余292页未读,继续阅读
资源评论
矛盾和规律
- 粉丝: 329
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功