public static void jiecheng(int n) {
int m = 1;
for(int i = n;i >= 1;i--) {
m = m * i;
}
System.out.println("階乘為:" + m);
}
public static void main(String[] args) {
jiecheng(5);
}
public static int getZ() {
int num = 200;
for(int i = 2;i < num;i++) {
if(num % i == 0) {
i = 2;
num++;
}
}
return num;
}
public static void main(String[] args) {
System.out.println(getZ());
}
int a[] = {10,90,80,40,60,50,20,30,70};
int max = a[0];
int min = a[0];
int sum = 0;
for(int i = 0; i < a.length;i++) {
if(a[i] > max) {
max = a[i];
}
if(a[i] < min) {
min = a[i];
}
sum = sum + a[i];
}
System.out.println("數(shù)組中最大值為:" + max);
System.out.println("數(shù)組中最小值為:" + min);
System.out.println("數(shù)組的總和為:" + sum);
System.out.println("數(shù)組的平均值為:" + (sum / a.length));
-
去除數(shù)組中的重復(fù)數(shù)字,讓重復(fù)數(shù)字變?yōu)?,將數(shù)組中重復(fù)元素保留一個(gè),其余清零
int a[] = {10,10,10,20,30,30,40,50,50};
for(int i = 0;i <= a.length - 1;i++) {
for(int j = 0;j < a.length;j++) {
if(a[i] == a[j] && i != j) {
a[j] = 0;
}
}
}
System.out.println(Arrays.toString(a));
Random r = new Random();
int i = r.nextInt(6);//0~5范圍內(nèi)的隨機(jī)數(shù)
int j = r.nextInt(6) + 1;//1~6范圍內(nèi)的隨機(jī)數(shù)
System.out.println(i);
System.out.println(j);
//產(chǎn)生隨機(jī)數(shù)放入數(shù)組
Random rr = new Random();
int a[] = new int[6];
for(int m = 0;m < a.length;m++) {
a[m] = rr.nextInt(6) + 1;
for(int n = 0;n < m;n++) {
if(a[n] == a[m]) {
m--;
}
}
}
System.out.println(Arrays.toString(a));
int a[] = {10,80,90,40,60,70,20,30,50};
for(int i = 1;i < a.length - 1;i++) {
for(int j = 0;j < a.length - i;j++) {
if(a[j] > a[j + 1]) {
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
System.out.println(Arrays.toString(a));
int arr[] = {1,2,3,4,5};
for(int i = 0;i < arr.length / 2;i++) {
int temp = arr[i];
arr[i] = arr[arr.length - 1 - i];
arr[arr.length - 1 - i] = temp;
}
System.out.println(Arrays.toString(arr));
-
找數(shù)組中是否有這個(gè)值,有就輸出第一次出現(xiàn)的下標(biāo),沒有就提示沒找到
int num = 90;
int arr[] = {10,20,30,60,50,40,90,80,70};
boolean flag = true;
for(int i = 0;i < arr.length;i++) {
if(arr[i] == num) {
System.out.println("找到了" + num + "," + "下標(biāo)為:" + i);
flag = false;
break;
}
}
if(flag) {
System.out.println("沒找到" + num);
}
int[] arr = {1,2,3,4,5,6,7,8,9};
int n = 7;//要查找的元素
int head = 0;//起始位置下標(biāo)
int end = arr.length - 1;//結(jié)束位置的下標(biāo)
boolean b = true;//控制找沒找到的打印輸出
while(head <= end) {
int middle = (head + end) / 2;
if(arr[middle] == n) {
System.out.println("找到了,下標(biāo)為:" + middle);
b = false;
break;
}else if(n < arr[middle]) {//在左邊
end = middle - 1;
}else {//在右邊
head = middle + 1;
}
}
if(b) {
System.out.println("沒找到");
}
-
使用do-while語句計(jì)算從1到100的所有奇數(shù)相加
int i = 1;
int sum = 0;
do {
if (i % 2 != 0)
sum += i;
i++;
} while (i <= 100);
System.out.println("1到100之間的奇數(shù)和為" + sum);
-
用for循環(huán)改寫從1到100的所有奇數(shù)相加的和
int sum = 0;
for (int i = 1; i <= 100; i++) {
if (i % 2 != 0)
sum += i;
}
System.out.println("1到100之間的奇數(shù)和為" + sum);
-
使用while語句計(jì)算從1到100的所有奇數(shù)相加
int i = 1;
int sum = 0;
while (i <= 100) {
if(i%2!=0)
sum += i;
i++;
}
System.out.println("1到100之間的奇數(shù)和為" + sum);
-
使用while語句計(jì)算1+2+3+...+10的和
int i = 1;
int sum = 0;
while (i <= 10) {
sum += i;
i++;
}
System.out.println("1+2+...+10=" + sum);
/**請輸出所有的水仙花數(shù)。
* 問題描述:水仙花數(shù)指一個(gè)特殊的三位數(shù),它的各位數(shù)字的立方和與其自身相等。
* 編程思路:關(guān)鍵是將三位數(shù)的個(gè)位、十位和百位數(shù)字分別拆分出來。**/
for (int i = 100; i < 1000; i++) { // 循環(huán)所有三位數(shù)
int a = i % 10; // 拆分出個(gè)位數(shù)字
int b = (i / 10) % 10; // 拆分出十位數(shù)字
int c = i / 100; // 拆分出百位數(shù)字
// 判斷立方和是否等于自身
if (a * a * a + b * b * b + c * c * c == i) {
System.out.println(i);
}
}
/**問題描述:在控制臺中用星號"*"輸出樣式圖形
**編程思路:
* 在外部使用循環(huán)語句執(zhí)行5次每次打印1行,
* 每行的內(nèi)容分別為空格和星號"*"
* 每行空格縮進(jìn)的數(shù)量為5減去所在行數(shù)
* 星號"*"的數(shù)量是所在行數(shù)的2倍減1。
* 在內(nèi)部使用循環(huán)語句首先打印空格,然后打印星號"*"
* 對應(yīng)的打印次數(shù)用循環(huán)次數(shù)控制,打印星號之后就可以換行。 **/
for (int row = 1; row <= 5; row++) { // 外層循環(huán)執(zhí)行換行
// 打印空格的數(shù)量為5減去所在行數(shù)
for (int c1 = 0; c1 < 5 - row; c1++) {
System.out.print(' ');
}
// 打印星號的數(shù)量為所在行數(shù)的2倍減1
for (int c2 = 0; c2 < 2 * row - 1; c2++) {
System.out.print('*');
}
// 換行
System.out.println();
}
/**問題描述:
* 如何判斷某個(gè)自然數(shù)是否為質(zhì)數(shù)。
* 質(zhì)數(shù)是只能被1和自身整除的自然數(shù),也稱素?cái)?shù),質(zhì)數(shù)中最小的為2。
* 所有自然數(shù)都可以被自身和1整除。
**編程思路:
* 我們只需判斷一個(gè)數(shù)能否被1和自身以外的數(shù)字整除即可
* 大于其本身的自然數(shù)除外。
* 若數(shù)字為n,則只需判斷從2到n-1之間的所有數(shù)字
* 即程序只需判斷該數(shù)能否被區(qū)間[2,n-1]內(nèi)的某個(gè)自然數(shù)整除即可
* 若在區(qū)間內(nèi)存在能被整除的數(shù)則說明不是質(zhì)數(shù)。**/
int n = 29;
boolean b = true; //布爾類型,表示是否為質(zhì)數(shù),初始值為真
for (int i = 2; i < n; i++) {
//若能夠整除則不是質(zhì)數(shù)
if (n % i == 0) {
b = false;
break; //跳出循環(huán)
}
}
//輸出結(jié)果,判斷是否為質(zhì)數(shù)
if (b) {
System.out.println(n + "是質(zhì)數(shù)");
} else {
System.out.println(n + "不是質(zhì)數(shù)");
}
/**問題描述:
* 最大公約數(shù)指兩個(gè)數(shù)字公共的約數(shù)中最大的
* 例如數(shù)字3的約數(shù)有1、3,數(shù)字9的約數(shù)有1、3、9,
* 則數(shù)字3和數(shù)字9的公共約數(shù)有1和3,其中3是最大的公約數(shù)。
**編程思路:
* 假設(shè)初始值從1開始逐步增1,
* 每次把能同時(shí)使兩個(gè)數(shù)整除的值都存儲起來,
* 那么最后一個(gè)存儲起來的值就是最大的約數(shù)。**/
// 求8和12的最大公約數(shù)
int m = 8;
int n = 12;
int result = 1;
for (int i = 1; i <= m; i++) {
if ((m % i == 0) && (n % i == 0)) {
result = i;
}
}
System.out.println(result);
public static void getYear(int n) {
if(n % 4 == 0) {
System.out.println("該年份是閏年");
}else {
System.out.println("該年份是平年");
}
}
public static void main(String[] args) {
getYear(2024);
}
-
不多于5位的整數(shù),求其是幾位數(shù),并分別打印輸出每一位數(shù)字
Scanner scan = new Scanner(System.in);
System.out.println("請輸入一個(gè)不多于5位的正整數(shù):");
int i = scan.nextInt();
int count = 0;
while(true) {
int wei = i % 10;//截取每一位
i = i / 10;
count++;
System.out.println(wei);
if(i == 0) {
break;
}
}
scan.close();//關(guān)閉數(shù)據(jù)流
System.out.println("是" + count + "位數(shù)");