package com.lele.day7_18;
import java.util.Scanner;
public class Test08 {
public static void main(String[] args) {
// (显示金字塔)编写程序,提示用户输入一个在1到15之间的整数,然后显示一个金字塔形状的图案,如下面的运行示例所示:
// Enter the number of lines: 3
// 1
// 2 1 2
// 3 2 1 2 3
System.out.print("Enter the number of lines:");
Scanner scanner = new Scanner(System.in);
int lines = scanner.nextInt();
scanner.close();
for (int i = 1; i <= lines; i++) {
for (int j = lines - i; j > 0; j--) //输出空格
System.out.print(" ");
for (int j = 0; j < i - 1; j++) //输出左边的三角部分
System.out.printf(" "+(i-j));
for (int j = i -1; j >= 0; j--) //输出右边的三角部分
System.out.printf(" "+(i-j));
System.out.println(); //输出换行
}
}
}
/*
Enter the number of lines:5
1
2 1 2
3 2 1 2 3
4 3 2 1 2 3 4
5 4 3 2 1 2 3 4 5
*/
package com.lele.day7_18;
public class Zuo41 {
public static void main(String[] args) {
// TODO Auto-generated method stub
print3();
}
public static void print() {
for (int i = 1; i <= 6; i ++) {
for (int j = 1; j <= i; j ++) {
System.out.print(j + " ");
}
System.out.println();
}
}
public static void print1() {
for (int i = 6; i >=1 ; i --) {
for (int j = 1; j <= i; j ++) {
System.out.print(j + " ");
}
System.out.println();
}
}
public static void print2() {
for(int line = 1;line <= 6;line++)
{
for(int i = 1;i <= 2 * (6 - line);i++)
System.out.print(" ");
for(int i = line;i >= 1;i--)
System.out.printf(" " + i);
System.out.print("\n");
}
}
public static void print3() {
for(int line = 6;line >= 1;line --)
{
for(int i = 1;i <= 2 *(6-line);i++)
System.out.print(" ");
for(int i = 1;i <= line;i ++)
System.out.print( i + " ");
System.out.print("\n");
}
}
}
打印全字塔形的数字)编写一个嵌套的for循环,打印下面的输出:
1
1 2 1
1 2 4 2 1
package com.lele.day7_18;
import java.util.Scanner;
public class Zuo42 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.print("请输入要打印的行数: ");
int x=sc.nextInt();
for(int j=1;j<=x;j++) {//这个for循环是用来控制行数
for(int z=1;z<=x-j;z++) {
System.out.print(" ");
}
for(int i=1;i<=j+1;i++) {
if(i<=j) {
System.out.printf("%4d",(int)Math.pow(2, i-1));
}
else {
for(int y=j-2;y>=0;y--) {
System.out.printf("%4d",(int)Math.pow(2,y));
}
}
}
System.out.println();
}
}
}
/*
请输入要打印的行数: 5
1
1 2 1
1 2 4 2 1
1 2 4 8 4 2 1
1 2 4 8 16 8 4 2 1
*/
(打印2到1000之间的素数)修改程序清单5-15,打印2到1000之间、包括2和1000的所有素数,每行显示8个素数。数字之间用一个空格字符隔开。.
package com.lele.day7_18;
public class Zuo43 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int count = 0;
for (int i = 2; i < 1000; i++) {
boolean f = true;
for (int j = 2; j < i; j ++) {
if (i % j == 0) {
f = false;
break;
}
}
if (f) {
System.out.print(i + ",");
count ++;
if (count % 8 ==0) {
System.out.println();
}
}
}
}
}
/*
2,3,5,7,11,13,17,19,
23,29,31,37,41,43,47,53,
59,61,67,71,73,79,83,89,
97,101,103,107,109,113,127,131,
137,139,149,151,157,163,167,173,
179,181,191,193,197,199,211,223,
227,229,233,239,241,251,257,263,
269,271,277,281,283,293,307,311,
313,317,331,337,347,349,353,359,
367,373,379,383,389,397,401,409,
419,421,431,433,439,443,449,457,
461,463,467,479,487,491,499,503,
509,521,523,541,547,557,563,569,
571,577,587,593,599,601,607,613,
617,619,631,641,643,647,653,659,
661,673,677,683,691,701,709,719,
727,733,739,743,751,757,761,769,
773,787,797,809,811,821,823,827,
829,839,853,857,859,863,877,881,
883,887,907,911,919,929,937,941,
947,953,967,971,977,983,991,997,
*/
package com.lele.day7_18;
public class Zuo44 {
public static void main(String[] args) {
// TODO Auto-generated method stub
double m = 3;
double num = 0;
double sum = 0;
for (int z = 1; z <= 97 ; z += 2) {
num = (z / m);
sum = sum + num ;
m += 2;
}
System.out.println("之后的和为:" + sum);
}
}
/*
之后的和为:45.124450303050196
*/
package com.lele.day7_18;
public class Zuo47 {
public static void main(String[] args) {
// TODO Auto-generated method stub
double sum = 1;
double e = 1;
double item = 1;
for (int i = 1; i <= 100000; i++) {
sum *= i;
item = 1 / sum;
e += item;
if (i % 10000 == 0) {
System.out.println("当i="+ i + "时,e为:"+ sum);
}
}
}
}
(完全数)如果一个正整数等于除它本身之外其他所有除数之和,就称之为完全数。例如:6是第一个完全数,因为6=1+2+3。下一个完全数是28=14+7+4+2+1。10 000以下的完全数有四个。编写程序,找出这四个完全数。
package com.lele.day7_18;
public class Zuo48 {
public static void main(String[] args) {
// TODO Auto-generated method stub
for (int i = 1; i < 10000; i++) {
int sum = 0;
for (int j = 1; j < i; j++ ) {
if (i % j == 0 ) {
sum += j;
}
}
if (sum == i) {
System.out.println(i);
}
}
}
}
/*
6
28
496
8128
*/
(游戏:石头、剪刀、布)编程练习题3.17给出玩石头-剪刀-布游戏的程序。修改这个程序,让用户可以连续地玩这个游戏,直到用户或者计算机赢对手两次以上为止。
package com.lele.day7_18;
import java.util.Random;
import java.util.Scanner;
public class Zuo49 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int jy = 0;
int ry = 0;
while (true) {
System.out.println("请输入您的出拳:(0:石头;1:剪刀;2:布");
Scanner scanner = new Scanner(System.in);
int r = scanner.nextInt();
Random random = new Random();
int j = random.nextInt(3);
System.out.println(j);
if ( (r == 0 && j ==1) ||(r ==1 && j ==2) ||(r ==2 && j ==0) ) {
System.out.println("恭喜您赢了!!");
ry ++;
}
if ( (r == 1 && j ==0) ||(r ==2 && j ==1) ||(r ==0 && j ==2) ) {
System.out.println("抱歉您输了!!");
jy ++;
}
if (r == j) {
System.out.println("平局!!");
}
if (ry >=2 || jy >= 2) {
break;
}
}
}
}
/*
请输入您的出拳:(0:石头;1:剪刀;2:布
1
1
平局!!
请输入您的出拳:(0:石头;1:剪刀;2:布
1
1
平局!!
请输入您的出拳:(0:石头;1:剪刀;2:布
1
2
恭喜您赢了!!
请输入您的出拳:(0:石头;1:剪刀;2:布
1
0
抱歉您输了!!
请输入您的出拳:(0:石头;1:剪刀;2:布
1
2
恭喜您赢了!!
*/
(十进制到二进制)编写程序,提示用户输入一个十进制整数,然后显示对应的二进制值。在这个程序中不要使用Java的Interger.toBinaryString(int)方法。
package com.lele.day7_18;
import java.util.Scanner;
public class Zuo50 {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("请输入你想转换的十进制数字:");
Scanner scanner = new Scanner(System.in);
int zhuan = scanner.nextInt();
scanner.close();
String result = "";
for(int i = zhuan; i > 0; i/=2){
result = i % 2 + result;
}
System.out.println(result);
}
}
/*
请输入你想转换的十进制数字:
12
1100
*/
(十进制到八进制)编写程序,提示用户输人一个十进制整数,然后显示对应的八进制值。在这个程序中不要使用Java的 Interger.to0cta1String(int)方法。
package com.lele.day7_18;
import java.util.Scanner;
public class Zuo51 {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("请输入你想转换的十进制数字:");
Scanner scanner = new Scanner(System.in);
int zhuan = scanner.nextInt();
scanner.close();
String result = "";
for (int i = zhuan; i > 0; i /= 8) {
result = i % 8 + result;
}
System.out.println(result);
}
}
package com.lele.day7_20;
import java.util.Scanner;
public class Zuo52 {
public static void main(String[] args) {
/*
* (最大数的出现次数)编写程序读取整数,找出它们的最大数,然后计算该数的出现次数。假设输人是以0结束的。 假定输入是3 5 2 5 5 5
* 0,程序找出最大数5,而5出现的次数是4。 提示:维护max和count两个变量。max存储当前最大数,而count存储它的出现次数。
* 初始状态时,将第一个数赋值给 max而将count赋值为1。然后将接下来的每个数字逐个地和max进行比较。
* 如果这个数大于max,就将它赋值给max,同时将count重置为1。如果这个数等于max,就给count 加1。
*/
int max = 1,count = 1;
Scanner input = new Scanner(System.in);
System.out.print("Enter numbers: ");
int number = input.nextInt();
while (number != 0){
if(number == max)
count++;
if (number > max){
max = number;
count = 1;
}
number = input.nextInt();
}
System.out.println("最大值为:" + max + ",出现了:" + count + "次");
}
}
package com.lele.day7_20;
import java.util.Scanner;
import java.util.concurrent.CountDownLatch;
public class Zuo53 {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("请输入三角形的a边:");
Scanner scanner = new Scanner(System.in);
double a = scanner.nextDouble();
System.out.println("请输入三角形的b边:");
Scanner scanner1 = new Scanner(System.in);
double b = scanner1.nextDouble();
System.out.println("请输入三角形的c边:");
Scanner scanner2 = new Scanner(System.in);
double c = scanner2.nextDouble();
double A = Math.toDegrees(Math.acos((a * a - b * b -c * c) / (-2 * b * c)));
double B = Math.toDegrees(Math.acos((b * b -a * a -c * c) / (-2 * a * c)));
double C = Math.toDegrees(Math.acos((c * c- b * b - a * a) / (-2 * a * b)));
System.out.println(A);
System.out.println(B);
System.out.println(C);
}
}
/*
请输入三角形的a边:
3
请输入三角形的b边:
4
请输入三角形的c边:
5
36.86989764584401
53.13010235415598
90.0
*/