Class1、财务应用程序:复利值(输入输出,加减乘除运算)
import java.util.Scanner;
public class Class07 {
//定义年利率
public static double RATE=0.05;
public static void main(String [] args){
Scanner sc=new Scanner(System.in);
System.out.print("请输入每个月要存的钱");
double x=sc.nextDouble();
double m=0;
for (int i=1;i<=12;i++) {
double n=1+RATE/12;
m=(x+m)*n;
System.out.println("第"+i+"个月之后的账户值"+m);
}
}
}
Class2、显示时间(模运算)
public class Class08 {
public static void main(String [] args) {
long times=System.currentTimeMillis();
System.out.println(times);
long second=times/1000;
System.out.println(second);
long minutes=times/60;
System.out.println(minutes);
long day=times/(1000*60*60*24);
System.out.println(day);
// 获取的当前时间距离1970.01.01 00:00:00的毫秒数 System.currentTimeMillis();
// 获取是纳秒数 System.nanoTime();
}
}
Class3、整钱兑零(模运算)
import java.util.Scanner;
public class Class09 {
public static void main (String [] args) {
Scanner sc=new Scanner(System.in);
System.out.println("请输入一个十进制数:");
double num=sc.nextDouble();
int f=(int)num*100;
int dollar=f/100;
int querater=(f%100)/25;
int dime=(f%100)%25/10;
int nickel=((f%100)%25)%10/5;
int penny=(((f%100)%25)%10)%5;
System.out.println(penny);
}
}
Class4.计算身体质量指数(if语句)
import java.util.Scanner;
public class Class11 {
public static void main(String [] args) {
Scanner sc=new Scanner(System.in);
System.out.print("请输入以英镑为单位的体重");
double b=sc.nextDouble();
System.out.print("请输入以英寸为单位的身高");
double c=sc.nextDouble();
double g=b/0.453;
double m=c/0.025;
double BMI=(g/m)*(g/m);
System.out.println("BMI="+BMI);
Judge(BMI);
}
public static void Judge(double BMI) {
if(BMI<18.5) {
System.out.println("偏瘦");
}else if(BMI>=18.5&&BMI<25.0) {
System.out.println("正常");
}else if(BMI>=25.0&&BMI<30.0) {
System.out.println("超重");
}else {
System.out.println("过胖");
}
}
}
Class5.判定闰年(if语句,逻辑运算)
import java.util.Scanner;
public class Class12 {
public static void main(String [] args) {
Scanner sc=new Scanner(System.in);
System.out.println("请输入年份:");
int year=sc.nextInt();
Judge(year);
}
public static void Judge(int y) {
if((y%4==0&&y%100!=0)||y%400==0) {
System.out.println(y+"是闰年");
}else {
System.out.println(y+"是平年");
}
}
}
Class6、彩票(if语句,模运算,逻辑运算)
public class Class13 {
public static void main(String [] args) {
Scanner sc=new Scanner(System.in);
System.out.println("请输入一个数:");
int num=sc.nextInt();
int num2 = (int)(Math.random()*100);
System.out.println(num2);
int o=num/10;
int t=num%10;
int o2=num2/10;
int t2=num%10;
if(num==num2) {
System.out.println("恭喜你获得10000美元");
}else if(o==t2&&t==o2){
System.out.println("恭喜你获得3000美元");
}else if((o==o2&&t!=t2)||(o!=o2&&t==t2)&&(o==t2&&t!=o2)||(o!=t2&&t==o2)){
System.out.println("恭喜你获得1000美元");
}else {
System.out.println("和遗憾,没有中奖!!!");
}
}
}
Class7.代数:解一元二次方程(if语句,Math类)
package com.lab.kaifa.example;
import java.util.Scanner;
public class Class14 {
public static void main (String [] args) {
Scanner sc=new Scanner(System.in);
System.out.println("请输入a");
double a=sc.nextDouble();
System.out.println("请输入b");
double b=sc.nextDouble();
System.out.println("请输入b");
double c=sc.nextDouble();
double p=Math.sqrt(b*b-(4*a*c));
double r1=(-b+p)/(2*a);
double r2=(-b-p)/(2*a);
if(b*b-(4*a*c)>0) {
System.out.println("此方程有两个实数根,他的根是"+r1+"和"+r2);
}else if(b*b-(4*a*c)==0) {
System.out.println("此方程有一个实根,他的根是r1=r2"+r1);
}else if(b*b-(4*a*c)<0) {
System.out.println("此方程无实数根");
}
}
}
Class8.代数:求解2×2线程方程(格式化输出)
import java.util.Scanner;
public class Class15 {
public static void main(String [] args) {
Scanner sc=new Scanner(System.in);
System.out.print("请输入a:");
int a=sc.nextInt();
System.out.print("请输入b:");
int b=sc.nextInt();
System.out.print("请输入c:");
int c=sc.nextInt();
System.out.print("请输入d:");
int d=sc.nextInt();
System.out.print("请输入e:");
int e=sc.nextInt();
System.out.print("请输入f:");
int f=sc.nextInt();
Judge(a,b,c,d,e,f);
}
public static int Num1(int a,int b,int c,int d,int e,int f) {
return (e*d-b*f)/(a*d-b*c);
}
public static int Num2(int a,int b,int c,int d,int e,int f) {
return (a*f-e*c)/(a*d-b*c);
}
public static void Judge(int a,int b,int c,int d,int e,int f) {
if((a*d-b*c)==0) {
System.out.println("The equation has no solution(此方程无解)");
}else if((a*d-b*c)!=0){
int x=Num1(a,b,c,d,e,f);
int y=Num2(a,b,c,d,e,f);
System.out.println("x="+x+" y="+y);
}
}
}
Class9.找到将来的日期(switch语句,模运算)
import java.util.Scanner;
public class Class16 {
public static void main(String [] args) {
Scanner sc=new Scanner(System.in);
System.out.println("输入今天的星期数:");
int today=sc.nextInt();
while(today<0&&today>6) {
System.out.println("输入今天的星期数:");
today=sc.nextInt();
}
System.out.println("输入今天之后的天数:");
int next=sc.nextInt();
Forecast(today,next);
}
public static void Forecast(int a,int b) {
int today=a;
switch(b) {
case 1:
System.out.println("这一天是星期"+(a+1-7));
break;
case 2:
System.out.println("这一天是星期"+(a+2-7));
break;
case 3:
System.out.println("这一天是星期"+(a+3-7));
break;
case 4:
System.out.println("这一天是星期"+(a+4-7));
break;
case 5:
System.out.println("这一天是星期"+(a+5-7));
break;
case 6:
System.out.println("这一天是星期"+(a+6-7));
break;
case 0:
System.out.println("这一天是星期"+a);
break;
}
}
}
Class10、.给出一个月的总天数(switch语句)
import java.util.Scanner;
public class Class17 {
public static void main(String [] args) {
Scanner sc=new Scanner(System.in);
System.out.println("请输入年份");
int year=sc.nextInt();
System.out.println("请输入月份");
int month=sc.nextInt();
Stamp(year,month);
}
public static void Stamp(int a,int b) {
switch(b) {
case 1:
System.out.println(b+"月有31天");
break;
case 2:{
if((a%4==0&&a%100!=0)||a%400==0) {
System.out.println(b+"月有28天");
}else {
System.out.println(b+"月有29天");
}
}
break;
case 3:
System.out.println(b+"月有31天");
break;
case 4:
System.out.println(b+"月有30天");
break;
case 5:
System.out.println(b+"月有31天");
break;
case 6:
System.out.println(b+"月有30天");
break;
case 7:
System.out.println(b+"月有31天");
break;
case 8:
System.out.println(b+"月有31天");
break;
case 9:
System.out.println(b+"月有30天");
break;
case 10:
System.out.println(b+"月有31天");
break;
case 11:
System.out.println(b+"月有30天");
break;
case 12:
System.out.println(b+"月有31天");
break;
default:
System.out.println("月份输入错误!请刷新重新输入!");
}
}
}
Class11.回文数字(if语句,模运算)
import java.util.Scanner;
public class Class18 {
public static void main(String [] args) {
Scanner sc=new Scanner(System.in);
System.out.println("请输入一个三位数:");
int num=sc.nextInt();
Judge(num);
}
public static void Judge(int a) {
int b=a/100;//判断百位数
int s=(a-b*100)/10;//判断十位数
int g=(a-(b*100)-s*10);//判断个位数
if(b==g) {
System.out.println("这个数是回文数");
}else {
System.out.println("这个数不是回文数");
}
}
}
Class12.几何:点是否在圆内(if语句,Math类)
import java.util.Scanner;
public class Class19 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in );
System.out.print("请输入点坐标x");
double x=sc.nextInt();
System.out.print("请输入点坐标y");
double y=sc.nextInt();
Judge(x,y);
}
public static void Judge(double a,double b) {
double r=10;
double a2=0;
double b2=0;
if((Math.sqrt((a-a2)*(a-a2)+(b-b2)*(b-b2)))<10){
System.out.println("("+a+","+b+")在圆内");
}else if(((Math.sqrt(a-a2)*(a-a2)+(b-b2)*(b-b2)))>10) {
System.out.println("("+a+","+b+")在圆外");
}
}
}
Class13.计算三角形的角度(Math类)
import java.util.Scanner;
public class Class21 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.print("请输入三角形第一个点坐标x,y");
double x1=sc.nextDouble();
double y1=sc.nextDouble();
System.out.print("请输入三角形第二个点坐标x,y");
double x2=sc.nextDouble();
double y2=sc.nextDouble();
System.out.print("请输入三角形第三个点坐标x,y");
double x3=sc.nextDouble();
double y3=sc.nextDouble();
double b=Math.sqrt((x3-x1)*(x3-x1)+(y3-y1)*(y3-y1));
double a=Math.sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2));
double c=Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
double A=a*Math.cos(a*a-b*b-c*c)/(-2*b*c);
double B=a*Math.cos(b*b-a*a-c*c)/(-2*a*c);
double C=a*Math.cos(c*c-a*a-b*b)/(-2*a*b);
System.out.println(A);
System.out.println(B);
System.out.println(C);
}
}
Class14.几何:两个圆(if语句,Math类)
import java.util.Scanner;
public class Class20 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
Point p1=new Point();
Point p2=new Point();
System.out.println("请输入第一个圆的坐标x,y和半径r1");
double r1=sc.nextDouble();
p1.x=sc.nextDouble();
p1.y=sc.nextDouble();
System.out.println("请输入第二个圆的坐标x,y和半径r2");
double r2=sc.nextDouble();
p2.x=sc.nextDouble();
p2.y=sc.nextDouble();
double distance=p1.jili(p2);
System.out.println("圆心之间距离为:"+distance);
Judge(distance,r1,r2);
}
public static void Judge(double distance,double r1,double r2 ) {
if(r1+r2>=distance) {
System.out.println("重叠");
}else if(Math.abs(r1-r2)>=distance){
System.out.println("圆2在圆1内");
}
}
}
//定义一个圆心的类
class Point{
double x;
double y;
public double jili(Point p) {
return Math.sqrt((x-p.x)*(x-p.x)+(y-p.y)*(y-p.y));
}
}
Class15.十进制转十六进制(switch语句,String)
import java.util.Scanner;
public class Class26 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("请输入一个0-15的数");
int x=sc.nextInt();
if(x<=0&&x<10) {
System.out.println(x+"的十六进制是"+x);
}else if(x==10) {
System.out.println(x+"的十六进制是a");
}else if(x==11){
System.out.println(x+"的十六进制是b");
}else if(x==12){
System.out.println(x+"的十六进制是c");
}else if(x==13){
System.out.println(x+"的十六进制是d");
}else if(x==14){
System.out.println(x+"的十六进制是e");
}else if(x==15){
System.out.println(x+"的十六进制是f");
}
}
}
Class16.给出ASCII码对应的字符(char,String)
import java.util.Scanner;
public class Class24 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("请输入0~127之间的一个整数:");
char a = (char) sc.nextInt();
System.out.println("该数字所对应的字符是:"+a);
}
}
Class17.几何:正多边形的面积(Math类)
import java.util.Scanner;
public class Class22 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("请输入边数");
int n=sc.nextInt();
System.out.println("请输入边长");
double s=sc.nextDouble();
double area=(n*s)/(4*Math.tan(n/s));
System.out.println(area);
}
}