實訓09.08:簡單的演算法練習
阿新 • • 發佈:2018-12-09
/*final 關鍵字 修飾的變數值 後期不可更改 相當於定義常量
常量 :不可更改
*/
final int a = 10;
//a = 20; 報錯的值不可更改!
/*輸入函式
* */
System.out.println("請輸入數字:");
Scanner scanner = new Scanner(System.in);
int b = scanner.nextInt(); //需要一個變數來承接
System.out .println(b);
String name = scanner.next(); //字串
System.out.println("請輸入年份:");
int year = scanner.nextInt();
if((year % 4 == 0 && year %100 != 0) || year % 400 ==0) {
System.out.println(year + "是閏年");
}else {
System.out.println(year + "是平年" );
}
System.out.println("請輸入月份:");
int moon = scanner.nextInt();
if(moon<0 || moon >12) {
System.out.println("輸入錯誤!");
return;
}else if(moon == 1 || moon ==3 || moon == 5 ||moon == 7 || moon ==8|| moon ==10 || moon ==12) {
System.out .println(moon + "月有31天");
}else if (moon){
}
//隨機數 Random系統的提供的類,用於獲取一個隨機自然數
//(大範圍 - 小範圍 + 1) + 小範圍
Random random = new Random();
// int num = random.nextInt(100); 0 ~ 100
// int num = random.nextInt() % 100; -99 ~ 99
int num = random.nextInt(40 - 20 + 1) + 20;
System.out.println(num);
// for(int i = 1;i <= 100; i++) {
// if(i % 2 != 0) {
// System.out.println(i);
// }
// }
// int sum = 0;
// for (int i = 1; i <= 100; i++) {
// sum += i;
// }
// System.out.println(sum);
// for(int i = 0;i <= 100; i++) {
// if(i % 7 == 0) {
// System.out.println(i);
// }
// }
// for (int i = 1;i <= 100;i++) {
// if(i % 10 == 7 ) {
// System.out.println(i);
// }
// }
// for (int i = 1;i <= 100; i++) {
// if(i /10 == 7) {
// System.out.println(i);
// }
// }
// for (int i = 1;i<= 100;i++) {
// if(i / 7 != 0 && i % 10 != 7) {
// System.out.println(i);
// }
// }
int min = 100, max = 0;
for(int i = 1 ;i <= 10;i++) {
// int num2 = random.nextInt(80 - 30 + 1) + 30;
int temp = random.nextInt(51) + 30;
if(temp > max) {
max = temp;
} if(temp < min) {
min = temp;
}
System.out.print(temp + " ");
}
System.out.println("最大值為:" + max + ";最小值為 :"+min);
//練習1
for(int i = 1;i < 4;i ++) {
for(int j = 1 ;j <= i +1; j++) {
System.out.print(j + " ");
}
System.out.println();
}
System.out.println("---------------------");
//練習2
for(int i = 0 ;i < 4; i++) {
for(int j =1; j <= 4 - i; j++) {
System.out.print(j + " ");
}
System.out.println();
}
System.out.println("---------------------");
//練習3
for(int i = 0;i < 4;i++) {
for(int j = 4; j >= i + 1; j--) {
System.out.print(j + " ");
}
System.out.println();
}
System.out.println("---------------------");
//練習4
for(int i = 1;i <= 9; i++) {
for(int j = 1; j <= i; j ++) {
System.out.print(j + " * " + i + " = " + i * j);
}
System.out.println();
}
//while迴圈
//練習1
int money = 1000000000;
int day = 0;
while(money != 0) {
money /=2;
day ++;
}
System.out.println("一共花了" + day + "天");
}
// 陣列
//eg : 32位元組
// int[] a1 = {11, 22, 44, 33, 55, 77, 66};
// a1[0] = 100;
// int[] a2 = new int [10]; //0 0 0 0 0 0 0 0 0 0
//
//
// for(int i = 0;i < a1.length;i++) {
// System.out.println(a1[i] + " ");
// }
// Randon random= new Random();
//練習1
Random random = new Random();
/* int[] a1 = new int[10];
int[] a2 = new int [10];
for(int i = 0 ;i < 10; i++) {
a1[i] = random.nextInt(41) + 30;
a2[i] = a1[i];
System.out.println(a1[i] + " " + a2[i]);
}
*/
//練習2
/* int[] arr1 = new int[5];
int[] arr2 = new int[5];
int[] arr = new int[10];
for(int i = 0 ;i < 5; i ++) {
arr1[i] = random.nextInt(41) +50;
}
*/
//練習3
int min = 100, minIndex = 0; //儲存下標
int max = 0,maxIndex = 0;
int[] array1 = new int [10];
for(int i =0; i < 10;i ++) {
array1[i] = random.nextInt(41) + 10;
if(array1[i] < min) {
minIndex = i;
min = array1[i] ;
} if(array1[i] > max) {
maxIndex = i;
max = array1[i];
}
System.out.print(array1[i] + " ");
}
System.out.println();
System.out.println(min + "下標為: " + minIndex);
System.out.println(max + "下標為:" + maxIndex);
//二維陣列 本質有多個一維陣列組成
//二位陣列的length只能獲取行數
int[][] m = {
{1,2,3,4},
{11,22,33,44},
{111,222,333,444}
};
System.out.println(m[1][3]);
package yb;
import java.util.Scanner;
public class beyond {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("請輸入一個年份,謝謝合作!");
int wsq= sc.nextInt();
if((wsq%4==0 && wsq%100 !=0) || wsq%400 ==0) {
System.out.println("好了,你可以上天了。");
System.out.println("請輸入一個月份");
int yy = sc.nextInt();
if(yy==1 || yy==3 || yy==5 || yy==7 || yy==8 || yy==10 || yy==12) {
System.out.println("31天");
}else if(yy==2) {
System.out.println("30天");
}else {
System.out.println("28天");
}
}else {
System.out.println("就你,還想上天?");
System.out.println("請輸入一個月份");
int yy = sc.nextInt();
if(yy==1 || yy==3 || yy==5 || yy==7 || yy==8 || yy==10 || yy==12) {
System.out.println("31天");
}else if(yy==2) {
System.out.println("30天");
}else {
System.out.println("29天");
}
}
}
package yb;
import java.util.Random;
public class CircleFor {
public static void main(String[] args) {
// Random
/*
* Random random = new Random(); int a = random.nextInt(41)+30;
*/
// System.out.println(a);
/*
* for(int i=1; i<=100;i=i+2) { System.out.println(i); }
*/
/*
* int sum = 0; for (int i = 1; i <= 100; i++) { sum = sum + i; }
*
* System.out.println(sum);
*
* for (int j = 1; j <= 100; j++) { if (j % 7 == 0) System.out.print(j + " "); }
* System.out.println();
*
* for (int j = 1; j <= 100; j++) { if (j % 10 == 7) System.out.print(j + " ");
* } System.out.println();
*
* for (int k = 1; k <= 100; k++) { if (k / 10 == 7) System.out.print(k + " ");
* } System.out.println();
*
* for (int o = 1; o <= 100; o++) { if ((o % 10 != 7) && (o % 10 != 7) && (o /
* 10 != 7)) System.out.print(o + " "); } System.out.println();
*
* int x[] = new int[10]; Random random = new Random();
*
* for (int y = 0; y < 10; y++) { x[y] = random.nextInt(51) + 30; }
*
* for (int i = 0; i < 10; i++) { for (int j = 0; j < 10 - i; j++) { if (x[j] >
* x[j + 1]) { int t = x[j]; x[j] = x[j + 1]; x[j + 1] = t; } } }
*
* for(int j =0;j<10;j++) { System.out.println(x[j]); }
*
* int max = 0, min = 10; for (int i = 1; i <= 100; i++) { int q =
* random.nextInt(51) + 30; if (q > max) max = q; if (q < min) min = q; }
* System.out.println(max + " " + min);
*/
for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j + " ");
}
System.out.println();
}
System.out
.println("-------------------------------------------------------------------------------------------");
for (int j = 4; j >= 1; j--) {
for (int k = 1; k <= j; k++) {
System.out.print(k + " ");
}
System.out.println();
}
System.out.println(
"--------------------------------------------------------------------------------------------");
for (int i = 4; i >= 1; i--) {
for (int k = 4; k >= i; k--) {
System.out.print(k + " ");
}
System.out.println();
}
System.out.println(
"--------------------------------------------------------------------------------------------");
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(i + "*" + j + "=" + j * i + " ");
}
System.out.println();
}
System.out.println(
"--------------------------------------------------------------------------------------------");
int y = 0;
double p = 1000000000;
while (p > 0) {
p = p / 2;
y++;
}
System.out.println(y);
}
}
}
package yb;
import java.util.Scanner;
public class days {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a,b,c,i,j;
int sum1=0,sum2=0;
int [] y = {0,31,28,31,30,31,30,31,31,30,31,30,31};
int [] u = {0,31,29,31,30,31,30,31,31,30,31,30,31};
System.out.println("請輸入年份:");
a = sc.nextInt();
if((a%4==0 && a%100 !=0) || a%400 ==0) {
System.out.println("請輸入一個月份");
b = sc.nextInt();
System.out.println("請輸入一天數");
c = sc.nextInt();
for(i=1;i<=b;i++) {
sum1 = sum1 + y[i];
}
System.out.println(sum1+c);
}else {
System.out.println("請輸入一個月份");
b = sc.nextInt();
System.out.println("請輸入一天數");
c = sc.nextInt();
for(i=1;i<=b;i++) {
sum1 = sum1 + u[i];
}
System.out.println(sum1+c);
}
}
}
package java02;
import java.util.Scanner;
public class section {
public static void main(String[] args) {
String [][] arr = new String [100][2];
Scanner wsq= new Scanner(System.in);
System.out.println("請輸入人數:");
int pp = wsq.nextInt();
for(int i=0;i<pp;i++) {
for(int j=0;j<pp;j++) {
System.out.println("請輸入賬號:");
String zh = wsq.next();
arr[i][0] = zh;
if(arr[i][0].equals(arr[i+1][0]))
j--;
else
break;
}
boolean yy = true;
while(yy) {
System.out.println("請輸入密碼:");
String mm = wsq.next();
System.out.println("請再次輸入密碼:");
String zmm = wsq.next();
if(mm.equals(zmm)) {
System.out.println("很好!幹得漂亮!");
arr[pp][1] = zmm;
break;
}else {
System.out.println("兩次密碼不一致!!!");
}
}
}
for(int i=0 ; i<pp;i++) {
for(int j=0;j<2;j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
}