1、打印下列图形
* *
*** * *
***** * * *
******* * * * *
***** * * *
*** * *
* *
图形一:
public class HomeWork8_24 { public static void main(String[] args) { for (int i = 1; i <= 4; i++) { for (int x = 1; x <= 4 - i; x++) { System.out.print(" "); } for (int j = 1; j <= 2 * i - 1; j++) { System.out.print("*"); } System.out.println(); } for (int i = 3; i >= 1; i--) { for (int x = 1; x <= 4 - i; x++) { System.out.print(" "); } for (int j = 1; j <= 2 * i - 1; j++) { System.out.print("*"); } System.out.println(); } } }
图形二:
public class HomeWork8_24 { public static void main(String[] args) { for (int i = 1; i <= 5; i++) { for (int x = 1; x <= 5 - i; x++) { System.out.print(" "); } for (int j = 1; j <= i - 1; j++) { System.out.print("* "); } System.out.println(); } for (int i = 4; i >= 1; i--) { for (int x = 1; x <= 5 - i; x++) { System.out.print(" "); } for (int j = 1; j <= i - 1; j++) { System.out.print("* "); } System.out.println(); } } }
2、打印出1-1000以内的所有“水仙花数”,所谓“水仙花数”是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个“水仙花数”。
public class HomeWork8_24 { public static void main(String[] args) { for (int i = 100; i <= 999; i++) { if ((i % 10)*(i % 10)*(i % 10)+(i/10%10)*(i/10%10)*(i/10%10)+(i/100)*(i/100)*(i/100)==i){ System.out.println(i); } } } }
3、
①
import java.util.Scanner; public class HomeWork8_24 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("请随机输入一个数:"); int num = scanner.nextInt(); int sum = 0; int[] l1 = new int[]{8, 4, 2, 1, 23, 344, 12}; for (int i = 0; i < l1.length; i++) { System.out.println(l1[i]); sum += l1[i]; } System.out.println("所有数值的和为:" + sum); for (int i = 0; i < l1.length; i++) { if (num == l1[i]) { System.out.println("数列中包含此数"); break; } if(num!=l1[i] && i==(l1.length-1)){ System.out.println("数列中不包含此数"); } } scanner.close(); } }
②
import java.util.Scanner; public class HomeWork8_24 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("请输入4家店的价格"); int[] prices = new int[4]; int min = 3200; for (int i = 0; i < 4; i++) { System.out.print("第" + (i + 1) + "家店的价格:"); prices[i] = scanner.nextInt(); if (min > prices[i]){ min = prices[i]; } } System.out.println("最低价格是:" + min); } }
③
import java.util.Scanner; public class HomeWork8_24 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("请输入会员本月的消费记录"); double[] prices = new double[5]; int sum = 0; for (int i = 0; i < 5; i++) { System.out.print("请输入第" + (i + 1) + "笔购物金额:"); prices[i] = scanner.nextDouble(); } System.out.println("序号 金额(元)"); for (int i = 0; i < 5; i++) { System.out.println((i + 1) + " " + prices[i]); sum += prices[i]; } System.out.println("总金额 " + sum); } }
4、通过键盘输入10个不同的整数,然后实现倒序保存并输出。如输入1,2,3,4,5,倒序后是 5,4,3,2,1并输出。要求用数组实现。遍历 交换
import java.util.Scanner; public class HomeWork8_24 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int[] l1 = new int[10]; int[] l2 = new int[10]; System.out.println("请输入10个不同的整数:"); for (int i = 0; i < 10; i++) { l1[i] = scanner.nextInt(); l2[9-i] = l1[i]; } for (int i = 0; i < 10; i++) { System.out.println(l2[i] + " "); } } }
5、通过键盘输入5个整数,将这5个整数按逆时针顺序转动一次后再输出,如输入1,2,3,4,5,逆时针转动一次后,输出2,3,4,5,1。要求用数组实现。遍历插入
import java.util.Scanner; public class HomeWork8_24 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int[] l1 = new int[5]; int[] l2 = new int[5]; System.out.println("请输入5位整数:"); for (int i = 0; i < 5; i++) { System.out.print("第" + (i + 1) + "位整数为:"); l1[i] = scanner.nextInt(); } for (int i = 0; i < 5; i++) { if (i < 4){ l2[i] = l1[i+1]; }else { l2[i] = l1[4-i]; } } for (int i = 0; i < 5; i++) { System.out.print(l2[i] + " "); } } }
6、有n个人围成一圈,从头开始循环报1-3,报到3的人出局,求最后剩下的那个人。
import java.util.Scanner; public class HomeWork8_24 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("请输入总人数:"); int n = scanner.nextInt(); scanner.close(); boolean[] in = new boolean[n];//定义一个人是否在圈内的数组,在为true,不在为false; for (int i = 0; i < n; i++) { in[i] = true; } int sum = n;//圈内的人数 int num = 0;//报到的数 int index = 0;//索引 while (sum > 1) { if (in[index]) {//假定索引为index的人在圈内 num++; if (num == 3) { num = 0; in[index] = false; sum--; } } index++; if (index == n) { index = 0; //当索引的值等于n时把0赋值给索引,重新开始循环 } } for (int i = 0; i < n; i++) { if (in[i]) { System.out.println("留下的人为原来的第" + (i + 1) + "个人"); } } } }标签:java,一维,int,System,Scanner,数组,l1,public,out From: https://www.cnblogs.com/hsk991213/p/17658098.html