java新手筆記5

來源:互聯網
上載者:User

標籤:

1.進位轉換

/*企業發放的獎金根據利潤提成。利潤(I)低於或等於10萬元時,獎金可提10%;利潤高於10萬元,低於20萬元時,低於10萬元的部分按10%提成,高於10萬元的部分,可提成7.5%;20萬到40萬之間時,高於20萬元的部分,可提成5%;40萬到60萬之間時高於40萬元的部分,可提成3%;60萬到100萬之間時,高於60萬元的部分,可提成1.5%,高於100萬元時,超過100萬元的部分按1%提成,從鍵盤輸入當月利潤I,求應發放獎金總數?*/import java.util.Scanner;public class Demo1 {public static void main(String args[]) {/*System.out.println("二進位:4 " +Integer.toBinaryString(4));System.out.println("八進位:16 " +Integer.toOctalString(16)); System.out.println("十六進位:10 " + Integer.toHexString(10));int a = 12328;int b,c,d,e,f;//取個位b = a % 10;//c = a % 100 / 10;c = a / 10 % 10;d = a / 100 % 10;e = a / 1000 % 10;f = a / 10000;System.out.println("b = " + b + "\tc = " + c);System.out.println("d = " + d + "\te = " + e + "\tf = " + f);int a = 462,b,c,d,e,f;if(a < 10){           System.out.println("1位元..."); } else if ( a < 100 ) {   b = a % 10;   c = a / 10 % 10;           System.out.println("2位元..." + b + c);} else if ( a < 1000 ) {   b = a % 10;   c = a / 10 % 10;   d = a / 100 % 10;           System.out.println("3位元..." + b + c + d); } else if ( a < 10000 ) {   b = a % 10;   c = a / 10 % 10;   d = a / 100 % 10;   e = a / 1000 % 10;           System.out.println("4位元..." + b + c + d + e); } else {   b = a % 10;   c = a / 10 % 10;   d = a / 100 % 10;   e = a / 1000 % 10;   f = a / 10000;           System.out.println("5位元..." + b + c + d + e + f); }Scanner scan = new Scanner(System.in);System.out.println("請輸入你資訊姓名、年齡、成績:");String name = scan.next();//擷取字串int age = scan.nextInt();//擷取整型數字double score = scan.nextDouble();System.out.println("輸入的資訊如下:");System.out.println("姓名:" + name + " 年齡:" + age + " 成績:" + score);Scanner scan = new Scanner(System.in);System.out.println("請輸入利潤:");double I = scan.nextDouble();        double bound = 0;//獎金方案if(I <= 10) {bound = I * 0.1;} else if( I < 20 ) {bound = 10 * 0.1 + (I - 10) * 0.075; // 35  10   10 0.075} else if( I < 40 ) {bound = 10 * 0.1 + 10 * 0.075 + (I - 20) * 0.05; } else if( I < 60 ) {bound = 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + (I - 40) * 0.03; } else if( I < 100 ) {bound = 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + 20 * 0.03 + (I - 60) * 0.015; } else {bound = 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + 20 * 0.03 + 40 * 0.015 + (I - 100) * 0.01; }System.out.println("獎金:" + bound);//String s = NULL;*/int a = 1, b = 6, c = 9;if(a > b){if(a > c){if(b > c){ System.out.println("@:" + a + b + c);} else {System.out.println(" :" + a + c + b);}} else {                  System.out.println(" :" + c + a + b);}} else {if(b > c) {if( a > c){   System.out.println(" :" + b + a + c);} else {                   System.out.println(" :" + b + c + a);}} else {               System.out.println("** :" + c + b + a);}}int year = 2012;boolean isLeapYear = year % 400 == 0 || year % 4 == 0 && year % 100 != 0;}}

 2.封裝類

//聲明類  類封裝public class Student {    String name;char sex;private int age;//屬性私人int sno;    //提供訪問屬性的方法  通過方法防止非法資料存入屬性void setAge() {age = 30;if(age > 150 || age < 0){           System.out.println("年齡不合法...");   age = 0;}         }int getAge() {        return age; //返回age屬性值}void study() {System.out.println("學習!...");}//方法void speak() {System.out.println("大家好!...");}}

 3.測試類別

//聲明類public class StudentTest {public static void main(String[] args){Student o = new Student();//建立自訂對象//o 學生對象   Student 類         int a = 20; System.out.println("o.name = " + o.name);o.speak();//方法調用o.study();o.name = "張三";o.sex = ‘男‘;//o.age = -20;o.setAge();o.sno = 180;o.name = "張飛";System.out.println("o.name = " + o.name);System.out.println("o.sex = " + o.sex);//System.out.println("o.age = " + o.age);System.out.println("o.getAge() = " + o.getAge());System.out.println("o.sno = " + o.sno);}}

 4.測試類別1

//聲明類public class StudentTest1 {public static void main(String[] args){Student s = null;//聲明對象的引用//s.study();  new s = new Student();//new 建立對象 執行個體化對象s.name = "李四";System.out.println("s.name = " + s.name);s.study();Student s2 = new Student();s2.name = "張三";System.out.println("s2.name = " + s2.name);System.out.println("s.name = " + s.name);s2.study();}}

 5.類說明

//聲明類public class Obj {    //狀態  屬性 執行個體變數String name;char sex;int age;//行為 方法  動作void speak() {System.out.println("大家好!...");}}//////////////////////////////////class ObjTest {public static void main(String[] args){Obj o = new Obj();//建立自訂對象//Random ran = new Random();System.out.println("o.name = " + o.name);o.speak();o.name = "張三";o.sex = ‘男‘;o.age = 20;o.name = "張飛";System.out.println("o.name = " + o.name);System.out.println("o.sex = " + o.sex);System.out.println("o.age = " + o.age);}}

 6.類方法

//聲明類public class MethodTest1 {    int a = 10;//與對象關聯    //方法  無參數 無傳回值void method1() {System.out.println("call method1()");}    //傳回值類型 add  方法名 ()傳入參數 void add (int a, int b) {        int sum  = a + b;        System.out.println("sum = " +  sum);}    //形參列表 資料類型 變數名double caluSal(int job,int house,int bound) {//System.out.println("sum = " +  job + house + bound);      return job + house + bound;//  return返回計算結果}public static void main(String[] args){   MethodTest1 mt = new MethodTest1();       int b = mt.a;   System.out.println("b = " + b);   mt.method1();   mt.add(10, 6);//調用時 參數類型 個數與聲明的方法匹配   double salary = mt.caluSal(8000,5000,6000);   System.out.println("salary = " + salary);   //結果返回還需要處理   double result = salary * 0.05;System.out.println("result = " + result);}}

 

java新手筆記5

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.