目录
练习题18.猜数字
练习题19.将十进制数转换为十六进制数
练习题20.打印菱形和空心菱形
练习题21.判断回文
练习题22.打印素数
练习题23.统计正数和负数的个数然后计算这些数的平均值
练习题24.找出能被5或6整除,但不能被两者同时整除的数
练习题25.求满足n²>12000的n的最小值;求满足n³<12000的n的最大值
练习题26.显示金字塔
练习题27.打印金字塔形的数字
练习题28.数列求和
练习题29.计算π
练习题30.计算e
练习题31.显示闰年
练习题32.完全数
练习题33.游戏:石头、剪刀、布
练习题34.最大数的出现次数
练习题35.最长的共同前缀
import java.util.Scanner;
public class Demo18 {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.println("Guess ....!");
int number= (int) (Math.random()*101);
while(true){
System.out.print("Enter a guess:");
int num=scanner.nextInt();
if(num>number){
System.out.println("too high");
}else if(num
import java.util.Scanner;
public class Demo19 {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.print("Enter a number:");
int number=scanner.nextInt();
String str="";
while(number!=0){
int num=number%16;
if(num<10){
str=num+str;
}else{
switch(num){
case 10:
str="A"+str;
break;
case 11:
str="B"+str;
break;
case 12:
str="C"+str;
break;
case 13:
str="D"+str;
break;
case 14:
str="E"+str;
break;
case 15:
str="F"+str;
break;
}
}
number/=16;
}
System.out.println("0X"+str);
}
}
public class Demo20 {
public static void main(String[] args) {
/*
i k
----* 1 -4
---* * 2 -3
--* * * 3 -2
-* * * * 4 -1
* * * * * 5 0
-* * * * 6 1
--* * * 7 2
---* * 8 3
----* 9 4
*/
for(int i=1;i<=9;i++){
for(int k=1;k<=Math.abs(i-5);k++){
System.out.print(" ");
}
for(int j=1;j<=i&&(i+j)<=10;j++){
System.out.print("* ");
}
System.out.println();
}
/*
*
* *
* *
* *
* *
* *
* *
* *
*
*/
for(int i=1;i<=9;i++){
for(int k=1;k<=Math.abs(i-5);k++){
System.out.print(" ");
}
for(int j=1;j<=i&&(i+j)<=10;j++){
if(j==1||j==i||j+i==10){
System.out.print("* ");
}else{
System.out.print(" ");
}
}
System.out.println();
}
}
}
public class Demo21 {
public static void main(String[] args) {
String s="上海自来水来自海上";
boolean flag=true;
for(int m=0,n=s.length()-1;ms.length()/2;m++,n--){
if(s.charAt(n)!=s.charAt(m)){
flag=false;
break;
}
}
if(flag){
System.out.println("是回文");
}else{
System.out.println("不是回文");
}
System.out.println("最后要执行的代码,无论是不是回文");
}
}
public class Demo22 {
public static void main(String[] args) {
int count=0;//记录素数的个数
int number=1;
while(count<=50){
//计算素数
boolean flag=true;
for(int i=2;i<=number/2;i++){
if(number%i==0){
flag=false;
break;
}
}
if(flag){
count++;
System.out.print(number+" ");
if(count%10==0){
System.out.println();
}
}
number++;
}
}
}
import java.util.Scanner;
public class Demo23 {
public static void main(String[] args) {
/* 正数的个数 int positiveCount;
* 负数的个数 int negativeCount;
* 总和 int sum
*
* 平均值 double average;
* */
Scanner scanner=new Scanner(System.in);
int positiveCount=0;
int negativeCount=0;
int sum=0;
double average=0;
//1.获取用户输入 直到用户输入0为止
System.out.print("Enter an integer,the input ends if it is 0:");
while(true){
int num=scanner.nextInt();
if(num<0){
negativeCount++;
}else if(num>0){
positiveCount++;
}else{
break;
}
sum=sum+num;
}
average=1.0*sum/(negativeCount+positiveCount);
System.out.println("The number of positive is "+positiveCount);
System.out.println("The number of negative is "+negativeCount);
System.out.println("The total is "+sum);
System.out.println("The average is "+average);
}
}
public class Demo24 {
public static void main(String[] args) {
//能被5整除或被6整除 但不能同时满足
//(num%5==0||num%6==0) && (num%5==0^num%6==0)
int count=0;
for(int i=100;i<=200;i++){
if(i%5==0||i%6==0 && i%5==0^i%6==0){
System.out.print(i+" ");
count++;
if(count%10==0){
System.out.println();
}
}
}
}
}
public class Demo25 {
public static void main(String[] args) {
int n=1;
while(n*n<=12000){
n++;
}
System.out.println(109*109);
System.out.println(n);//110
n=1;
while(n*n*n<=12000){
n++;
}
System.out.println(n*n*n);
System.out.println(n-1);
System.out.println(22*22*22);
}
}
import java.util.Scanner;
public class Demo26 {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.print("Enter the line:");
int line=scanner.nextInt();
for(int i=1;i<=line;i++){
for(int k=1;k<=line-i;k++){
System.out.print(" ");
}
for(int j=-i;j<=i;j++){
if(j!=0&&j!=1){
System.out.printf("%-3d",Math.abs(j));
}
}
System.out.println();
}
}
}
import java.util.Scanner;
public class Demo27 {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.print("Enter the line:");
int line=scanner.nextInt();
for(int i=1;i<=line;i++){
for(int k=1;k<=line-i;k++){
System.out.print(" ");
}
for(int j=-(i-1);j<=i-1;j++){
if(j<=0){
System.out.printf("%-4d",(int)Math.pow(2, j+i-1));
}else{
System.out.printf("%-4d",(int)Math.pow(2, -j+i-1));
}
}
System.out.println();
}
}
}
public class Demo28 {
public static void main(String[] args) {
double sum=0;
for(int i=1;i<=97;i+=2){
sum=sum+1.0*i/(i+2);
}
System.out.println(sum);
}
}
public class Demo29 {
public static void main(String[] args) {
double pi=0.0;
double flag=1.0;
for(int i=1;i<=100000000;i++){
pi=pi+flag/(2*i-1);
flag=-flag;
}
pi=4*pi;
System.out.println(pi);
}
}
public class Demo30 {
public static void main(String[] args) {
double e=1;
for(int i=1;i<=10000;i++){
double multi=1.0;
for(int j=1;j<=i;j++){
multi=multi*j;
}
e=e+1/multi;
}
System.out.println(e);
}
}
public class Demo31 {
public static void main(String[] args) {
int count=0;
for(int i=101;i<=2100;i++){
if(i%4==0&&i%100!=0||i%400==0){
System.out.print(i+" ");
count++;
if(count%10==0){
System.out.println();
}
}
}
System.out.println("\n"+count);
}
}
public class Demo32 {
public static void main(String[] args) {
int sum=0;
for(int i=1;i<=10000;i++){
for(int j=1;j<=i/2;j++){
if(i%j==0){
sum+=j;
}
}
if(sum==i){
System.out.println(i);
}
sum=0;
}
}
}
import java.util.Scanner;
public class Demo33 {
public static void main(String[] args) {
int cCount=0;
int pCount=0;
Scanner scanner=new Scanner(System.in);
while(true){
System.out.print("请输入 0剪刀 1石头 2布:");
int p=scanner.nextInt();
int c=(int) (Math.random()*3);
if(p==c){
System.out.println("平局,请继续!");
}
if(p==0&&c==1){
System.out.println("玩家剪刀,电脑石头,电脑赢!");
cCount++;
}
if(p==0&&c==2){
System.out.println("玩家剪刀,电脑布,玩家赢!");
pCount++;
}
if(p==1&&c==0){
System.out.println("玩家石头,电脑剪刀,玩家赢!");
pCount++;
}
if(p==1&&c==2){
System.out.println("玩家石头,电脑布,电脑赢!");
cCount++;
}
if(p==2&&c==0){
System.out.println("玩家布,电脑剪刀,电脑赢!");
cCount++;
}
if(p==2&&c==1){
System.out.println("玩家布,电脑石头,玩家赢!");
pCount++;
}
if(cCount==2||pCount==2){
break;
}
}
if(cCount==2){
System.out.println("最终 电脑获胜");
}else{
System.out.println("最终 玩家获胜");
}
}
}
import java.util.Scanner;
public class Demo34 {
public static void main(String[] args) {
int max=0;
int count=0;
Scanner scanner=new Scanner(System.in);
System.out.print("Enter some numbers:");
while(true){
int n=scanner.nextInt();
if(n>max){
max=n;
count=1;
}else if(n==max){
count++;
}else if(n==0){
break;
}
}
System.out.println(max+":"+count);
}
}
import java.util.Scanner;
public class Demo35 {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.print("Enter the first string:");
String s1=scanner.nextLine();
System.out.print("Enter the second string:");
String s2=scanner.nextLine();
int i=0;
for(;i