Java Programming Classic 300 Examples of learning notes

Source: Internet
Author: User
Tags decrypt

First, the basic article
1. Do not use the third party to achieve the exchange of two variable values:
Package basic;

Import Java.util.Scanner;

public class Exchangevalue {
public static void Main (string[] args) {
Scanner scan = new Scanner (system.in);
System.out.println ("Please enter the value of variable A:");
Long a = Scan.nextlong ();
System.out.println ("Please enter the value of variable B:");
Long B = Scan.nextlong ();
System.out.println ("a=" + A + "\tb=" + b);
SYSTEM.OUT.PRINTLN ("variable exchange ... \ t");
A = a ^ b;
b = a ^ b;
A = a ^ b;
System.out.println ("a=" + A + "\tb=" + b);
}
}

2. Use XOR or add and decrypt:
Package basic;

Import Java.util.Scanner;

public class Encryption {
public static void Main (string[] args) {
Scanner scan=new Scanner (system.in);
System.out.println ("Please enter an English string or decrypt the string");
String Password=scan.nextline ();
Char[] Array=password.tochararray ();
for (int i=0;i<array.length;++i) {
Array[i]= (char) (array[i]^20000);
}
SYSTEM.OUT.PRINTLN ("Encrypt or decrypt the results as follows:");
System.out.println (new String (array));
}
}

3. Summing with BigDecimal:
Package basic;

Import Java.math.BigDecimal;

public class Factorial {
public static void Main (string[] args) {
BigDecimal sum=new BigDecimal (0.0);
BigDecimal factorial=new BigDecimal (1.0);
int i=1;
while (i<=10) {
Sum=sum.add (factorial);
++i;
Factorial=factorial.multiply (New BigDecimal (1.0/i));
}
System.out.println ("1+1/2!+1/3!+1/4!.... +1/10!=" +sum);
}
}

4.for Loop Print Hollow Diamond:
Package basic;

public class Diamond {
public static void Main (string[] args) {
Printdiamond (6);
}

public static void Printdiamond (int size) {
if (size% 2 = = 0) {
size++;
}
for (int i = 0; i < SIZE/2 + 1; ++i) {
for (int j = SIZE/2 + 1; j > i + 1; j--) {
System.out.print ("");
}
for (int j = 0; J < 2 * i + 1; j + +) {
if (j = = 0 | | j = 2 * i) {
System.out.print ("*");
} else {
System.out.print ("");
}
}
System.out.println ("");
}
for (int i = SIZE/2 + 1; i < size; i++) {
for (int j = 0; J < I-size/2; J + +) {
System.out.print ("");
}
for (int j = 0; J < 2 * size-1-2 * i; j + +) {
if (j = = 0 | | j = 2 * (Size-i-1)) {
System.out.print ("*");
} else {
System.out.print ("");
}
}
System.out.println ("");
}
}
}

5. Monkey Peach problem: A pile of peaches, 5 monkeys successively to, each time evenly divided 5 copies, throw away the extra one, take their share. Ask at least a few peaches, the last one to get a few peaches.
Package basic;

public class Monkeypeach {
public static void Main (string[] args) {
int n = 1;
int m = 0;
int flag = 1;
int monkeynum = 5;
while (true) {
flag = 1;
m = monkeynum * n + 1;
for (int i = monkeynum; I >= 1; i--) {
If ((M% (monkeyNum-1) = = 0)) {
m = m/(monkeyNum-1) * monkeynum + 1;
flag++;
} else {
Break
}
}
if (flag = = Monkeynum)
Break
n++;
}
SYSTEM.OUT.PRINTLN ("Total peaches:" + M);
System.out.println ("The fifth monkey gets the number of peaches:" + N);
}
}

6. Joseph Question:
Package basic;

Import Javax.swing.JOptionPane;

public class Josephus {
public static void Main (string[] args) {
String s;
int N, K, M, N1;
s = Joptionpane.showinputdialog ("Please enter the number of people:");
n = integer.parseint (s);
N1 = n + 1;
s = Joptionpane.showinputdialog ("Please enter the number of the person who started the Count:");
K = Integer.parseint (s);
s = Joptionpane.showinputdialog ("Please enter the death number:");
m = Integer.parseint (s);
int a[] = new Int[n + 1];
A[0] = 0;
System.out.println ("Outgoing person's number:");
for (int i = 1; i < a.length; i++) {
A[i] = 1;
}
for (int i = 1; I <= m; i++) {
if (n = = 1)
Break
else if (i = = m) {
n--;
i = 0;
A[k] = 0;
System.out.print (k + "");
}
do {
k++;
k = k% N1;
} while (A[k]! = 1);
}
System.out.println ("");
SYSTEM.OUT.PRINTLN ("Survivor Number:" + K);
}
}

7. Sudoku Problem:
Package basic;

Import Java.util.Scanner;

public class Sudoku {
public static void Main (string[] args) {
Scanner s = new Scanner (system.in);
System.out.println ("Please enter the number of Sudoku dimensions to be computed (odd numbers greater than 1):");
int x = S.nextint ();
int h = 0;
int L = X/2;
Int[][] A = new int[x][x];
for (int i = 1; I <= x * x; i++) {
A[h][l] = i;
h--;
l++;
if (H < 0 && l >= x) {
H + = 2;
l--;
} else if (H < 0) {
h = x-1;
} else if (l >= x) {
L = 0;
} else if (A[h][l] > 0) {
H + = 2;
l--;
}
}
for (int i = 0; i < x; i++) {
for (int j = 0; J < x; j + +)
System.out.print (A[i][j] + "");
System.out.println ();
}
}
}

8. Convert the number format to a currency string:
Package basic;

Import Java.text.NumberFormat;
Import Java.util.Locale;
Import Java.util.Scanner;

public class Currencyformat {
public static void Main (string[] args) {
Scanner scan = new Scanner (system.in);
System.out.println ("Please enter a currency number:");
Double number = scan.nextdouble ();
NumberFormat format = numberformat.getcurrencyinstance (Locale.china);
System.out.println ("Locale.china:" + format.format (number));
Format = numberformat.getcurrencyinstance (locale.us);
System.out.println ("locale.us:" + format.format (number));
Format = numberformat.getcurrencyinstance (locale.uk);
System.out.println ("locale.uk:" + format.format (number));
}
}

9. Verify the IP address:

Package basic;

Import Java.util.Scanner;

public class IPAddress {
public static void Main (string[] args) {
Scanner sc = new Scanner (system.in);
System.out.println ("Please enter the IP address you want to verify:");
String text = Sc.nextline ();
String info = matches (text);
SYSTEM.OUT.PRINTLN (info);
}

public static string matches (string text) {
if (text = null &&!text.isempty ()) {
String regex = "^ (1\\d{2}|2[0-4]\\d|25[0-5]|[ 1-9]\\d| [1-9]) \\."
+ "(1\\d{2}|2[0-4]\\d|25[0-5]| [1-9]\\d|\\d] \ \. "
+ "(1\\d{2}|2[0-4]\\d|25[0-5]| [1-9]\\d|\\d] \ \. "
+ "(1\\d{2}|2[0-4]\\d|25[0-5]| [1-9]\\d|\\d) $ ";
if (Text.matches (regex)) {
Return text + "is a legitimate IP address! ";
} else {
Return text + "is not a valid IP address! ";
}
}
Return "Please enter the IP address you want to verify!" ";
}
}

Java Programming Classic 300 Examples of learning notes

Related Article

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.