1. 程式人生 > >Android開發學習-1

Android開發學習-1

八種基本型別:

byte;int (2147483647最大);short;long;float  32位;double   64位;char、字元型別;boolean布林值0 1; 
變數必須宣告。並且初始化才能用;

定義陣列int []aa=new int[];

錯誤的:

Public static void main(string[] args)

{

string name; 

system.out.println(name);會報錯、編譯有問題。

}

正確:

Public static void main(string[] args)

{

string name=null; 

system.out.println(name);

}

如果把name放在public static……上面。則不能用、是的,靜態函式只能訪問自己裡面的。

System.out.print(integer .toinarystring(i));轉換成binary

System.out.Print(integer.tohexstring(i));轉換成十六進位制;

數字“0x即(0x)表示十六進位制的數;

Float轉換成int精度損失:int精度比float高;

public class aa {

public static void main(String[] args){

int x=0x7fffffff;

int y=0x7ffffff0;

System.out.println(x-y);

float m=x;

float n=y;

System.out.println(m-n);

}

}

Double轉換成int精度不會損失。說明double精度比int
Char int long floatdouble
強制轉換就在前面加如:intI; (byte) I;

System.out.println((int)i);

控制檯輸入:

 Math函式;

隨機數;

random r=new random();

for(int i=0;i<10;i++)

{

System.out.print(r.nextint(20))//隨機數從了、019 

}

隨機輸出4A——Z的字元

public static void main(String[] args) {

int []aa=new int[26] ;

int j=0;

Random d=new Random();

/* for(int i=0;i<26;i++){

    aa[i]=i+65;

    //System.out.println(aa[i]);

}*/

Random ran=new Random();

for(int i=0;i<4;i++){

System.out.println((charaa[ran.nextInt(26)]);

}

Java運算子。If(){}else{}   witch case;  for ();  do{}while{} ;  while     
 “=”賦值;“==”等於;%取餘數;Break;終止迴圈 continue;跳過本次迴圈++”自加。

Int i=1

System.Out.Printi++);輸出的是1.先輸出再計算;

System.out.Print++i;輸出的是2.先計算再輸出;

自減也是如此

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);

System.out.println ("輸入一個數");

int num=sc.nextInt();

int sum=num*(num+8);

System.out.println(sum);

}//float.double型別的也可以。

一、java不像C中擁有scanf這樣功能強大的函式,大多是通過定義輸入輸出流物件。常用的類有BufferedReaderScanner
例項程式:
1、利用 Scanner 實現從鍵盤讀入integerfloat 型資料

//import java.io.*;
import java.util.*;
public class InputTest{
public static void main(String[] args){
  Scanner in = new Scanner(System.in);    //Scanner
  System.out.println("Please input a float number:");
  float a = in.nextFloat();    //接收float資料
  System.out.println("Please input a string: ");    //這裡試了一下輸入String資料,但中間有空格就不能顯示,Scanner類還不具有這功能
  Scanner str = new Scanner(System.in);
  System.out.println("The string is :" + str.next());
  System.out.println("The float number is: " + a);
  for(int i = 0;i < 4;i++){
   System.out.println("Please input a int number: ");   //for迴圈接收int型資料
   int b = in.nextInt();
   System.out.println("The int number is: " + b);
  }
}
}
2、利用 BufferedReader實現從鍵盤讀入字串並寫進檔案abc.txt

import java.io.*;
public class InputTest{
public static void main(String[] args) throws IOException{
   BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
   BufferedWriter buf2 = new BufferedWriter(new FileWriter("abx.txt"));
    String str = buf.readLine();
    while(!str.equals("exit")){
   buf2.write(str);
   buf2.newLine();
   str = buf.readLine();
  }
  buf.close();
  buf2.close();
}
}

關於JDK1.5 Scanner類的說明

ScannerSDK1.5新增的一個類,可是使用該類建立一個物件.
Scanner reader=new Scanner(System.in);

然後reader物件呼叫下列方法(函式),讀取使用者在命令列輸入的各種資料型別:
next.Byte(),nextDouble(),nextFloat,nextInt(),nextLine(),nextLong(),nextShot()
使用nextLine()方法輸入行中可能包含空格.如果讀取的是一個單詞,則可呼叫.next()方法

