Java开发语句和代码块模板
生活随笔
收集整理的這篇文章主要介紹了
Java开发语句和代码块模板
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1 輸出
System.out.println("Hello World");System.out.println("First Number = " + firstNumber);
System.out.println("args[" + i + "]: " + args[i]);
2 變量定義
int firstNumber;long firstNumber;
firstNumber = 10;
double thirdNumber = 10.2d;
char ch = 'a';
boolean value = true;
String greeting = "Hello world!";
char[] helloArray = { 'h', 'e', 'l', 'l', 'o', '.'};
int[] array1;
array1 = newint[100];
double[] array2 = newdouble[10];
long[] array3= {10L, 23L, 30L, 11L};
Date date = new Date();
SimpleDateFormat ft = new SimpleDateFormat ("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
3 流程控制語句
if (score < 50) {? ? ? ? ? ? System.out.println("You are not pass");
? ? ? ? }
? ? ? ? else if (score >= 50 && score < 80) {
? ? ? ? ? ? System.out.println("You are pass");
? ? ? ? }
====
if (num1 > num2)
? ? ? result = num1;
? ?else
? ? ? result = num2;
====
? ? ? ? switch (age) {
? ? ? ? case 18:
? ? ? ? ? ? System.out.println("You are 18 year old");
? ? ? ? ? ? break;
? ? ? ? case 20:
? ? ? ? ? ? System.out.println("You are 20 year old");
? ? ? ? ? ? break;
? ? ? ? default:
? ? ? ? ? ? System.out.println("You are not 18 or 20 year old");
? ? ? ? }
====
? ? ? ? for(intvalue = 0; value < 10; value = value + 3) {?
? ? ? ? ? ? System.out.println("Step ="+ step + " ?value = "+ value);
? ? ? ? ? ? step = step + 1;?
? ? ? ? }
? ? ? ? for (int index = 0; index < myArray.length; index++) {
? ? ? ? ? ? System.out.println("Element " + index + " = " + myArray[index]);
? ? ? ? }
====
? ? ? ? while( value < 10) ?{
? ? ? ? ? ? System.out.println("Value = "+ value);
? ? ? ? ? ? value = value + 2;
? ? ? ? }
? ? }
? ? ? ? do {
? ? ? ? ? ? System.out.println("Value = " + value);
? ? ? ? ? ? value = value + 3;
? ? ? ? } while (value < 10);
4 類模板
public class MyFirstJavaProgram {? ? public static void main(String []args) {
? ? ? ?System.out.println("Hello World");
? ? }
}?
====
package com.xxx;
public class Person {
? ? public String name;
? ? public Person(String persionName) {
? ? ? ? this.name = persionName;
? ? }
? ? public String getName() {
? ? ? ? return this.name;
? ? }
}
====
package com.xxx;
public class FieldSample {
? ? public static int MY_STATIC_FIELD = 100;
? ? public String myValue;
? ? public FieldSample(String myValue) ?{
? ? ? ? this.myValue= myValue;
? ? }
}
====
package com.xxx;
public class MethodSample {
? ? public String text = "Some text";
? ? public MethodSample() ?{
? ? }
? ? public String getText() {
? ? ? ? return this.text;
? ? }
? ? public void setText(String text) {
? ? ? ? this.text = text;
? ? }
? ? public static int sum(int a, int b, int c) {
? ? ? ? int d = ?a + b + c;
? ? ? ? return d;
? ? }
}
====
package com.xxx;
public class FinalFieldExample {
? ? public final int myValue = 100;
? ? public static final long MY_LONG_VALUE = 1234L;
}
====
package com.xxx;
public class Animal {
? ? public Animal() {
? ? }
? ? public void move() {
? ? ? ? System.out.println("Move ...!");
? ? }
? ? public void say() {
? ? ? ? System.out.println("<nothing>");
? ? }
}
Cat.java
package com.xxx;
public class Cat extends Animal {
? ? public void say() {
? ? ? ? System.out.println("I am Cat");
? ? }
}
====
class MyClass {
? ?int x;
? ?
? ?MyClass() {
? ? ? x = 10;
? ?}
}
====
5?
6 導入
import java.util.*;import java.text.*;
import java.util.Date;
import java.io.*;
import java.io.BufferedReader;?
import java.io.File;?
import java.io.FileInputStream;?
import java.io.FileReader;?
import java.io.IOException;?
import java.io.InputStream;?
import java.io.InputStreamReader;?
import java.io.RandomAccessFile;?
import java.io.Reader;?
import java.util.regex.Matcher;
import java.util.regex.Pattern;
7 正則
? String line = "This order was placed for QT3000! OK?";? String pattern = "(.*)(\d+)(.*)";
? // Create a Pattern object
? Pattern r = Pattern.compile(pattern);
? // Now create matcher object.
? Matcher m = r.matcher(line);
8 簡單函數
/** Return the max between two numbers */public static int max(int num1, int num2) {
? ?int result;
? ?if (num1 > num2)
? ? ? result = num1;
? ?else
? ? ? result = num2;
? ?return result;?
}
====
/** Method to swap two variables */
public static void swap(int n1, int n2) {
? ?int temp = n1;
? ?n1 = n2;
? ?n2 = temp;
}
====
9 文件
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));str = br.readLine();
File f = new File("C:/java/hello");
InputStream f = new FileInputStream(f);
File f = new File("C:/java/hello");
OutputStream f = new FileOutputStream(f);
10 簡單小程序
import java.io.*;
public class fileStreamTest{
? ?public static void main(String args[]){
? ?? ?try{
? ? ? byte bWrite [] = {11,21,3,40,5};
? ? ? OutputStream os = new FileOutputStream("test.txt");
? ? ? for(int x=0; x < bWrite.length ; x++){
? ? ? ? ?os.write( bWrite[x] ); // writes the bytes
? ? ? }
? ? ? os.close();
? ? ?
? ? ? InputStream is = new FileInputStream("test.txt");
? ? ? int size = is.available();
? ? ? for(int i=0; i< size; i++){
? ? ? ? ?System.out.print((char)is.read() + " ?");
? ? ? }
? ? ? is.close();
? ?}catch(IOException e){
? ? ? System.out.print("Exception");
? ?}
? ?}
}
====
總結
以上是生活随笔為你收集整理的Java开发语句和代码块模板的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: GDAL学习总结
- 下一篇: 图解VC++绘制数学曲线