public class Demo4B {
public static void main(String[] args) {
// TODO code application logic here
int i = 0, j = 0;
for (i = 1; i <= 8; i++) {//表格「列」
//System.out.println("i=" + i);
//System.out.println("--------------------");
for (j = 1; j <= 9; j++) {//表格「欄」
System.out.print("■");
}
System.out.println();
}
}
}
//----------------------------------
public class Demo4C {
public static void main(String[] args) {
// TODO code application logic here
int i = 0, j = 0;
i=1;
while ( i <= 8) {//表格「列」
//System.out.println("i=" + i);
//System.out.println("--------------------");
j=1;
while ( j <= 9) {//表格「欄」
System.out.print("■");
j++;
}
System.out.println();
i++;
}
}
}
public class Demo4F {
public static void main(String[] args) {
// TODO code application logic here
int i = 0, j = 0;
for (i = 0; i <= 4; i++) {//表格「列」
for(int s=4; s>i; s--){ //印空格
System.out.print(" ");
}
for (j = 1; j<=(i*2+1); j++) {//印字母
System.out.print((char)(65+i));
}
System.out.println();
}
}
}
public class Demo4E {
public static void main(String[] args) {
// TODO code application logic here
int i = 0, j = 0;
for (i = 1; i <= 9; i++) {//表格「列」
for (j = 9; j >= 1; j--) {//表格「欄」
if (j > i) {
System.out.print(" ");
} else {
System.out.print("*");
}
}
System.out.println();
}
}
}
//-----------輸出視窗的字體「DialogInput」
public class Demo4D {
public static void main(String[] args) {
// TODO code application logic here
int i = 0, j = 0;
for (i = 9; i >= 1; i--) {//表格「列」
for (j = 1; j <= i; j++) {//表格「欄」
System.out.print("*");
}
System.out.println();
}
}
}
public class Demo5 {
public static void main(String[] args) {
// TODO code application logic here
int x1, x2, x3, x4, x5, x6, x7, x8;
int[] ints;
ints = new int[20];
Student studs[] = new Student[10];
new Student();
ints[2] = 15;
ints[6] = 30;
studs[1] = new Student();
studs[3] = new Student();
studs[1].name = "Cindy";
studs[3].name = "Wendy";
}
}
class Student {
String name;
}
//-------------------------------------------------
public class Demo6 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int[] ints = new int[7];
ints[0] = 1;
ints[1] = 3;
ints[2] = 5;
ints[3] = 7;
ints[4] = 9;
ints[5] = 11;
ints[6] = 13;
int[] ints2 = {1, 3, 5, 7, 9, 11, 13};
int[] ints3;
//ints3 ={1, 3, 5, 7, 9, 11, 13};
ints3 = new int[]{1, 3, 5, 7, 9, 11, 13};
//加強型for迴圈(for each)->僅提供讀取
for (int t : ints3) {
System.out.println(t);
t *= 2; //請不要透過 for each改變陣列元素值
}
System.out.println("---------------");
//搭配流程控制的迴圈
for (int i = 0; i < ints3.length; i++) {
System.out.println(ints[i]);
}
}
}
import java.util.Random;
public class Demo6A {
public static void main(String[] args) {
// TODO code application logic here
int[] nums = new int[49];
// int x1,x2,x3
//產生亂數
Random random = new Random();
for(int i=1; i<=10000; i++){
int n = random.nextInt(49)+1; // 1~49
nums[n-1]++;
// switch(n){
// case 1:
// nums[0]++;
// break;
// case 2:
// nums[1]++;
// break;
// }
}
int i=0;
for(int x : nums){
System.out.println(++i+" -> 出現 "+x+" 次");
}
}
}
請隨機產生介於1 ~ 49 之間數字 10,000 次,並且統計每一個數字
在這 10,000 當中出現的次數,執行結果參考如下:
1 -> 出現 ???次
2 -> 出現 ???次
3 -> 出現 ???次
4 -> 出現 ???次
...
48 -> 出現 ???次
49 -> 出現 ???次
import java.util.Random;
public class Demo6A {
public static void main(String[] args) {
// TODO code application logic here
int[] nums = new int[49];
// int x1,x2,x3
//產生亂數
Random random = new Random();
for(int i=1; i<=10000; i++){
int n = random.nextInt(49); // 0~48
nums[n]++;
// switch(n){
// case 1:
// nums[0]++;
// break;
// case 2:
// nums[1]++;
// break;
// }
}
int i=0;
for(int x : nums){
System.out.println(++i+" -> 出現 "+x+" 次");
}
}
}
//-----------------------------------------------------------------------------
import java.util.Random;
public class Demo6B {
public static void main(String[] args) {
// TODO code application logic here
//隨機產生介於1 ~ 49 之間7個不重複數字
int[] box = new int[49];
for (int i = 1; i <= box.length; i++) {
box[i - 1] = i;
}
int count = 0;
Random random = new Random();
while (count < 7) {
int n = random.nextInt(box.length); // 0 ~ 48
if (box[n] != -1) {//確認尚未被使用
System.out.println(box[n]);
count++;
box[n]=-1; //作記號
}
}
}
}
沒有留言:
張貼留言
文章有誤或有問題麻煩您留言告知! 謝謝您~~