Java黑皮书课后练习题14.6

14.6(Game: display a checkerboard) Write a program that displays a checkerboard in which each white and black cell is a Rectangle with a fill color black or white, as shown in Figure 14.44c.

 

package section_14;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class _06 extends Application {
	public void start(Stage primaryStage) {
		GridPane pane = new GridPane();

		int count = 0;
		double side = 50;
		for (int i = 0; i < 8; i++) {
			count++;
			for (int j = 0; j < 8; j++) {
				Rectangle rectangle = new Rectangle(side, side, side, side);
				if (count % 2 == 0)
					rectangle.setFill(Color.WHITE);
				pane.add(rectangle, j, i);
				count++;
			}
		}
		Scene scene = new Scene(pane);
		primaryStage.setTitle("_14_06");
		primaryStage.setScene(scene);
		primaryStage.show();
	}

	public static void main(String[] args) {
		launch(args);
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java黑皮课后习题10.11要求我们编写一个程序,该程序在一个二维数组中查找是否存在一个指定的值。如果存在,则返回该值的位置(行和列),否则返回(-1, -1)。 首先,我们需要创建一个二维数组,并初始化它。然后,通过遍历数组的每个元素来查找指定值。当找到指定值时,记录其位置并返回。 以下是解题思路: 1. 创建一个名为findValue的方法,参数为一个二维整数数组和一个整数值。 2. 在方法内,使用两个嵌套的for循环遍历数组的每个元素,外层循环控制行,内层循环控制列。 3. 在每次循环中,检查当前元素是否等于指定值。如果是,返回当前位置(row, column)。 4. 如果遍历完整个数组都没有找到指定值,返回位置(-1, -1)。 以下是代码示例: ```java public class Main { public static void main(String[] args) { // 创建二维数组 int[][] array = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; int target = 5; // 指定值 // 调用findValue方法查找指定值的位置 int[] position = findValue(array, target); // 输出结果 if (position[0] == -1 && position[1] == -1) { System.out.println("指定值不存在!"); } else { System.out.println("找到指定值,位置为(" + position[0] + ", " + position[1] + ")"); } } public static int[] findValue(int[][] array, int target) { for (int row = 0; row < array.length; row++) { for (int column = 0; column < array[row].length; column++) { if (array[row][column] == target) { return new int[]{row, column}; } } } return new int[]{-1, -1}; } } ``` 以上代码会输出"找到指定值,位置为(1, 1)",即指定值5在二维数组的第2行第2列。如果指定值不在数组中,则输出"指定值不存在!"。 希望这个回答对你有帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值