3ScannerBufferedReader的區別
在命令列模式下要輸入資料至程式中時,我們可以使用標準輸入串物件System.in.但是,我們並不經常直接使用它,因為System.in提供的 read方法每次只能讀取一個位元組的資料,而我們平時所應用的通常是讀取一個字串或者是一個數字,所以read方法所以提供的功能,對我們來說並沒有太大的用處.
Java SE 6,可以使用Scanner類取得使用者的輸入,Scanner類位於java.util包中,如果你要使用Scanner取得使用者輸入的話,要加上 import java.util.Scanner;這條語句.import的功能是告訴編譯器,你將使用java.util包中的Scanner.
我們來看一個例子:
import java.util.Scanner;
public class TestScanner{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
System.out.println("請輸入一個字串:");
System.out.println("您輸入的字串是:" + scan.next());
}
}

執行上面的程式,你將會看到你輸入的字串將在下面原樣顯示出來.
我們來看看這個程式中每條語句的意思:
new是建立一個物件,程式中new的意思是建立了一個Scanner類的物件scan.但是在建立Scanner類的物件時,需要用System.in 作為它的引數,也可以將Scanner看作是System.in物件的支持者,System.in取得使用者輸入的內容後,交給Scanner來作一些處理.
Scanner類中提供了多個方法:
next():取得一個字串;
nextInt():將取得的字串轉換成int型別的整數;
nextFloat():將取得的字串轉換成float;
nextBoolean():將取得的字串轉換成boolean;

Scanner獲得使用者的輸入非常的方便,但是Scanner取得輸入的依據是空格符,包括空格鍵,Tab鍵和Enter.當按下這其中的任一鍵時,Scanner就會返回下一個輸入當你輸入的內容中間包括空格時,顯然,使用Scanner就不能完整的獲得你輸入的字串.這時候我們可以考慮使用BufferedReader類取得輸入.其實在Java SE 1.4及以前的版本中,尚沒有提供Scanner方法,我們獲得輸入時也是使用BufferReader.
BufferedReader類位於java.io包中,所以要使用這個類,就要引入java.io這個包:import java.io.BufferedReader.
使用BufferedReader物件的readLine()方法必須處理java.io.IOException異常(Exception).
使用BufferedReader來取得輸入,理解起來要複雜得多.但是使用這個方法是固定的,每次使用前先如法炮製就可以了.
BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
String text = buffer.readLine();
readLine()方法會返回使用者在按下Enter鍵之前的所有字元輸入,不包括最後按下的Enter返回字元.
完整的示例程式如下:
import java.io.BufferedReader;
public class TestBufferedReader{
public static void main(String[] args) throws IOException{
BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
System.out.println("請輸入一串字串");
String text = buffer.readLine();
System.out.println("您輸入的字串是:" + text);
}

}
4、如下面的程式所示:class StringTest
{
public static void main(String[] args)
{
System.out.println(args[0]);
System.out.println(args[1]);
System.out.println(args[2]);
}
}

在執行語句即:java + 類名後面輸入內容,即會被args接收,因為args是接收命令列引數的。

Int i=11;

If(i>0){}Else if(i==11){}else{}

則。I==11.不執行;

Switch(i){

Case 1:system.out.println(“”);Break;

Case 2:system.out.println(“”);break;

Default:

}//1 2  i的值;

沒有break則從符合case開始一直執行,執行到最後;

Default不要放前面。

For(int i=0;i<100;i++){}

Scanner sc=new scanner(system.in);

Int aa=sc.nextInt();

For(int i=1,j=aa;i<aa;i++,j--){

}多個條件判斷語句;

While(i<100){

i++;

system.out.println(“好好學習”);

ifi==50{break;}//50次的時候終止迴圈;

}

Scanner sc=new scanner(system.in);

String info=sc.next();

While(info.equals(“yes”)){

System.out.println(“合格了”);

System.out.print(“合格了麼?(y/n)”);

Info=sc.next();

}//判斷輸入的是不是yes

system.Out.print輸出是黑色的字型、System.err.println輸出的是紅色字

srting aaa; 定義一個字元變數

!取反運算;

While(!aaa{

}

當不是aaa的時候執行;