java数组总结及键盘输入方法
生活随笔
收集整理的這篇文章主要介紹了
java数组总结及键盘输入方法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
鍵盤輸入
數組
數組名
數組元素
角標(下標,索引)
數組的長度(元素的個數)
數組是有序排列的
數組的長度一旦確定,不能修改
數組是引用類型,數組的元素可以是別的類型
按維度 :一維數組,二維數組。。。。
按數組元素的類型 :基本數據類型,引用數據類型
4.一位數組的使用
1:一位數組的聲明和使用
2:如何調用數組指定位置的元素
3:如何獲取數組的長度
4:如何遍歷數組
5:數組元素的默認初始化值
與一維數組類似
1: 一位數組的聲明和使用
2:如何調用數組指定位置的元素
3:如何獲取數組的長度
4:如何遍歷數組
5:數組元素的默認初始化值
6.數組的練習
package com.learn.exerse;import java.util.Scanner;/*從鍵盤輸入學生成績,找出最高分,并輸出學生成績等級* 成績>=最高分-10 等級為“A”* 成績>=最高分-20 等級為“B”* 成績>=最高分-30 等級為“C”* 其余 等級為“D”*先讀入學生人數,根據人數創建int數組,存放學生成績* */public class ArrayDemo2 {public static void main(String[] args) {//1.使用Scanner,讀取學生個數Scanner scanner = new Scanner(System.in);System.out.println("請輸入學生人數");int number = scanner.nextInt();//2.創建數組,動態初始化int[] scores = new int[number];//3.遍歷數組,給數組元素賦值System.out.println("請輸入" + number + "個學生成績");for(int i = 0;i < scores.length;i++) {scores[i] = scanner.nextInt();}//4.獲取數組元素的最大值int maxScores = scores[0];for(int i = 0;i < scores.length;i++) {if(maxScores < scores[i])maxScores = scores[i];//獲取數組中的最大元素}//5。根據每個學生的成績,輸出等級和成績/*for(int i = 0;i < scores.length;i++) {if(maxScores - scores[i] <=10)System.out.println("第" + (i + 1) + "個學生的成績為" + scores[i] + ",等級為" + "A");else if(maxScores - scores[i] <= 20)System.out.println("第" + (i + 1) + "個學生的成績為" + scores[i] + ",等級為" + "B");else if(maxScores - scores[i] <= 30)System.out.println("第" + (i + 1) + "個學生的成績為" + scores[i] + ",等級為" + "C");elseSystem.out.println("第" + (i + 1) + "個學生的成績為" + scores[i] +",等級為" + "D");}*/for(int i = 0;i<scores.length;i++) {String level;if(maxScores - scores[i] <= 10)level = "A";else if(maxScores - scores[i] <= 20)level = "B";else if(maxScores - scores[i] <= 30)level = "C";else level = "D";System.out.println("第" + (i+1) + "個學生的成績為" + scores[i] + "等級為" + level);}}}楊輝三角
package com.learn.exerse;/*第一行有1個元素,第n行有n個元素* 每一行的第一個元素和最后一個元素都是1* 從第三行開始,對于非第一個元素和最后一個元素的元素有* yanghui[i][j] = yanghui[i-1][j-1] + yanghui[i-1][j];* * */ public class YangHuiTest {//1.聲明二維數組,初始化//2給數組元素賦值//3遍歷public static void main(String[] args) {int[][] yanghui = new int[10][];for(int i = 0;i < yanghui.length;i++) {yanghui[i] = new int[i + 1];//2.1給首末元素賦值yanghui[i][0] = yanghui[i][i] = 1;//給每行的非首末元素賦值if(i > 1) {for(int j = 1;j < yanghui[i].length-1;j++) {yanghui[i][j] = yanghui[i-1][j-1] + yanghui[i-1][j];}}}for(int i = 0;i < yanghui.length;i++) {for(int j = 0;j < yanghui[i].length;j++) {System.out.println(yanghui[i][j]);}}}}好了,先寫這么多
總結
以上是生活随笔為你收集整理的java数组总结及键盘输入方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 个人作业2——英语学习APP案例分析
- 下一篇: Netconf资料收集