Review of small Java algorithms

Source: Internet
Author: User

Package com.bshinfo.bm.util;

Import Java.util.HashMap;
Import Java.util.Iterator;
Import Java.util.Map;
Import Java.util.Scanner;

public class Test {

Rhombic
public void Test1 () {
for (int i = 0; i < 5; i++) {//Line number
for (int j = 0; J <5-i-1; J + +) {//spaces
System.out.print ("");
}
for (int k = 0; k < i+1)-1; k++) {//asterisk
System.out.print ("*");
}
System.out.println ("");
}
for (int i = 5; i > 0; i--) {//Line number
for (Int J =5-i+1; J >0; j--) {//spaces
System.out.print ("");
}
for (int k = i-1)-1; k >0; k--) {//asterisk
System.out.print ("*");
}
System.out.println ("");
}
}
Rhombic
public void test2 (int n) {
for (int i = 0; i < n-1; i++) {
for (int x = i + 1; x < n; + +) {
System.out.print ("");
}
for (int y = 0; y < (i + 1) * 2-1; y++) {
System.out.print ("*");
}
System.out.println ();
}
for (int i = 0; i < n; i++) {
for (int x = 0; x < i; + +) {
System.out.print ("");
}
for (int y = i; y < 2 * n-i-1; y++) {
System.out.print ("*");
}
System.out.println ();
}
}

Triangle
public void Test3 () {
for (int i = 0; i < 5; i++) {
for (int j = 0; J < 5-i-1; J + +) {
System.out.print ("");
}
for (int j = 0; J < i+1; J + +) {
System.out.print ("*");
}
System.out.println ("");
}
}

99 Multiplication Table
public void Test4 () {

for (int i = 1; i < i++) {
for (int j = 1; j < I+1; J + +) {
System.out.print (j+ "*" +i+ "=" + (I*J) + "");
}
System.out.println ("");
}
}
Number of occurrences of characters in the statistics string
public void Test5 () {
String str = "Asdfsadfewrtewfxzfagres";
map<string,integer> map =new hashmap<string,integer> ();
int count = Str.length ();
for (int i = 0; i < count; i++) {
if (Map.containskey (str.substring (i, i+1))) {
int temp = Map.get (str.substring (i, i+1));
temp++;
Map.put (Str.substring (i, i+1), temp);
}else{
Map.put (Str.substring (i, i+1), 1);
}
}
For (String Key:map.keySet ()) {
int value = Map.get (key);
System.out.println ("character" +key+ "The number of occurrences is" +value);
}
}
Determine if the number of daffodils is entered
public void Test6 () {
Scanner sc = new Scanner (system.in);
String str = sc.nextline ();
int count = Str.length ();
if (count%2!=0) {
int temp = 0;
for (int i = 0; i < COUNT/2; i++) {
int temp1 = Integer.parseint (str.substring (i, i+1));
int temp2 = Integer.parseint (str.substring (count-i-1, count-i));
if (TEMP1!=TEMP2) {
SYSTEM.OUT.PRINTLN (str + "not the number of daffodils! ");
Return
}else{
temp++;
}
}
if (temp = = (COUNT/2)) {
SYSTEM.OUT.PRINTLN (str + "is the number of daffodils! ");
}
}

}
public static void Main (string[] args) {

Test test = new test ();
Test.test1 ();
Test.test2 (5);
Test.test3 ();
Test.test4 ();
Test.test5 ();
Test.test6 ();
}

}

Review of small Java algorithms

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.