淺析Java常用API(Scanner,Random)匿名對象
API:即Application programming Interface,應(yīng)用編程接口。
Java中封裝了許許多多的API供用戶使用,Scanner與Random便是其中之一,API實際就是類,已經(jīng)封裝好了Scanner類,Random類,我們只需按照其語法編寫即可,無需了解其根本源代碼
Scanner類:
1.使用Scanner類需導(dǎo)入其所在包,import java.util.Scanner或import java.util.*(前者是導(dǎo)入util中的Scanner類,后者是導(dǎo)入util中的所有類)
2.創(chuàng)建對象 Scanner 對象名=new Scanner(System.in) //System.in代表來源是鍵盤(大多數(shù)情況)
3.使用對象并調(diào)用其方法 對象名.nextxx() //根據(jù)接受類型不同,調(diào)用不同方法
import java.util.Scanner;
//從鍵盤輸入三個數(shù),輸出最大值
public class ScannerDemo {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int num=s.nextInt();
System.out.println(num);
String str=s.next();
System.out.println(str);
int a=s.nextInt();
int b=s.nextInt();
int c=s.nextInt();
int max=a>b?a:b;
int endmax=c>max?c:max;
System.out.println(endmax);
}
}
整形---nextInt(),字符串--next(),浮點型--nextFloat()......
Random類
1.導(dǎo)包 import java.util.Random或import java.util.*(前者是導(dǎo)入util中的Random類,后者是導(dǎo)入util中的所有類)
2.創(chuàng)建 Random r=new Random()
3.使用
import java.util.Random;
import java.util.Scanner;
//猜隨機數(shù),只有五次機會
public class DemoRandom {
public static void main(String[] args) {
Random r=new Random();
Scanner s=new Scanner(System.in);
int res=r.nextInt(100);//[0,100)
System.out.println(res);
int i=0;
while (i<5){
System.out.println("請輸入猜的數(shù)字,我們幫你判斷");
int num=s.nextInt();
if(num>res){
System.out.println("大了哦");
i++;
continue;}
else if(num<res){
System.out.println("小了");
i++;
continue;
}
else {
System.out.println("猜對了");
i++;
break;
}
}
if(i==5)
System.out.println("你的次數(shù)用完了");
else
System.out.println("恭喜,你用了"+i+"次");
}
}
對象名.方法()//r.nextInt()即隨機產(chǎn)生一個整形范圍的數(shù)字------------------r.next(n)//隨機產(chǎn)生一個[0,n)之間的數(shù)值(左閉右開)
匿名對象:即無需給對象起名字,只能使用一次,下次再使用又是一個新的匿名對象,可作函數(shù)的形參,函數(shù)返回值(new 類名())
import java.util.Scanner;
/*匿名對象作形參,返回值
*/
public class DemoAnonymous {
public static void main(String[] args) {
meth(new Scanner(System.in));
Scanner s=meth2();
int num=s.nextInt();
System.out.println(num);
}
public static void meth(Scanner sc){
int num=sc.nextInt();
System.out.println(num);
}
public static Scanner meth2(){
return new Scanner(System.in);
}
}
7行為匿名對象作為形參,17行返回一個匿名對象。
以上所述是小編給大家介紹的Java常用API(Scanner,Random)匿名對象詳解整合,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
在Spring Boot2中使用CompletableFuture的方法教程
這篇文章主要給大家介紹了關(guān)于在Spring Boot2中使用CompletableFuture的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧2019-01-01
Java線程池ThreadPoolExecutor的使用及其原理詳細解讀
這篇文章主要介紹了Java線程池ThreadPoolExecutor的使用及其原理詳細解讀,線程池是一種多線程處理形式,處理過程中將任務(wù)添加到隊列,然后在創(chuàng)建線程后自動啟動這些任務(wù),線程池線程都是后臺線程,需要的朋友可以參考下2023-12-12
使用springboot aop來實現(xiàn)讀寫分離和事物配置
這篇文章主要介紹了使用springboot aop來實現(xiàn)讀寫分離和事物配置,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04
Spring?Boot攔截器和監(jiān)聽器實現(xiàn)對請求和響應(yīng)處理實戰(zhàn)
這篇文章主要介紹了Spring?Boot攔截器和監(jiān)聽器實現(xiàn)對請求和響應(yīng)處理實戰(zhàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-06-06
java連接SQL Server數(shù)據(jù)庫的方法
這篇文章主要為大家詳細介紹了java連接SQL Server數(shù)據(jù)庫的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10

