1. 程式人生 > >一到十五章語法示例

一到十五章語法示例

bre print arrays for stringbu ddn 嵌套 ati 定義

java語法示例

鄭浩
第二章變量
1.數據類型 變量名
double score;
String name;
char sex;
2.變量名=值;
Score=98.5;
name=“張三”;
sex=男
3.數據類型 變量名=值;
double=98.5;
String name=:張三;
char sex;
3.{數據類型}表達式
int b=(int)d;
第三章選擇結構(一)
1.if(條件){
代碼塊//條件執行後要執行的代碼,可以是條語句,也可以是一組語句
}
public class Demo{
public static void main(String[]arge){
//語句1;
//語句2;
if(條件){
//語句3;
}
//語句4;
}}
2.if else選擇結構
語法:if(條件){
//代碼塊1
}else{
//代碼塊
}
public class Demo{
public static void main(String[]arge){
int(score>98){
System.out.println(“老師 說顏值很高獎勵一個mp4”);
}else{
System.out.println("老師說:醜逼去死");
}
}}
3.多重if選擇結構
if(條件1){
//代碼塊1
}else if(條件2){
//代碼塊2
}else{
//代碼塊3
public class Demo{
public static void main(String[]arge){
int(score>98){
System.out.println("帥");
}else if(score>60){
System.out.println("還是帥");
}else{
System.out.println("鄭麽帥");
}
}}
4.if選擇嵌套結構
if(條件1){
if(條件2){
//代碼塊1
}else{
//代碼塊2
}
}else{
//代碼塊3
}
public class Demo{
public static void main(String[]arge){
Scanner input=new Scannner(System.in);
System.out.println("請輸入比賽成(s)績:");
double score=input.nextDouble();
System.out.println("請輸入性別");
String gender=input.next();
if(score<=10){
if(gender.equals(“男”){
System.out.println("進入帥哥總決賽");
}else if(gender.equals("女")){
System.out.println("進入美女總決賽");
}
}else{
System.out.println("醜逼淘汰");}
}}
第四章選擇結構(二)
1.switch選擇結構
switch(表達式){
cose 常量:
//代碼塊1
break;
......
default:
//代碼塊n;
break;
public class Demo{
public static void main(String[]arge){
Scanner input=new Scannner(System.in);
int ss=1//名次;
switch(ss){
case 1:
System.out.println("一個月夏令營");
break;
case 2:
System.out.println("筆記本");
case 3:
System.out.println(" 移動硬盤");
break;
default;
System.out.println("去死 還想要獎勵?");
}
}
第五章循環結構(一)
1.while循環結構
while(循環條件){
//循環操作
}
int i=1;
while(i<100){
System.out.println("醜逼淘汰");
i=i+1;
2.do while循環
do{
//循環操作
}循環條件}
}
int i=1;
do{
System.out.println("醜逼淘汰");
i++;
}while(i<=100);
}
第六章循環結構(二)
1.for循環結構
for(表達式1;表達式2;表達式3){
//循環體
}
public class Demo{
public static void main(String[]arge){
for(int i=0;i<100;i++){
System.out.println("醜逼淘汰");
第八章數組
1.聲明數組
數據類型[]數組名;
double height[];//儲存學員身高,類型為double
String names;//儲存學員姓名,類型為String
2.分配空間
數據類型[]數組名=new 數據類型{數組長度};
int scores{}=new int[30];//儲存30個學員成績
3.賦值
數組名[下標值]
scores[0]=89;
scores[1]=88;
scores[]2=87;
數據類型[] 數組名={值1,值2,值3.....值n};
int[]scores={30,23,45.,61.,21};或者int[]scores=new int[]{30,23,45.,61.,21};
4.數組排序
Arrays.sort(數組名);
第九章循環結構進階
1.二重循環結構
while(循環條件1){
//循環操作1
while(循環條件2){
//循環操作2;
}
}
//do-while與do-while循環嵌套
do{
//循環操作1
do{
//循環操作2
}while(循環條件2);
}while(循環條件1);
//for與for循環嵌套
for(循環條件1){
//循環操作1
for(循環條件2){
//循環操作2
}
}
//while與for循環嵌套
while(循環條件1){
//循環操作1
for(循環條件2){
//循環操作2
第十一章(類和對象)

public class<類名>
//定義屬性部分
屬性1的類型 屬性1;
屬性2的類型 屬性2;
......
屬性3的類型 屬性3;
//定義方法部分
方法1;
方法2;
......
方法n;
如何定義類
public class 類名{
}
編譯類的方法
訪問修飾符 返回值類型 方法名(){
//方法體
}
類名 對象名=new 類名();
School center =new School();
對象名.屬性//引用對象的屬性
center.name=“北京中心”;//給name屬性賦值
對象名.方法名()//引用對象的方法
center.show();
第十二章類的無參方法
1.定義類的方法
public 返回值類型方法名(){
//方法的主體
}
public void show();
return 表達式;
2.方法調用
對象名.方法名();
public class SP {
String color="黃色";
public void show(){
System.out.println("正在0.1/秒的速度向前奔跑");


}
public String hen(){
String ball="球";
return ball;
}
public String hua(){
return color;
}
public String ha(){
return "這是一個"+hua()+"的玩具獅子";
public class SPP {
public static void main(String[] args) {
SP zheng=new SP();
String hao=zheng.ha();
System.out.println(hao);
zheng.show();
System.out.println("搶到一個"+zheng.hen());
3.javaDoc註釋
/** */
//
/* */

第十四章 帶參數的方法
1.定義帶參方法
<訪問修飾符> 返回值類型<方法名>(<參數列表>){
//方法的主體
public void addName(String name){

2.調用帶參方法
對象名方法名(參數1,參數2,...,參數n)
}
st.addName(newName)//調用帶參並穿參
3.聲明包
package 包名;

package zheng1;
import java.util.Scanner;
public class QQ2 {
}
4.導入包
import 包名.類名;
java .util
import包名,*;
第十五章字符串
1.用字符串
//創建一個字符串的對象“hello”
String s=“hello world”;
//創建一個空字符串
String s=new String();
或者
//創建一個空字符串對象
String s=new String(“鄭麽帥”);
字符串1.length();
if(pwd.length()>=6){ //判斷密碼長度
字符串1.equals(字符串 2);
if(uname.equals(“TOM”)&&pwd.equals(“123456”)){
}
字符串1.equalsIgnoreCase(字符串 2);
if(uname.equalsIgnoreCase(“TOM”)&&pwd.equalsIgnoreCase(“123456”)){
}
字符串.toLowerCase()
返回字符串的小寫形式
字符串.toUpperCase()
返回字符串的大寫形式
if(uname.toLowerCase.equals().equals((“TOM”).toLowerCase))
&& pwd.toLowerCase().equals((“123456”).toUpperCase()))
}
字符串1.concat(字符串2);
s.concat(name);

2.字符串的提取與查詢

1.indexOf()方法
String s“青春無悔”
int index=s.indexOf(“春”);
2.lastlndexOf()方法
String s“青春無悔無悔青春”
int index=s.lastlndexOf(“春青春”);
3.substring(int index)方法
String s=“青春無悔”
String result=s.substring(1);
4..substring(int beginindex,intendindex)方法
String s=“青春無悔無悔青春”
String result=s.substring(2,6);
5.time()方法

3.StringBuffer類
字符串1.toString();
String s1=sb2.tostring();
//轉換為String類
字符串1.append(字符串2);
StringBuffer sb=new StringBuffer(“青春無悔”);
字符串1.insert(位置,參數);
str.insert(i“,”)

一到十五章語法示例