java-BASIC Programming (Spiral Matrix & multiplication table)

Source: Internet
Author: User

Package cn.rick.study;

Import Java.io.BufferedReader;
Import Java.io.InputStreamReader;
Import Java.util.Scanner;

/**
*
* @author Rick-bao Date 2014-8-29
*
*/
public class Somebasiccode {

public static void Main (string[] args) {

Multiplicationtable ();//Multiplication table

Createringmatrix ();//Spiral Matrix


System.out.print ("\n\n\t\t\t\t This display made by Rick, please reserve the right!" ");
}

/*
* spiral Matrix: A complex algorithm than the 99 multiplication table author Rick date 2014-8-29 14:31:06
*/
protected static void Createringmatrix () {
while (True) {
String input = ";
System.out.println ("Please enter the number of rows in the matrix (square):");
InputStreamReader inputstreamreader = new InputStreamReader (
system.in);//Get input stream
BufferedReader buff = new BufferedReader (InputStreamReader);
try {
input = Buff.readline ();//Read input stream
} catch (Exception e) {
System.out.println (e.tostring ());
}
Int n;//receives the converted variable
try {
n = integer.parseint (input);
} catch (Exception e) {
System.out.println (" You are not entering numbers (1, 2, 3, etc.)! ");
Continue;
}
System.out.println ("This is an array of spiral matrices with a row number of" + N + ");
int IntA = 1;
Int[][] Array = new int[n][n];//description Two-dimensional array
int intB;
-----algorithm begins
if (n% 2! = 0)
IntB = n/2 + 1;//odd I loop number
Else
IntB = n/2;//even when I loop number

for (int i = 0; i < IntB; i++) {//Outer loop control column
From left to right landscape
for (int j = i; J < N-i; J + +) {
ARRAY[I][J] = IntA;
inta++;
}
Portrait from top to bottom
for (int k = i + 1; k < n-i; k++) {
Array[k][n-i-1] = IntA;
inta++;
}
From right to left landscape
for (int I = n-i-2; I >= i; i--) {
Array[n-i-1][i] = IntA;
inta++;
}
Portrait from Bottom to top
for (int m = n-i-2; M > i; m--) {
Array[m][i] = IntA;
inta++;
}
}

------output Two-dimensional array contents
for (int i = 0; i < n; i++) {
for (int j = 0; J < N; j + +) {
System.out.print ("" + Array[i][j] + "");
}
System.out.println ();
}

-----end end of the algorithm
}

}

/*
* Multiplication Table Author Rick date August 29, 2014 15:33:36
*/
private static void Multiplicationtable () {
Scanner Scanner = new Scanner (system.in);//Get input
int input = 0;//receives the input variable
SYSTEM.OUT.PRINTLN ("Hint: The following for you to print the multiplication table, the same format as the < 99 multiplication table >!") ");
System.out.print ("Please enter the number of lines (press ENTER when finished):");
while (Scanner.hasnext ()) {
try {
input = Scanner.nextint ();
Scanner.close ();
} catch (Exception e) {
System.out.println ("Please enter numbers (such as 1, 2, 3, 5, etc.)!" ");
Break
}
for (int i = 1; i <= input; i++) {
System.out.print ("\ n");
for (int j = 1; J <= I; j + +) {
System.out.print ("" + j + "*" + i + "=" + I * j + "");
}
}
Break
}
}

}

java-BASIC Programming (Spiral Matrix & multiplication table)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.