JAVA基础知识+基础代码
生活随笔
收集整理的這篇文章主要介紹了
JAVA基础知识+基础代码
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Java基礎知識
異常處理
try {} catch(Exception e) {} void work() throws Exception {} //拋出異常 throw new Exception("輸入的字符不能為空!"); class MyException1 extends Exception //自定義異常類 {String msg = null;public MyException1(String m){this.msg = m;}public String toString(){return "拋出自定義異常:" + msg;} } public class test {public static void main(String[] args){int i = 10;try{if (i < 100){throw new MyException1("輸入值小于100"); //拋出異常}}catch (MyException1 ex){System.out.println(ex); //會調用ex.toString()方法}} }輸入流
import java.util.Scanner; //包含這個包 Scanner in=new Scanner(System.in); //新建流對象 int a=in.nextInt(); //輸入int型 double d=in.nextDouble(); //輸入double型 String s=in.nextLine(); //輸入字符串輸出
System.out.println("輸入有誤"); //自帶換行的輸出 System.out.println(a); //可以輸出int等各種類型的數據 System.out.printf("%d\n",a); //與c語言類似的格式 System.out.print(P[i].id+" "+P[i].name+" "+P[i].age+"\n"); //與cout類似 System.out.print(String.format("%4d\n",c)); //控制格式的輸出 Math.abs(); //絕對值函數,很多數學中的函數都要加Math.才能調用 String ans=String.format("%.6f", T2); //控制格式直接將數轉換為字符串 String[] sub=s.split(" |\n"); //以某些分隔符將一個字符串分隔類型轉換
String str="abc";char[ ] c=str.toCharArray(); //String轉換為char[] char[ ] c={'a','b','c'};String str=new String(c); //char[] 轉換為String String s="34"; int Age=Integer.parseInt(s); //String轉換為int double e=Double.parseDouble(es); //String轉換為double開數組
double[][] A=new double[N+1][N+1]; //二維double 型 int[] B=new int[N]; //以為int型 String[] S=new String[N]; //String 型 int C[100]; //跟C類似 多維就多加幾個[] 創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的JAVA基础知识+基础代码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 龙卷风网络收音机怎么用
- 下一篇: 造梦西游online人物属性介绍