(1)编写程序,随机生成一副牌,并从中随机选择4张牌。
/*
* Copyright (c) 2019-2020 Demo All Rights Reserved.
* ProjectName: Pratice Two
* @author: 简程涛
* @date: 23/09/2019, 10:00
* @version: 1.0
*/ package com.pratice;
import javax.swing.plaf.synth.SynthStyle;
import java.util.Random;
public class Card {
/**
* main函数
* @param args
*/
public static void main(String[] args)
{
Card card = new Card();
System.out.println("One card:");
String[] cards = card.generateCards();
System.out.println("After shuffling:");
card.shuffle(cards);
System.out.println("Select four cards from them randomly:");
for (int i = 0; i < 4; i++)
{
System.out.printf("" + cards[i] + " ");
}
}
/**
* 生成牌
* @return
*/
public String[] generateCards()
{
String[] cards = new String[54];
cards[53] = "大王";
cards[52] = "小王";
for (int i = 0; i < cards.length - 2; i++)
{
cards[i] = configSuit(i) + configFace(i);
}
print(cards);
return cards;
}
/**
* 打印牌
* @param cards
*/
public void print(String[] cards)
{
for (int i = 0; i < cards.length; i++)
{
if (i % 13 == 0)
{
System.out.println();
}
System.out.printf("%-5s", cards[i]);
}
System.out.println();
}
/**
* 打乱牌序
* @param cards
* @return
*/
public String[] shuffle(String[] cards)
{
Random random = new Random();
for (int i = 0; i < cards.length; i++)
{
int temp = random.nextInt(cards.length);
swap(i, temp, cards);
}
print(cards);
return cards;
}
/**
* 交换函数
* @param a
* @param b
*/
public void swap(int a, int b, String[] cards) {
String t = cards[a];
cards[a] = cards[b];
cards[b] = t;
}
/**
*生成花色
* @param i
* @return
*/
public String configSuit(int i)
{
int x = i / 13;
switch (x)
{
case 0: return "♥";
case 1: return "♦";
case 2: return "♠";
case 3: return "♣";
default: return null;
}
}
/**
* 生成面值
* @param i
* @return
*/
public String configFace(int i)
{
int mod = i % 13;
if (mod == 0)
{
return "A";
}
else if (mod > 0 && mod < 10)
{
return "" + (mod + 1);
}
else if (mod == 10)
{
return "J";
}
else if (mod == 11)
{
return "Q";
}
else if (mod == 12)
{
return "K";
}
else
{
return null;
}
}
}
运行截图:
(2)“四子连”游戏,是一个双人游戏,在游戏中,玩家轮流将有颜色的棋子放在一个6 x7的垂直悬挂的网格中。在对手实现一行、一列或者一条对角线上有四个相同颜色的棋子之前,你能做到就赢了,否则输了。
/*
* Copyright (c) 2019-2020 Demo All Rights Reserved.
* ProjectName: Practice Two
* @author: 简程涛
* @date: 25/09/2019, 18:52
* @version: 1.0
*/
package com.pratice;
import java.lang.reflect.Array;
import java.util.Scanner;
public class Chess {
static int dir[][] = {{-1, 0}, {-1, -1}, {0, -1}, {-1, 1}};
public static void main(String[] args) {
String[][] chess = new String[10][10];
init(chess);
output(chess);
while(true){
playChess1(chess);
playChess2(chess);
}
}
/**
* 玩家一
* @param chess 棋盘
*/
public static void playChess1(String[][] chess) {
System.out.println("Drop a white disk on (x, y):");
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
int y = sc.nextInt();
chess[x][y] = "\uf0a2";
output(chess);
if (judge(chess, x, y)) {
System.out.println("The white player wins.");
}
}
/**
* 玩家二
* @param chess 棋盘
*/
public static void playChess2(String[][] chess) {
System.out.println("Drop a black disk on (x, y):");
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
int y = sc.nextInt();
chess[x][y] = "\uf0a4";
output(chess);
if (judge(chess, x, y)) {
System.out.println("The white player wins.");
}
}
/**
* 输出棋盘
* @param chess 棋盘
*/
public static void output(String[][] chess) {
for(int i = 1; i <= 6; ++i) {
for(int j = 1; j <= 7; ++j) {
System.out.printf(chess[i][j] + " ");
}
System.out.println();
}
}
/**
* 初始化
* @param chess 棋盘
*/
public static void init(String[][] chess) {
for(int i = 1; i <= 6; ++i) {
for(int j = 1; j <= 7; ++j) {
chess[i][j] = "\uF0B3";
}
}
}
public static boolean judge(String[][] chess, int x, int y){
for (int i = 0; i < 4; i++){
if (dfs(x, y, dir[i][0], dir[i][1], chess[x][y], chess) == 4){
return true;
}
}
return false;
}
/**
* 搜索
* @param x 当前横坐标
* @param y 当前纵坐标
* @param dx x变化量
* @param dy y变化量
* @param now 当前黑/白棋
* @param chess 棋盘
* @return
*/
public static int dfs(int x, int y, int dx, int dy, String now, String[][] chess){
if (x > 6 || y > 7 || x < 1 || y < 1 || chess[x][y] != now){
return 0;
}
return dfs(x + dx, y + dy, dx, dy, now, chess) + 1;
}
}
运行截图:
(3)从键盘输入二维数组,对二维数组排序并输出。
/*
* Copyright (c) 2019-2020 Demo All Rights Reserved.
* ProjectName: Pratice Two
* @author: 简程涛
* @date: 23/09/2019, 17:22
* @version: 1.0
*/
package com.pratice;
import java.lang.reflect.Array;
import java.util.Scanner;
//对二维数组每一行数字进行排序
import static java.util.Arrays.sort;
public class Sort {
static int m, n;
public static void main(String[] args)
{
int a[][] = new int[100][100];
Scanner sc = new Scanner(System.in);
System.out.println("Please input rows of the array:");
n = sc.nextInt();
System.out.println("Please input columns of the array:");
m = sc.nextInt();
System.out.println("Please input array:");
input(a);
func(a);
output(a);
}
public static void input(int[][] a)
{
Scanner sc = new Scanner(System.in);
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
a[i][j] = sc.nextInt();
}
}
}
public static void func(int[][] a)
{
for (int i = 0; i < n; i++)
{
sort(a[i], 0, m);
}
}
public static void output(int[][] a)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
System.out.printf("%-2d ", a[i][j]);
}
System.out.println();
}
}
}
运行截图:
(4)请结合Javafx的TextinputDialog和Alert组件,输入一个年份和月份(输入格式:2019-09),并将该年月的月历在GUl界面中输出。
/*
* Copyright (c) 2019-2020 Demo All Rights Reserved.
* ProjectName: Pratice Two
* @author: 简程涛
* @date: 25/09/2019, 14:55
* @version: 1.0
*/
package com.pratice;
import javafx.application.Application;
import javafx.scene.control.Alert;
import javafx.scene.control.TextInputDialog;
import javafx.stage.Stage;
import java.time.LocalDate;
import java.util.Optional;
import java.util.Scanner;
public class Calendar extends Application {
/**
* 判断闰年
* @param year
* @return
*/
public boolean isLeap(int year){
return year%400 == 0 || (year % 4 == 0 && year % 100 != 0);
}
/**
* 获取每月天数
* @param year
* @param month
* @return
*/
public int getDays(int year, int month){
switch(month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12: return 31;
case 2: return isLeap(year) ? 29 : 28;
default: return 30;
}
}
/**
* 获取每月第一天的星期
* @param year
* @param month
* @return
*/
public int getFirstWeek(int year,int month){
LocalDate firstDay = LocalDate.of(year, month, 1);
int week = firstDay.getDayOfWeek().getValue();
return week;
}
/**
* 打印一个月日历
* @param year
* @param month
* @return
*/
public String print(int year, int month){
String s = "" + month + "月\n" + "Su Mo Tu We Th Fr Sa\n";
int first = getFirstWeek(year, month);
if (first % 7 != 0)
{
int t = first;
for (int k = 0; k < t - 1; k++)
{
s = s + " ";
}
}
int days = getDays(year, month);
for (int i = 1; i <= days; i++)
{
String t1 = String.format("%02d", i);
String t = String.format("%-3s", t1);
s = s + t;
if ((i + first ) % 7 == 0)
{
s = s + "\n";
}
}
s = s + "\n";
return s;
}
@Override
public void start(Stage stage) throws Exception {
TextInputDialog textInputDialog = new TextInputDialog();
textInputDialog.setTitle("Simple Calendar");
textInputDialog.setContentText("Input year and month please");
Optional<String> result = textInputDialog.showAndWait();
// 字符串转化成整数
String s = result.get();
int m = Integer.parseInt(s.substring(0, 4));
int y = Integer.parseInt(s.substring(4, 6));
//输出
Alert alert = new Alert(Alert.AlertType.NONE);
alert.setTitle("Simple Calendar");
alert.setContentText(print(m, y));
alert.showAndWait();
}
}
运行截图: