Java 基础练习题

class Demo01{
	public static void main(String[] args){
   		System.out.println((9.5 * 4.5 - 2.5 * 3) / (45.5 - 3.5));
   		//常量直接进行运算即可
 	}
}

 

class Demo02{
	public static void main(String[] args){
		System.out.println((14 / 1.6) / (45.5 / 60));
		//与1.1类似
	}
}

class Home01{
		public static final int ADDNUM=365*24*60*60/7-365*24*60*60/13-365*24*60*60/45;
		//用final函数将每年变化的人口数定义为定值,用ADDNUM命名
		public static void main(String[] args){
			System.out.println("过了一年的人口数:"+(312032486+ADDNUM));        
			System.out.println("过了两年的人口数:"+(312032486+2*ADDNUM));        
			System.out.println("过了三年的人口数:"+(312032486+3*ADDNUM));        
			System.out.println("过了四年的人口数:"+(312032486+4*ADDNUM));        
			System.out.println("过了五年的人口数:"+(312032486+5*ADDNUM));
			//+有连结和运算两种作用,双引号"后的+是起连结作用,两个数字用+运算时、若有其他的+起连结作用时、该运算要用括号()括起来、否则就起连结作用
		}
}

class Demo03{
	public static void main(String[] args){        
		System.out.println(((24 * 1.6) / (1 + (40 + 35.0 / 60) / 60)));
		//该题与上题稍稍有点变化,35.0/60此处两整数相除需将一个整数变为浮点数,否则会整除
	}
}

 

class Home02{
	public static void main(String[] args){
		System.out.println("x="+(44.5*0.55-50.2*5.9)/(3.4*0.55-50.2*2.1)+" y="+(3.4*5.9-44.5*2.1)/(3.4*0.55-50.2*2.1));
	}
}

 

import java.util.Scanner;//导入Java的Scanner包,Scanner是控制输入的一个类
class Demo02_01{
    public static void main(String[] args){
        Scanner input=new Scanner(System.in);//可以从控制台输入数据
        double celsius;
        System.out.print("Enter a number for Celsius:");//提示用户输入摄氏度
        celsius=input.nextDouble();//键盘输入
        double fahrenheit=celsius * (9.0/5) + 32;//华氏度与摄氏度之间的转换公式
        System.out.print(celsius +" Celsius is "+ fahrenheit +" Fahrenheit");//输出结果
    }
}

import java.util.Scanner;
class Demo02_02{
    public static void main(String[] args){
        Scanner input=new Scanner(System.in);
        System.out.print("Enter the radius and length of a cylinder:");
        double radius=input.nextDouble();
        double length=input.nextDouble();
        double area=radius*radius*3.14;
        double volume=area*length;
        System.out.println("The area is "+area);
        System.out.println("The volum is "+volume);
    }
}

import java.util.Scanner;
class Demo02_03{
    public static void main(String[] args){
        Scanner input=new Scanner(System.in);
        System.out.print("Enter a number between 0 and 1000:");
        int number=input.nextInt();
        int a=number%10;        //取出个位数
        int b=number/10%10;     //取出十位数
        int c=number/100%10;    //取出百位数
        int sum=a+b+c;          
        System.out.print("The sum of the digits is "+sum);
    }
}

import java.util.Scanner;
class Demo02_04{
    public static void main(String[] args){
        Scanner input=new Scanner(System.in);
        System.out.print("Enter the time zone offset to GMT:");
        int gmt = input.nextInt();    //输入时区
        long totalMilliseconds = System.currentTimeMillis(); //获取从1970年1月1日0:00到当前经历的毫秒数
        long totalSecond = totalMilliseconds / 1000;
        long totalMinute = totalSecond /60;
        long totalHour = totalMinute /60;
        long nowSecond = totalSecond %60;
        long nowMinute = totalMinute %60;
        long nowHour = (totalHour + gmt)%24;
        System.out.print("The current time is "+ nowHour +":"+ nowMinute +":"+ nowSecond);
    }
}

import java.util.Scanner;
class Demo02_05{
    public static void main(String[] args){
        Scanner input=new Scanner(System.in);
        System.out.print("Enter the monthly saving amount:");
        double dollers=input.nextDouble();
        double sumDollers=0;
        for(int i=1 ; i<=6 ; i++){
            sumDollers=(sumDollers + dollers)*(1 + 0.00417);
        }
        System.out.print("六个月的总额为"+ sumDollers);
    }
}

import java.util.Scanner;
class Demo02_06{
    public static void main(String[] args){
        Scanner input=new Scanner(System.in);
        System.out.print("Enter x1 and y1:");
        double x1=input.nextDouble();
        double y1=input.nextDouble();
        System.out.print("Enter x2 and y2:");
        double x2=input.nextDouble();
        double y2=input.nextDouble();
        double distance=Math.pow((x1-x2) * (x1-x2) + (y1-y2) * (y1-y2),0.5);
        System.out.print("The distance between the two points is "+ distance);
    }
}

import java.util.Scanner;
class Demo02_07{
    public static void main(String[] args){
        Scanner input=new Scanner(System.in);
        System.out.print("Enter three points for a triangle:");
        double x1=input.nextDouble();
        double y1=input.nextDouble();
        double x2=input.nextDouble();
        double y2=input.nextDouble();
        double x3=input.nextDouble();
        double y3=input.nextDouble();
        double l1=Math.pow((x1-x2) * (x1-x2) + (y1-y2) * (y1-y2),0.5);
        double l2=Math.pow((x1-x3) * (x1-x3) + (y1-y3) * (y1-y3),0.5);
        double l3=Math.pow((x3-x2) * (x3-x2) + (y3-y2) * (y3-y2),0.5);
        double s=(l1+l2+l3)/2;
        double area=Math.pow(s*(s-l1)*(s-l2)*(s-l3),0.5);
        System.out.print("The area of the triangle is "+ area);
    }
}

import java.util.Scanner;
class Demo03_01{
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a b c:");
        double a = input.nextDouble();
        double b = input.nextDouble();
        double c = input.nextDouble();
        double delt = b*b - 4*a*c;
        double x1 = (-b + Math.sqrt(delt)) / 2*a;
        double x2 = (-b - Math.sqrt(delt)) / 2*a;
        if(delt < 0){
            System.out.print("无解");
        }else if(delt > 0){
            System.out.printf("x1=%f,x2=%f\n",x1,x2);
        }else{
            System.out.printf("x=%f\n",x1);
        }
    }
}

import java.util.Scanner;
class Demo03_03{
    public static void main(String[] args){
            Scanner input = new Scanner(System.in);
            System.out.print("Enter today's day:");
            int today = input.nextInt();
            System.out.print("Enter the number of days elapsed since today:");
            int afterday = input.nextInt();
            int day=(today + afterday) % 7;
            switch(day){
                case 0:
                    System.out.print("周日");break;
                case 1:
                    System.out.print("周一");break;
                case 2:
                    System.out.print("周二");break;
                case 3:
                    System.out.print("周三");break;
                case 4:
                    System.out.print("周四");break;
                case 5:
                    System.out.print("周五");break;
                case 6:
                    System.out.print("周六");break;        
            }
    }
}

import java.util.Scanner;
class Demo03_04{
    public static void main(String[] args){
        Scanner scanner=new Scanner(System.in);
        System.out.print("请输入一个数字:");
        int num = scanner.nextInt();
        int temp = num;
        int sum = 0;
        while(num != 0){
            sum = sum + num % 10;
            num = num / 10;
        }
        if(sum == temp){
            System.out.println("num是回文");
        }else{
            System.out.println("num不是回文");
        }
    }
}

import java.util.Scanner;
import java.util.Random;
class Demo03_05{
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        System.out.print("输入一个两位数的彩票:");
        int numb = Random.nextInt(100);
        int scanNumb = input.nextInt();
        int a = scanNumb%10;
        int b = scanNumb/10;
        int a1 = numb%10;
        int b1 = numb/10;
        if(numb == scanNumb){
            System.out.print("您中了 10 000 美元!!");
            
        }else if(a==b1&&b==a1){
            System.out.print("您中了 3000 美元!!");
        }else if(a==b1||a==a1||b==a1||b==b1){
            System.out.print("您中了 1000 美元!!");
        }else{
            System.out.print("您未中奖");
        }
    }
}

import java.util.Scanner;
class Demo03_07{
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        int year = input.nextInt();
        int m = input.nextInt();
        int q = input.nextInt();
        if(m == 1||m == 2){
            m=m+12;
            year=year-1;
        }
        int k = year % 100;
        int j = year / 100;
        int h = (q + 26*(m + 1)/10 + k + k/4 + j/4 + 5*j)%7;
        switch(h){
            case 0:
            System.out.print("星期六");break;
            case 1:
            System.out.print("星期天");break;
            case 2:
            System.out.print("星期一");break;
            case 3:
            System.out.print("星期二");break;
            case 4:
            System.out.print("星期三");break;
            case 5:
            System.out.print("星期四");break;
            case 6:
            System.out.print("星期五");break;
        }
    }
}

import java.util.Scanner;
class Demo03_09{
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a point's x- and y- coordinates:");//提示用户输入点的坐标
        double x = input.nextDouble();
        double y = input.nextDouble();
        if(x>0 && y>0 && 100-1.0/2 *x < y){    //判断是否在三角形内,这里判断方法为线性规划
            System.out.print("在三角形内");    //在三角形内输出“在三角形内”的结果
        }else{
            System.out.print("在三角形外");    //在三角形外输出“在三角形外”的结果
        }
    }
}

import java.util.Scanner;
class Demo03_10{
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        System.out.print("Enter r1's center x-,y- coordinates,width,and height:");//提示输入第一个矩形r1的长、宽和中心坐标
        double x1=input.nextDouble();
        double y1=input.nextDouble();
        double wid1=input.nextDouble();
        double height1=input.nextDouble();
        System.out.print("Enter r2's center x-,y- coordinates,width,and height:");//提示输入第二个矩形r2的长、宽和中心坐标
        double x2=input.nextDouble();
        double y2=input.nextDouble();
        double wid2=input.nextDouble();
        double height2=input.nextDouble();
        if((wid1+wid2)/2 <= Math.abs(x1-x2) || (height1+height2)/2 <= Math.abs(y1-y2)){
            System.out.print("r2 does not overlap r1");
        }else if((wid1+wid2)/2 >= Math.abs(x1-x2) && (height1+height2)/2 >= Math.abs(y1-y2)){
            System.out.print("r2 is inside r1");
        }else{
            System.out.print("r2 is overlap r1");
        }
    }
}

 

import java.util.Scanner;
class Demo03_14{
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        System.out.print("请输入0~15之间的一个数:");
        int a = input.nextInt();
        for(int i=1;i<=a;i++){
            for(int j=1;j<=4*(a-i);j++){
                System.out.print(" ");
            }
            for(int k=1;k<=2*(i-1)+1;k++){
                int num = (int)(1+Math.abs(i-k));
                for(int kong = 1;kong<=3;kong++){    //控制每个数字前空格的输出,也可用格式化输出System.out.printf("%3d",num);正3向右对齐,负3向左对齐
                    System.out.print(" ");
                    if(num>=10){
                        kong++;
                    }
                }
                System.out.print(num);
            }
            System.out.println();
        }
    }
}

 

//图一
class Demo03_15_01{
    public static void main(String[] args){
        for(int i=1;i<=6;i++){
            for(int j=1;j<=i;j++){
                System.out.print(j+" ");
            }
            System.out.println();
        }
    }
}
//图二
class Demo03_15_02{
    public static void main(String[] args){
        for(int i=1;i<=6;i++){
            for(int j=1;j<=7-i;j++){
                System.out.print(j+" ");
            }
            System.out.println();
        }
    }
}
//图三
class Demo03_15_03{
    public static void main(String[] args){
        for(int i=1;i<=6;i++){
            for(int z=1;z<=2*(6-i);z++){
                System.out.print(" ");
            }
            for(int j=1;j<=i;j++){
                System.out.print(i-j+1 +" ");
            }
            System.out.println();
        }
    }
}
//图四
class Demo03_15_04{
    public static void main(String[] args){
        for(int i=1;i<=6;i++){
            for(int z=1;z<=2*(i-1);z++){
                System.out.print(" ");
            }
            for(int j=1;j<=7-i;j++){
                System.out.print(j+" ");
            }
            System.out.println();
        }
    }
}

 

 

/*
                                                            i   j=4*(8-i)   k(max)=1+2*(i-1)   
                            1                               1   4*7         1   1+2*(1-1)
                        1   2   1                           2   4*6         3   1+2*(2-1)
                    1   2   4   2   1                       3   4*5         5   1+2*(3-1)
                1   2   4   8   4   2   1                   4   4*4         7   1+2*(4-1)
            1   2   4   8  16   8   4   2   1               5   4*3         9   
        1   2   4   8  16  32  16   8   4   2   1           6   4*2         11  
    1   2   4   8  16  32  64  32  16   8   4   2   1       7   4*1         13  
1   2   4   8  16  32  64 128  64  32  16   8   4   2   1   8   4*0         15  

每行数字的规律(要找到num与k的关系):以最后一行为例当i=8时,
1   2   3   4   5   6   7   8   9   10  11  12  13  14  15  k的变化

2^0 2^1 2^2 2^3 2^4 2^5 2^6 2^7 2^6 2^5 2^4 2^3 2^2 2^1 2^0 num=2^x
0   1   2   3   4   5   6   7   6   5   4   3   2   1   0   x=7-|7-(k-1)|
7   6   5   4   3   2   1   0   1   2   3   4   5   6   7
0   1   2   3   4   5   6   7   8   9   10  11  12  13  14  k-1
每行的规律:
x=(maxk-1/2)-|(maxk-1/2)-(k-1)|
b=(maxk-1/2)
x=b-|b-(k-1)|
num=2^x

*/

 代码实现如下:

class Demo03_16{
    public static void main(String[] args){
        for(int i=1;i<=8;i++){            //一行一次循环,是最外层的循环
            for(int j=1;j<=(8-i)*4;j++){    //控制每行第一个数字前的空格输出
                System.out.print(" ");
            }
            for(int k=1;k<=1+2*(i-1);k++){    //这里的代码实现说明在上图中
                int maxK = 1+2*(i-1);
                int b = (maxK-1)/2;
                int x = b-Math.abs(b-(k-1));
                int num = (int)Math.pow(2,x);
                for(int a=1;a<=3;a++){        //控制每个数字前空格的输出,也可用格式化输出System.out.printf("%3d",num);正3向右对齐,负3向左对齐
                    if(k==1)break;            //每行的第一个数前不额外输出空格,判断是否为第一个数,若是则break进行下一个数前的空格输出
                    System.out.print(" ");    //输出一个空格,根据循环的次数输出空格的个数
                    if(num>=10){
                        a++;        //当num大于10时,a+1使循环次数少一次
                        if(num>=100){
                            a++;    //当num大于100时,a再+1使循环次数再少一次
                        }
                    }
                }
                System.out.print(num);//空格后输出num
            }
            System.out.println();//一行num输出完后换行
        }
    }
}

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值