第16天学习Java的笔记(标准类,Scanner)
生活随笔
收集整理的這篇文章主要介紹了
第16天学习Java的笔记(标准类,Scanner)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
還有34天
一、定義一個標準的類
package Demo1601;/** 一個標準的類通常要擁有下面四個組成部分:** 1.所有的成員變量都用private關鍵字修飾* 2.為每個成員變量編寫一對兒Getter/Setter方法* 3.編寫一個無參數的構造方法* 4.編寫一個全參數的構造方法(這時的set方法并不是沒有作用,可以用來后期改變參數)** 這樣標準的類也叫做Java Bean* */public class Student {private String name;private int age;public Student() {}public Student(String name, int age) {this.name = name;this.age = age;}public void setName(String name) {this.name = name;}public String getName() {return name;}public void setAge(int age) {this.age = age;}public int getAge() {return age;} } package Demo1601;public class Demo01Student {public static void main(String[] args) {Student stu1 = new Student();stu1.setAge(15);stu1.setName("hh");System.out.println("這是一個無參數的構造方法,我是" + stu1.getName() + ",今年" + stu1.getAge() + "歲了。");Student stu2 = new Student("xx",16);System.out.println("這是一個全參數構造方法,我是" + stu2.getName() + "今年" + stu2.getAge() + "歲了。");} }API概述及其使用步驟(Application Program interface)
二、Scanner的使用步驟
package Demo1602;import java.util.Scanner;//1.導包/* Scanner類的功能,可以實現鍵盤輸入數據,到程序當中。引用類型的一般使用步驟:1.導包 import 包路徑.類名稱; 如果需要使用的目標類和當前類位于同一包下,則可以省略導包語句不寫。 只有java.lang包下的內容不需要導包,其他的包都需要import語句2.創建 類名稱 對象名 = new 類名稱();3.使用 對象名.成員方法名();獲取鍵盤輸入的一個數字:int num = sc.nextInt(); 獲取鍵盤輸入的一個字符串:String str = sc.next(); * */public class Demo01Scanner {public static void main(String[] args) {//System.in代表從鍵盤輸入Scanner sc = new Scanner(System.in);//2.創建//3.獲取鍵盤輸入的數字/字符int num = sc.nextInt();System.out.println("輸入的數字為:" + num);String str = sc.next();System.out.println("輸入的字符串為:" + str);} }三、練習一:鍵盤輸入兩個數求和值
package Demo1602; import java.util.Scanner; /* * 題目:鍵盤輸入兩個int數字,并且求出和值 * * 思路: * 1.既然需要鍵盤輸入,那么就用scanner * 2.Scanner的三個步驟:導包、創建、使用 * 3.需要的是兩個數字,所以需要調用兩次nextInt方法 * 4.得到了兩個數字,就需要加在一起 * 5.將結果打印輸出 * */ public class Demo02ScannerSum {public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.println("請輸入第一個數字:");int num1 = sc.nextInt();System.out.println("請輸入第二個數字:");int num2 = sc.nextInt();System.out.println("輸入兩個數字的和值為:" + (num1+num2));} }四、練習二:鍵盤輸入三個數求最大值
package Demo1602;/** 題目:* 鍵盤輸入三個int數字,然后求出其中的最大值。* 思路:* 1.既然是鍵盤輸入,肯定需要使用到Scanner* 2.Scanner三個步驟:導包、創建、使用nextInt()方法* 3.既然是三個數字,那么調用三次nextInt()方法,得到三個int變量* 4.無法同時判斷三個數字誰最大,應該轉換成兩個步驟:* 4.1首先判斷兩個當中誰最大,拿到前兩個的最大值* 4.2 拿著前兩個中的最大值,再和第三個數字比較,得到三個數組當中的最大值* 5.打印最終結果* */import java.util.Scanner;public class Demo03ScannerMax {public static void main(String[] args) {//1.第一種思路:數組循環輸入并循環比較,我的思路,較為麻煩/*int[] array = new int[3];Scanner scanner = new Scanner(System.in);for (int i = 0; i < array.length; i++) {System.out.println("請輸入數組的第" + (i + 1) + "個元素");array[i] = scanner.nextInt();}int max = array[0];for (int i = 0; i < array.length; i++) {if (array[i] > max) {max = array[i];}}System.out.println("最大值為:" + max);*///2.第二種思路,直接利用三元運算符,兩個兩個的比較最大值,但是比較適合少的,多的還是需要用數組Scanner scanner = new Scanner(System.in);System.out.println("請輸入第一個數字:");int a = scanner.nextInt();System.out.println("請輸入第二個數字:");int b = scanner.nextInt();System.out.println("請輸入第三個數字:");int c = scanner.nextInt();int temp = a > b ? a : b;int max = temp > c ? temp : c;System.out.println("最大值為:" + max);} } 與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的第16天学习Java的笔记(标准类,Scanner)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 第15天学习Java的笔记(封装性,th
- 下一篇: 第17天学习Java的笔记(匿名对象,随