import java.util.Scanner;
public class J714
{
/**
* @taking input from user
*/
public static void main(String[] args)
{
/**
* @taking input from user
*/
Scanner input = new Scanner(System.in);
System.out.print("Enter the length of a square matrix: ");
int n = input.nextInt();
/**
* @declaring the array of n*n size
*/
int[][] board = new int[n][n]; boolean isSameOnARow = false; boolean isSameOnAColumn = false;
boolean isSameOnADiagonal = false; boolean isSameOnASubdiagonal = false;
/**
* @performing the fucntions
*/
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
board[i][j] = (int)(Math.random() * 2.0D);
System.out.print(board[i][j]);
}
System.out.println();
}
/**
* @taking a for loop
*/
for (int i = 0; i < board.length; i++) {
boolean same = true;
for (int j = 1; j < board[0].length; j++) {
/**
* @checking the conditions
*/
if (board[i][0] != board[i][j]) {
same = false;
break;
}
}
/**
* @checking the conditions
*/
if (same) {
System.out.println("All " + board[i][0] + "'s on row " + i);
isSameOnARow = true;
}
}
for (int j = 0; j < board[0].length; j++) {
boolean same = true;
for (int i = 1; i < board.length; i++) {
if (board[0][j] != board[i][j]) {
same = false;
break;
}
}
if (same) {
System.out.println("All " + board[0][j] + "'s on column " + j);
isSameOnAColumn = true;
}
}
boolean same = true;
for (int i = 1; i < board.length; i++) {
if (board[0][0] != board[i][i]) {
same = false;
break;
}
}
if (same) {
System.out.println("All " + board[0][0] + "'s on major diagonal");
isSameOnADiagonal = true;
}/**
* @checking the conditions
*/
same = true;
for (int i = 1; i < board.length; i++) {
if (board[0][(board.length - 1)] != board[i][(board.length - 1 - i)]) {
same = false;
break;
}
}
/**
* @checking the conditions and printing the required elements
*/
if (same) {
System.out.println("All " + board[0][(board.length - 1)] + "'s on sub-diagonal");
isSameOnASubdiagonal = true;
}
if (!isSameOnARow) {
System.out.println("No same numbers on a row");
}
if (!isSameOnAColumn) {
System.out.println("No same numbers on a column");
}
if (!isSameOnADiagonal) {
System.out.println("No same numbers on the major diagonal");
}
if (!isSameOnASubdiagonal)
System.out.println("No same numbers on the sub-diagonal");
}
}
import java.util.Scanner;
public class ExploringMatrix
{
public static void main (String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.print("Enter the size for the matrix: ");
int size = scan.nextInt();
int[][] m = new int[size][size]; for (int i =0;i<size;i++)
{
for (int j =0;j<size;j++)
{
m[i][j] = (int) (Math.random()*2);
System.out.print(m[i][j]);
}
System.out.println();
}
boolean letitbe;
//Row
int j =0; boolean row=false, column=false, major =false, sub = false;
for(int i=0; i<size;i++)
{
letitbe = true;
for (j =0;j<size-1;j++)
{
if (m[i][j] != m[i][j+1]) letitbe = false;
}
if (letitbe)
{
System.out.println("All " + m[i][j] + "s on row " + i);
row = true;
}
}
//Column
for(int i=0; i<size;i++)
{
letitbe = true;
for (j =0;j<size-1;j++)
{
if (m[j][i] != m[j+1][i]) letitbe = false;
}
if (letitbe)
{
System.out.println("All " + m[j][i] + "s on column " + i);
column = true;
} }
//Major diagonal, there is only one eh?
letitbe = true;
for (int i=0;i<size-1;i++)
{
if (m[i][i]!= m[i+1][i+1]) letitbe = false;
}
if (letitbe)
{
System.out.println("All " + m[0][0] + "s on major diagonal");
major = true;
} letitbe = true;
for (int i=0;i<size-1;i++)
{
if (m[i][size-i-1]!= m[i+1][size-i-1-1]) letitbe = false;
}
if (letitbe)
{
System.out.println("All " + m[0][size-1] + "s on sub-diagonal");
sub = true;
} if ( column == false) System.out.println("No same numbers on a column");
if ( row == false) System.out.println("No same numbers on a row");
if ( major == false) System.out.println("No same numbers on the major diagonal");
if ( sub == false) System.out.println("No same numbers on the sub-diagonal");
}
}
Exploring Matricies in Java
Problem:
Write a program that prompts the user to enter the length of a square matrix, randomly fills in 0s and 1s into the matrix,
prints the matrix, and finds the rows, columns, and diagonals with all 0s or 1s. Output:
Enter the size for the matrix: 4
1000
0111
0101
0100
No same numbers on a column
No same numbers on a row
No same numbers on the major diagonal
No same numbers on the sub-diagonal

最新文章

  1. Longest Absolute File Path
  2. VS2013 ViewData ViewBag Ajax等关键词报错(当前上下文不存在名称)而且不提示也点不出来,但是可以正常运行,
  3. Hibernate的多表查询,分装到一个新的实体类中的一个方法
  4. HDU 3844 Mining Your Own Business
  5. 记录一款不错的插件fullpage.js
  6. 一个zip压缩类,欢迎吐槽
  7. 神经网络结构在命名实体识别(NER)中的应用
  8. #define is unsafe
  9. 算法提高 金陵十三钗 状压DP
  10. MySQL、MariaDB修改默认字符集
  11. (9)Microsoft office Word 2013版本操作入门_文本框_word排版
  12. 服务器被ddos攻击?分析如何防止DDOS攻击?
  13. 获取jdk支持的编码类型
  14. [Python学习笔记-002] lambda, map, filter and reduce
  15. HDU4292(KB11-H 最大流)
  16. python获得命令行参数的方法
  17. 简单模拟flume
  18. mongodb学习笔记之索引(转)
  19. JQuery为元素添加样式的实现方法
  20. New Concept English there (4)

热门文章

  1. 4.自定义类加载器实现及在tomcat中的应用
  2. 二进制对比工具HexCmp的使用
  3. PTA习题6-8 统计一行文本的单词个数 (15分)
  4. c语言中“ld返回值1退回状态”
  5. SpringCloud 2020.0.4 系列之 Feign
  6. UML图 | 时序图(顺序、序列图)绘制
  7. JVM:类加载与字节码技术-2
  8. Noip模拟47 2021.8.25
  9. csp-s 2021
  10. 2021.8.19考试总结[NOIP模拟44]