admin 管理员组文章数量: 1087135
2024年1月11日发(作者:oracle正则表达式空格)
实验三 Java 语言的流程控制和数组
一、 实验项目名称
开发环境的设置和安装
二、 实验目的
(1)
学习和掌握 IntelliJ IDEA 软件的开发
(2)
学习和掌握条件控制
(3)
学习和掌握循环控制
三、 实验步骤
1.
编写一个程序,结合 JavaFx 实现输入年份找出对应的中国生肖值。
代码:
package ;
import ation;
import ;
import putDialog;
import ;
import al;
public class ChineseZodiac extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
TextInputDialog inputDialog = new TextInputDialog();
le("根据年份查询十二生肖值:");
derText("请输入你将查询的年份:");
tentText("");
Optional
int year = nt(());
String result = judge(year);
Alert alert = new Alert(ATION);
le("根据年份查询十二生肖值:");
derText(year+"年对应十二生肖里的:");
tentText(result);
dWait();
}
public String judge(int year){
String temp = switch(year % 12){
case 0-> "猴";
case 1-> "鸡";
case 2-> "狗";
case 3-> "猪";
case 4-> "鼠";
case 5-> "牛";
case 6-> "虎";
case 7-> "兔";
case 8-> "龙";
case 9-> "蛇";
case 10-> "马";
case 11-> "羊";
default-> throw new RuntimeException("error");
};
return temp;
}
public static void args){
(args);
}
}
2.
编写程序,提示用户输入两个圆的中心坐标和各自的半径值,然后判断第二个圆是在第一个圆内,还是和第一个圆重叠。
代码:
package ;
import r;
public class TwoCircles {
public static void main(String[] args){
Scanner input = new Scanner();
double x1,y1,x2,y2,r1,r2,d1,d2;
n("请输入两圆的中心坐标:");
x1 = uble();
y1 = uble();
x2 = uble();
y2 = uble();
n("请依次输入两圆的半径:");
r1 = uble();
r2 = uble();
d1 = ((x1-x2,2)+(y1-y2,2));
d2 = r1 + r2;
if(d1 >= d2) n("第二个圆在第一个圆外");
else if((r1-r2) < d1 && d1 < d2) n("第二个圆与第一个圆重叠");
else if(x1 == x2 && y1 == y2 && r1 == r2) n("第二个圆与第一个圆重合");
else if(d1 < (r1-r2)) n("第二个圆在第一个圆内");
}
}
3.
编写程序,模拟从一副扑克牌中抽出 1 张牌。
代码:
① Poker类:
package ;
public class Poker {
private String point;
private PokerColor color;
//构造函数,初始化poker数组
public Poker (int index){
if(index == 53) point = "小王";
else if(index == 52) point = "大王";
else{
point = indexToString(index / 4 + 1);
int temp = index % 4;
if(temp == 0) color = _PEACH;
else if(temp == 1) color = _PEACH;
else if(temp == 2) color = ;
else color = ;
}
}
public String indexToString(int number){
if(number == 1) return "A";
else if(number == 11) return "J";
else if(number == 12) return "Q";
else if(number == 13) return "K";
else return (number+"");
}
public String getPoint(){
return point;
}
public PokerColor getColor(){
return color;
}
}
② TestPoker类:
package ;
import r;
public class TestPoker {
public static void main(String[] args){
Scanner input = new Scanner();
//创建扑克牌
Poker[] poker = new Poker[54];
for(int i = 0;i < 54;i++) poker[i] = new Poker(i);
//洗牌
washPoker(poker);
//抽取
n("这里有54张扑克牌,你想抽取第几张?");
int index = t();
//点数
String str1 = poker[index].getPoint();
//花色(排除大小王)
String str2 = ("大王")||("小王") ? "" :
ColorToString(poker[index].getColor());
n("这张扑克牌是:"+str2+str1);
}
public static void washPoker(Poker[] poker){
int[] index = new int[54];
Poker[] tempPoker = new Poker[54];
boolean[] flag = new boolean[54];
int n = 0;
//保存0-53的乱序索引并查重
while(n < 54){
int temp = (int)(()*54);
if(!flag[temp]){
index[n++] = temp;
flag[temp] = true;
}
}
//更新数组(洗牌)
for(int i = 0;i < 54;i++) tempPoker[i] = poker[index[i]];
for(int i = 0;i < 54;i++) poker[i] = tempPoker[i];
}
public static String ColorToString(PokerColor color){
if(color == _PEACH) return "♠";
else if(color == _PEACH) return "♥";
else if(color == ) return "♦";
else return "♣";
}
}
enum PokerColor{
BLACK_PEACH,RED_PEACH,CUBE,PLUM;
}
4.
显示金字塔,编写程序,提示用户输入一个 1 到 15 之间的整数,然后显示一个金字塔形状的图案。 图形如下所示:
输出行数:3
1
2
3
代码:
package ;
import r;
public class DigitalPyramids {
public static void main(String[] args){
Scanner input = new Scanner();
n("请输入1-15的整数:");
int number = t();
int length = 1,length2 = (number-1)*2;
n(number+"的数字金字塔为:");
//执行number次
for(int i = 1;i <= number;i++){
int tempNumber = i;
boolean flag = false;
//输出前置空格
for(int j = 0;j < length2;j++){
(" ");
}
//输出数字
for(int j = 0;j < length;j++){
if(tempNumber == 1) flag = true;
("%d ",tempNumber);
if(!flag) tempNumber--;
else tempNumber++;
}
n();
length += 2;
1
1
2
2 3 2
length2 -= 2;
}
}
}
5.
编写程序,实现五子棋游戏。五子棋是一种两人对弈的纯策略型棋类游戏,通常双方分别使用黑白两色的棋子,下在棋盘直线与横线的交叉点上,先形成 5 子连线者获胜。结合JavaFx 实现五子棋游戏。要求:游戏界面较好,交互方便。运行结果类似:
代码:
① :
package ;
import ation;
import ;
import ;
public class MainApp extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
var pane = new FiveChess();
var scene = new Scene(pane,620,620);
ne(scene);
le("五子棋游戏");
();
}
public static void main(String[] args){
launch(args);
}
}
② :
package ;
import ;
import csContext;
import ;
import ;
import ;
public class FiveChess extends Pane {
private GraphicsContext gc;
public boolean isBlackPerforming = true;
public boolean hasFinished = false;
public int[][] chessboard = new int[21][21];
public FiveChess() {
Canvas canvas = new Canvas(620, 620);
gc = phicsContext2D();
getChildren().add(canvas);
//绘制600*600的棋盘
l();
for (int i = 1; i <= 21; i++) {
ct(10, 10 + (i - 1) * 30, 600, 1);
ct(10 + (i - 1) * 30, 10, 1, 600);
}
//获取鼠标单击位置,然后修正并获取落子位置
ouseClicked(e -> {
if(hasFinished){
return;
}
int x = (int) ();
int y = (int) ();
//以方块(30*30)中心作为参照,向左向右修正鼠标单击位置
int chessX = (x - 10) % 30 >= 15 ? (x - 10) / 30 * 30 + 30 + 10 : (x - 10) / 30 * 30 +
10;
int chessY = (y - 10) % 30 >= 15 ? (y - 10) / 30 * 30 + 30 + 10 : (y - 10) / 30 * 30 +
10;
//防止覆盖
if(chessboard[chessY / 30 - 1][chessX / 30 - 1]!=0){
return;
}
//黑白棋轮流下
if (isBlackPerforming) {
l();
al(chessX - 12, chessY - 12, 24, 24);
chessboard[chessY / 30 - 1][chessX / 30 - 1] = 1;
if (hasWon(1, chessY / 30 - 1, chessX / 30 - 1)){
Alert alert=new Alert(ATION);
le("游戏结束");
derText("黑棋获胜!");
tentText("恭喜黑棋获得本次游戏的胜利!");
();
hasFinished=true;
}
isBlackPerforming = false;
} else {
l();
al(chessX - 12, chessY - 12, 24, 24);
oke();
Oval(chessX - 12, chessY - 12, 24, 24);
chessboard[chessY / 30 - 1][chessX / 30 - 1] = -1;
if (hasWon(-1, chessY / 30 - 1, chessX / 30 - 1)){
Alert alert=new Alert(ATION);
le("游戏结束");
derText("白棋获胜!");
tentText("恭喜白棋获得本次游戏的胜利!");
();
hasFinished=true;
}
isBlackPerforming = true;
}
});
}
//判断输赢
public boolean hasWon(int key, int lastRow, int lastColumn) {
int continousCount = 0;
//第一个方向:行
int[] thisRow = new int[21];
for (int i = 0; i < 21; i++) {
thisRow[i] = chessboard[lastRow][i];
}
for (int i = 0; i < 21; i++) {
if (thisRow[i] == key) {
continousCount++;
if (continousCount == 5) {
return true;
}
} else {
continousCount = 0;
}
}
//第二个方向:列
continousCount = 0;
int[] thisColumn = new int[21];
for (int i = 0; i < 21; i++) {
thisColumn[i] = chessboard[i][lastColumn];
}
for (int i = 0; i < 21; i++) {
if (thisColumn[i] == key) {
continousCount++;
if (continousCount == 5) {
return true;
}
} else {
continousCount = 0;
}
}
//第三个方向:平行于主对角线方向
continousCount=0;
int[] mainAngle=new int[21];
int tmpRow=lastRow,tmpColumn=lastColumn,j;
while(tmpRow>0 && tmpColumn>0){
tmpRow--;
tmpColumn--;
}
for (j = 0;tmpRow<20 && tmpColumn<20 ; j++) {
mainAngle[j]=chessboard[tmpRow][tmpColumn];
tmpRow++;
tmpColumn++;
}
for (int i = 0; i < 21; i++) {
if (mainAngle[i] == key) {
continousCount++;
if (continousCount == 5) {
return true;
}
} else {
continousCount = 0;
}
}
//第四个方向:平行于副对角线方向
continousCount=0;
int[] subAngle=new int[21];
tmpRow=lastRow;
tmpColumn=lastColumn;
while(tmpColumn<20 && tmpRow>0){
tmpRow--;
tmpColumn++;
}
for (j = 0;tmpRow<20 && tmpColumn>0 ; j++) {
subAngle[j]=chessboard[tmpRow][tmpColumn];
tmpRow++;
tmpColumn--;
}
for (int i = 0; i < 21; i++) {
if (subAngle[i] == key) {
continousCount++;
if (continousCount == 5) {
return true;
}
} else {
continousCount = 0;
}
}
return false;
}
}
四、 实验数据及处理结果
1. 结合 JavaFx 实现输入年份找出对应的中国生肖值:
2.判断两圆位置:
3. 模拟从一副扑克牌中抽出 1 张牌:
4.数字金字塔:
5.五子棋:
五、 思考讨论题或体会或对改进实验的建议
1.新版本的switch支持箭头表达式返回,避免了贯穿结构。
2.扑克牌程序的体会:
①基本数据类型数组执行如“int[] num = new int[4]”后会被初始化,内部含有值,而对象数组不同,对象数组在执行如“Poker[] poker = new Poker[54]”后内部为null,空指针没有指向实例对象,所以要用个循环对其初始化。
②数组索引(除去大王小王)通过“除4加1”的方式可以建立与点数的映射,通过“对4取模”可以建立与花色的映射
③打乱数组顺序的方式之一是利用随机数建立一个乱序的索引数组index[]并查重
④静态方法只能访问静态成员和方法,非静态则没那么多限制。
⑤枚举类型中不能直接写如“♠”,不符合标识符命名规则。
3.五子棋:
①必须修正鼠标单击位置来获取落子位置,一方面鼠标可能点击非发处,另一方面鼠标不一定能准确点击到两线交点处。
②绘制棋子时,参数“顶点坐标”是图形外接矩形的左顶点坐标,要想将棋子刚好放到交点处要将坐标减去其半径。
③要标记每次落子位置,防止覆盖。
④棋盘坐标xy分别对应棋盘数组的列和行。
⑤判断输赢的方法:遍历四个方向,对黑棋来说,每出现一次黑子起连续数量加1,遇白子重
置为0,满5获胜。
4.通过这次实验增强了我对条件循环类对象方法javafx的理解。
版权声明:本文标题:Java 语言的流程控制和数组-实验报告 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1704980693a468416.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论