- Sep 29 Wed 2021 20:23
-
類別練習-學生成績系統
- Sep 24 Fri 2021 19:15
-
陣列/字串-猜猜幾A幾B
- Sep 23 Thu 2021 20:45
-
String - 字串

字串-String
是一種參考資料型態,當宣告 String str = "abc"; 時,會在stack記憶體中開一個空間名為str,然後在heap記憶體的【字串池】中開一個放置字串"abc"
- Sep 21 Tue 2021 15:17
-
遞迴-「費氏數列」&「河內塔」
- Sep 20 Mon 2021 15:11
-
陣列-判斷數字是否輸入過/並統計出現次數
- Sep 19 Sun 2021 13:12
-
LeetCode紀錄-Two Sum
- Sep 16 Thu 2021 12:55
-
陣列應用-陣列依照輸入的數量加長 / 排列陣列大小

讓使用者輸入學生成績,範圍0~100,輸入-1時結束程式
import java.util.Scanner;
public class MainClass {
public static void main(String[] args) {
//輸入學生成績,範圍0~100,輸入-1時結束程式
Scanner sc = new Scanner(System.in);
int student[] = new int[2];
int count = 0;
while (true) {
if (count == student.length) {
student = doubleArr(student);
}
int input;
do {
System.out.print("Enter " + (count + 1) + "th Grade:");
input = sc.nextInt();
if (input == -1) {
break;
}
} while (input < 0 || input > 100);
if (input == -1) { //必須在break一次離開當前迴圈
break;
}
student[count] = input;
count++;
System.out.println(student.length);
}
}
public static int[] doubleArr(int arr[]) {
int newArr[] = new int[arr.length * 2]; //將陣列長度放大兩倍
for (int i = 0; i < arr.length; i++) { //將原陣列的數值放入增倍的陣列
newArr[i] = arr[i];
}
return newArr; //傳回新陣列的地址
}
}
- Sep 15 Wed 2021 20:35
-
基本函式介紹-交換陣列位置 / 計算次方 / 骰子遊戲



