The following is the full source, requires 1.8 version of the JDK, or some new features may appear compatibility issues, remove the comment number to run
Package Hello;
Import Java.util.Scanner;
public class Hello
{
public static void Main (string[] args)
{
TODO auto-generated method stubs
/*
This is the first program
System.out.println ("This is a subtraction calculator");
Scanner in = new Scanner (system.in);
int A;
int b;
System.out.print ("Please enter the first number:");
A = In.nextint ();
System.out.print ("Please enter the first number:");
b = In.nextint ();
SYSTEM.OUT.PRINTLN ("Result:" +a+ "-" +b+ "=" + (A-B) ");
*/
/*
This is a second program.
System.out.println (1.2-1.1);
int foot;
foot=32/(int) 3.0;//coercion type conversion
double inch;
Scanner in = new Scanner (system.in);
Foot = In.nextint ();
inch = In.nextdouble ();
Double meter= (FOOT+INCH/12) *0.3048*100;
SYSTEM.OUT.PRINTLN ((int) meter+ "CM");//coercion type conversion
*/
/*
Double A = 1.0;
Double b = 0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1;
System.out.println (A==B);
System.out.println ("a=" +a+ "; b=" +b);
System.out.println (Math.Abs (A-B) <1e-6);//Judging whether the floating-point number is equal or not, the absolute value of the difference should be calculated and compared with a smaller one, because the floating-point calculation has errors;
Initialization
Scanner in = new Scanner (system.in);
int blance = 0;
while (true)
{
System.out.print ("Please coin:");
int amount = In.nextint ();
Blance = blance + amount;
System.out.println ("Current coin:" +blance+ "Yuan");
System.out.println (amount>=10);
if (blance >= 10)
{
System.out.println ("*************");
System.out.println ("*java Intercity railway line *");
SYSTEM.OUT.PRINTLN ("* No designated SEAT ticket *");
SYSTEM.OUT.PRINTLN ("* Fare $10 *");
System.out.println ("**************");
System.out.println ("Change:" + (blance-10));
blance = 0;
}
}
*/
/*
While loop
System.out.print ("Please enter an integer:");
Scanner in = new Scanner (system.in);
int type = In.nextint ();
Switch (type)
{
Case 1:
SYSTEM.OUT.PRINTLN ("Input 1");
Break
Case 2:
SYSTEM.OUT.PRINTLN ("Input 2");
Break
Default
SYSTEM.OUT.PRINTLN ("Input is not 1 and 2");
Break
}
*/
/*
Do and loop
Scanner in = new Scanner (system.in);
System.out.print ("Please enter a number:");
int number = In.nextint ();
int count = 0;
Do
{
Number = NUMBER/10;
Count = count + 1;
}while (number>0);//The last semicolon cannot be less
System.out.println ("This is a" +count+ "bit number");
*/
/*
Cycle Count
int count = 10;
do {
System.out.println (count);
Count = count-1;
} while (count>0);
SYSTEM.OUT.PRINTLN ("Launch!");
*/
/*
Find average
Scanner in = new Scanner (system.in);
int number = 0;
int sum = 0;
int count = 0;
Do
{
System.out.print ("Please enter an integer (-1 means all the numbers entered):");
Number = In.nextint ();
if (number! =-1)
{
Count = count + 1;
sum = sum + number;
}
}while (number! =-1);
if (count!=0)
System.out.println ("Number of Numbers:" +count+ "; the average is:" + (double) sum/count);
Else
SYSTEM.OUT.PRINTLN ("No valid numbers entered");
*/
/*
Guessing program
Scanner in = new Scanner (system.in);
int number = (int) (Math.random () *100+1);//[0,1)-->[0,100)-->[1,100]
int A;
int count=0;
SYSTEM.OUT.PRINTLN ("Hint:" +number);
System.out.print ("Pro input you guessed the number:");
a =in.nextint ();
count=count+1;
while (A!=number)
{
if (A>number)
System.out.println ("too large");
else if (a<number)
SYSTEM.OUT.PRINTLN ("slightly smaller");
System.out.print ("Pro input you guessed the number:");
a =in.nextint ();
count=count+1;
}
System.out.println ("Congratulations you guessed right, you guessed:" +count+ "Times");
*/
/*
Reverse output
System.out.print ("Please enter a positive integer:");
Scanner in = new Scanner (system.in);
int number = In.nextint ();
int result = 0;
System.out.print ("Reverse output:");
Do
{
int digit = number% 10;
Result = result * + digit;
System.out.print (digit);
Number = NUMBER/10;
}while (number>0);
System.out.println ();
SYSTEM.OUT.PRINTLN ("Reverse order Number:" +result);
*/
/*
Calculate factorial
Scanner in = new Scanner (system.in);
int n = in.nextint ();
int factor = 1;
for (int i=1;i<=n;i++)
{
Factor = factor * I;
}
System.out.println (factor);
*/
/*
Prime number algorithm
The out://label allows the break to jump out of all loops within the loop below the marked position
for (int n=2;n<100;n++)
{
int isprime = 1;
for (int i=2; i<n;i++)
{
if (n% i = = 0)
{
IsPrime = 0;
System.out.println (n+ "not Prime");
break;//termination cycle;
Break out;//label used, continue can also be used
}
}
if (IsPrime = = 1)
System.out.print (n+ "");
Else
System.out.println (n+ "not Prime");
}
*/
/*
Sum 1-1/2+1/3-1/4+1/5.........1/n
Scanner in = new Scanner (system.in);
int n = in.nextint ();
Double sum = 0.0;
int sign = 1;
for (int i=1;i<=n;i++,sign =-sign)//Add an expression using commas
{
sum + = sign*1.0/i;
}
SYSTEM.OUT.PRINTLN (sum);
System.out.printf ("%.2f", sum);//Number control
*/
/*
Greatest common divisor for two numbers
Scanner in = new Scanner (system.in);
int a = In.nextint ();
int b = In.nextint ();
int gcd = 1;
for (int i = 2;i<=a && i<=b; i++)
{
if (a%i = = 0&&b%i = = 0)
GCD = i;
}
System.out.println ("Greatest common divisor is:" +GCD);
*/
/*
Array: Find a number in a group of numbers that is larger than the average and output the average
Scanner in = new Scanner (system.in);
int sum = 0;
int count = 0;
Count = In.nextint ();
if (count>0)
{
int[] numbers = new Int[count];
for (int i=0;i<count;i++)
{
Numbers[i] = In.nextint ();
sum = sum + numbers[i];
}
if (count!=0)
{
Double average = Sum/count;
for (int i = 0; i < count; i++)
{
if (Numbers[i] > average)
System.out.print (numbers[i]+ "");
}
System.out.println ();
System.out.println ("Number of Numbers:" +count+ "; the average is:" + (double) sum/count);
}
Else
SYSTEM.OUT.PRINTLN ("No valid numbers entered");
}
for (int i=0;i<number.length;i++) This is also possible, and better
*/
/*
Code extensibility, changing the length of an array without modifying loops
Scanner in = new Scanner (system.in);
Int[] scores = {87,98,69,54,65,76,87,99,100,103};
System.out.println (scores.length);
for (int i=0;i<scores.length;i++)
{
System.out.println (scores[i]+ "");
}
*/
/*
The array name is just the manager of the array, not the owner of the array.
int a[]={1,2};
int b[]=a;
b[0]=5;
System.out.println (A[0]);//a[0] will also become 5, in fact A and B are managed by the same array of spaces
*/
/*
Array copy
Scanner in = new Scanner (system.in);
int a[]={1,2,3,4,5};
int b[]=new Int[a.length];
for (int i=0;i<b.length;i++)
{
B[i]=a[i];
System.out.print (b[i]+ "");
}
*/
/*
Iterating through an array
Scanner in = new Scanner (system.in);
int[] data = {1,2,34,5,6,7,8,54,5,3,54,35};
int x = In.nextint ();
int loc =-1;
for (int i=0;i<data.length;i++)
{
if (X==data[i])
{
loc = i;
System.out.println (x+ "is the first" + (loc+1) + "a");
}
}
if (loc==-1)
System.out.println (x+ "not in the array");
Find only if there is a certain number
for (int k:data)//This loop is the For-each loop, K is the element in data starting from 0 subscript to the maximum subscript traversal, and the For-each loop cannot change the array value K is read-only
{
if (k==x)
{
System.out.println ("There is this number");
Break
}
}
For-each format: for (< type > < variable >:< array >)
//{
//...
//}
*/
/*
Construction of the first 50 prime number algorithms
Scanner in = new Scanner (system.in);
Int[] primes = new INT[50];
Primes[0] = 2;
int cnt = 1;
main_loop://Marking
for (int x = 3;cnt < 50;x++)
{
for (int i=0; i<cnt; i++)
{
if (x% primes[i]==0)
{
Continue Main_loop;
}
}
primes[cnt++] = x;
}
for (int k:primes)
{
System.out.print (k + "");
}
*/
/*
List of construction primes
Scanner in = new Scanner (system.in);
boolean[] IsPrime = new boolean[100];
for (int i=0;i<isprime.length;i++)
{
Isprime[i]=true;
}
for (int i=2;i<isprime.length;i++)
{
if (Isprime[i])
{
for (int k=2;i*k<isprime.length;k++)
{
Isprime[i*k]=false;
}
}
}
for (int i=2;i<isprime.length;i++)
{
if (Isprime[i])
{
System.out.print (i+ "");
}
}
*/
/*
Tic-Tac-Chess program
Scanner in = new Scanner (system.in);
final int size=3;
int [] [] board=new int[size][size];
Boolean gotresult = false;
int NUMOFX = 0;
int numofo = 0;
Read-in Matrix
for (int i=0; i<size;i++)//equivalent to for (int i=0; i<board.length;i++)
{
for (int j=0; i<size;j++)//equivalent to for (int j=0; i<board[i].length;j++)
{
Board[i][j]=in.nextint ();
}
}
Check row
Check column
Check Diagonal
Output results
*/
The following are the contents of characters and strings
Package Type, integer is the parcel type of int, the package type can have some underlying type without a property tool, for example:
System.out.println (Integer.max_value);
System.out.println (Integer.min_value);
System.out.println (integer.bytes);
System.out.println (integer.size);
The. operator, such as A.length, is an object or a class on the left side of the point, which is the object or some of the services or data that the class can provide, or some action that can be made.
System.out.println (Character.isdigit (' one '));//character.isdigit () can help us determine if it's a number.
System.out.println (Character.tolowercase (' a '));//convert A to lowercase
string variables and array variables are managers, not owners;
String S=new string ("GKFH");//Creates a string object, initializes the object with "GKFH", and first signs the variable s that manages the object, let s manage the object
String s= "GKFH";//This is also possible, and arrays like that, the compiler helps you create an object to give s to manage
Strings can be concatenated using the + sign, and if one side is a string and the other is not a string, the system will represent that part of the string as a string
/*
String read-In
Scanner in = new Scanner (system.in);
String s;
S=in.nextline ();//read a whole line
System.out.println (s);
S=in.next ();//read a continuous word, 1234 56 will read in 1234
System.out.println (s);
System.out.println (s== "Bye");//This comparison method is wrong, even if s= "bye", the result is also false, because S is the object is the manager and the group is similar, if you want to compare the method of using the next sentence
System.out.println (S.equals ("Bye"));//Compare two strings for equality use. Equals () method
*/
String manipulation
String name= "Erwqer";
System.out.println (Name.length ());//Count string length
System.out.println (Name.charat (0));//output name the first character of the string, as well as the array, counting from 0
for (int i=0;i<name.length (); i++)
{
System.out.print (Name.charat (i));
}
System.out.println ();
String cannot be looped with For-each
Take substring
String s1= "0123a56389 kanji";
System.out.println (s1.substring (2));
System.out.println (s1.substring (2,4));//The output is "23", not to the fifth character, but the output to the fifth character before the content, note
int Loc=s1.indexof (' 3 ');
System.out.println (S1.indexof (' 2 '));//Find character
System.out.println (S1.indexof (' 3 ', loc+1));//Find the character, find 3 this character starts from loc+1 this position to find
System.out.println (S1.indexof ("A56"));//Find a substring, get the start position
S1.startswith (t) to determine whether to start with a character or string
S1.endswith (t) determines whether to end with a character or string
S1.trim () Remove the space around the string
S1.replaceall (c1, C2); Replace all C1 in this string with C2
S1.tolowercase () turns the string into uppercase
S1.touppercase () turns the string into lowercase
The above involves changing the contents of the string can not change the S1 itself, but to assign the result to other strings, such as assigning to S2; shape like s2=s1.touppercase (), output s2 get the desired result
Swith-case can be used for strings, but requires JDK version 1.6 and above (1.6 not included)
Switch (s)
//{
Case "QWWE":
//
Break
//
Default
Break
//}
Math class: Abs for Absolute, POW power, random can give the number, round can do some rounding, example:
System.out.println (Math.Abs (-12));//-12 absolute value
System.out.println (Math.Round (10.645));//The result is 11, rounded up
System.out.println (Math.random ());//Give a [0,1] range of numbers
System.out.println (Math.pow (2, 3));//The result is 8, that is, the 2 3-time value is obtained, is floating point, all available decimals
}
};
Java Primer Basic Learning Source, with C or C + + language Foundation can be quickly mastered