package com.baidu;
public class Test01 {
public static void main(String[] args) {
for (int i = 1; ; i++) {
int total = i;
boolean isEnough = true;
for (int j = 1; j <= 5; j++) {
if((total - 1) % 5 == 0){
total = (total - 1) / 5 * 4;
}
else {
isEnough = false;
break;
}
}
if(isEnough){
System.out.println(i);
break;
}
}
}
}
- 程序里面出現(xiàn)了重復(fù)或相對(duì)獨(dú)立的功能 携添,那么應(yīng)該將這些功能單獨(dú)寫成一個(gè)方法
- 重載方法: 在一個(gè)類中可以出現(xiàn)同名方法 只要他們的參數(shù)列表不同就能夠加以區(qū)分.
- 參數(shù)列表不同指的是, 參數(shù)的類型不相同或者參數(shù)的個(gè)數(shù)不相同或者二者皆不同
package com.baidu;
import java.util.Scanner;
// 排列數(shù): A(m,n) = m! / n!
// 組合數(shù): C(m,n) = m! / n! / (m-n)!
public class Test03 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("輸入兩個(gè)正整數(shù): ");
int a = input.nextInt();
int b = input.nextInt();
if(a >= b){
int fa = f(a);
int fb = f(b);
int fc = f(a-b);
int x = fa / fb / fc;
System.out.println(x);
}
else{
System.out.println("a必須大于b僧须!");
}
input.close();
}
// 重載方法
// 在一個(gè)類中可以出現(xiàn)同名方法 只要他們的參數(shù)列表不同就能夠加以區(qū)分
// 參數(shù)列表不同指的是: 參數(shù)的類型不相同或者參數(shù)的個(gè)數(shù)不相同或者二者皆不同
public static int f(int n) {
int fb = 1;
for (int j = n; j > 0; j--) {
fb *= j;
}
return fb;
}
}
- 數(shù)組 - 用一個(gè)變量保存多個(gè)同類型的值
package com.baidu;
public class Test05 {
public static void main(String[] args) {
//數(shù)組 - 用一個(gè)變量保存多個(gè)同類型的值
int[] f = { 0, 0, 0, 0, 0, 0 };// int[] f = new int[6]
for (int i = 1; i <= 60000; i++) {
int face = (int) (Math.random() * 6 + 1);
f[face - 1] += 1;
}
for (int i = 1; i <= 6; i++) {
System.out.println(i + "點(diǎn)出現(xiàn)了" + f[i - 1] + "次");
}
}
}
- for-each只讀循環(huán)膳犹,不能改變數(shù)組的元素
(在for循環(huán)里面用x代表數(shù)組里的所有的值)
package com.baidu;
public class Test06 {
public static void main(String[] args) {
int[] f = new int[20];
f[0] = f[1] = 1;
for (int i = 2; i < f.length; i++) {
f[i] = f[i - 1] + f[i - 2];
}
// for-each循環(huán)(Java 5+)
// 只讀循環(huán)蛇受,不能改變數(shù)組的元素
for (int x : f) {
System.out.println(x);
}
}
}
package com.baidu;
import java.util.Scanner;
public class Test07 {
public static void main(String[] args) {
String[] names = {"關(guān)羽","張飛","劉備","趙云","馬超"};
double[] scores = new double[names.length];
Scanner input = new Scanner(System.in);
for (int i = 0; i < scores.length; i++) {
System.out.print("請(qǐng)輸入" + names[i] + "的成績(jī) :");
scores [i] = input.nextDouble();
}
input.close();
double sum = 0;
double min = scores[0];
double max = scores[0];
for (int i = 0; i < scores.length; i++) {
sum += scores[i];
}
for (int i = 1; i < scores.length; i++) {
if(min > scores[i]){
min = scores[i];
}
else if(max < scores[i]){
max = scores[i];
}
}
System.out.println("平均分:" + sum / scores.length);
System.out.println("最低分: " + min);
System.out.println("最高分: " + max);
}
}
- 15個(gè)基督徒和15個(gè)非基督徒從1開始報(bào)數(shù)循狰,報(bào)到9的死,下一個(gè)人接到1開始報(bào)數(shù)逞盆。
package com.baidu;
public class Test09 {
public static void main(String[] args) {
boolean[] people = new boolean[30];
for (int i = 0; i < people.length; i++) {
people[i] = true;
}
int counter = 0;
int index = 0;
int number = 0;
while(counter < 15){
if(people[index]){
number += 1;
if(number == 9){
people[index] = false;
counter += 1;
number = 0;
}
}
index += 1;
if(index == people.length){
index = 0;
}
}
for(boolean isChrist : people){
System.out.println(isChrist ? "基督徒" : "非基督徒");
}
}
}
package com.baidu;
public class Test10 {
public static void main(String[] args) {
int[] x = {23 , 67 , 12 , 99 , 58 , 77 , 88 , 4 , 45 , 81};
bubbleSort(x);
for(int a : x){
System.out.print(a + "");
}
}
public static void bubbleSort(int[] array) {
boolean swapped = true;
for (int i = 1; swapped && i <= array.length - 1; i++) {
swapped = false;
for(int j = 0 ; j < array.length - i; j++){
if(array [j] > array [j + 1]){
//交換兩個(gè)元素
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
swapped= true;
}
}
}
}
}