//猜生日
public class Demo01 {
public static void main(String[] args) {
// TODO Auto-generated method stub
String set1 = "1 3 5 7"
+ "9 11 13 15"
+ "17 19 21 23"
+ "25 27 29 31";
String set2 = "2 3 6 7"
+ "10 11 14 15"
+ "18 19 22 23"
+ "26 27 30 31";
String set3 = " 4 5 6 7"
+ "12 13 14 15"
+ "20 21 22 23"
+ "28 29 30 31";
String set4 = "8 9 10 11"
+ "12 13 14 15"
+ "24 25 26 27"
+ "28 29 30 31";
String set5 = "16 17 18 19"
+ "20 21 22 23"
+ "24 25 26 27"
+ "28 29 30 31";
int day = 0;
Scanner input = new Scanner(System.in);
System.out.println("生日在序列1中吗?");
System.out.println(set1);
System.out.println("输入0代表不是,1代表是的");
int answer = input.nextInt();
if (answer == 1) {
day += 1;
}
System.out.println("生日在序列2中吗?");
System.out.println(set2);
System.out.println("输入0代表不是,1代表是的");
answer = input.nextInt();
if (answer == 1) {
day += 2;
}
System.out.println("生日在序列3中吗?");
System.out.println(set3);
System.out.println("输入0代表不是,1代表是的");
answer = input.nextInt();
if (answer == 1) {
day += 4;
}
System.out.println("生日在序列4中吗?");
System.out.println(set4);
System.out.println("输入0代表不是,1代表是的");
answer = input.nextInt();
if (answer == 1) {
day += 8;
}
System.out.println("生日在序列5中吗?");
System.out.println(set5);
System.out.println("输入0代表不是,1代表是的");
answer = input.nextInt();
if (answer == 1) {
day += 16;
}
System.out.println("你的生日为:" + day + " 号!");
}
}
import java.util.Scanner;
//将16进制转化为10进制
public class Demo02 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.println("输入一个十六进制的数字:");
String hexString = input.nextLine();
if (hexString.length() != 1) {
System.out.println("你必须输入具体的一个字符");
System.exit(1);//非正常退出
}
char ch = Character.toUpperCase(hexString.charAt(0));
if ('A' <= ch && ch <= 'F') {
int value = ch - 'A' + 10;
System.out.println("十进制的值为: " + value);
}else if (Character.isDigit(ch)) {
System.out.println("十进制的值为: " + ch);
}else {
System.out.println("无效的输入!");
}
}
}
import java.util.Scanner;
//LotteryUsingStrings
public class Demo03 {
public static void main(String[] args) {
// TODO Auto-generated method stub
String lottery = "" + (int)(Math.random() * 10) + (int)(Math.random() * 10);//字符串的拼接
Scanner input = new Scanner(System.in);
System.out.println("请输入两位数字");
String guess = input.nextLine();
char lotteryDigit1 = lottery.charAt(0);
char lotteryDigit2 = lottery.charAt(1);
char guessDigit1 = guess.charAt(0);
char guessDigit2 = guess.charAt(1);
System.out.println("彩票数字为:" + lottery);
if (guess.equals(lottery)) {
System.out.println("你赢得了$10000");
}else if (guessDigit1 == lotteryDigit2 && guessDigit2 ==lotteryDigit1) {
System.out.println("你赢得了$3000");
}else if (guessDigit1 == lotteryDigit1 || guessDigit1 == lotteryDigit2 || guessDigit2 == lotteryDigit1 || guessDigit2 == lotteryDigit2) {
System.out.println("你赢得了$1000");
}else {
System.out.println("很遗憾,您没有得奖!");
}
}
}
//格式化控制台输出
//无double类型
public class Demo04 {
public static void main(String[] args) {
// TODO Auto-generated method stub
double amount = 12618.98;
double interestRate = 0.0013;
double interest = amount * interestRate;
System.out.println("Interest is $" + interest);
//保留小数
System.out.println("interest is $" + (int)(interest * 100) / 100.0);
//下面使用printf来保留两位有效数字
System.out.printf("interest is $%4.2f",interest);
int count = 5;
double number = 45.56;
System.out.printf("count is %d and number is %f\n",count,number);
System.out.printf("%3d#%2s#%4.2f\n",1234,"java",51.6653);//会四舍五入
}
}
public class Demo05 {
public static void main(String[] args) {
// TODO Auto-generated method stub
//如果要显示一个带逗号的数字,可以在数字限定符前添加一个逗号
System.out.printf("%,8d %,10.1f\n",12345678,12345678.263);
//如果要在数字前面添加0而不是空格来凑齐位数,可以在一个数字限定符前添加0
System.out.printf("%08d %08.1f\n",1234,5.63);
//默认情况下,输出是右对齐的。可以在格式限定符中放一个(-)来指定输出是左对齐的
System.out.printf("%8d%8s%8.1f\n",1234,"java",5.63);
System.out.printf("%-8d%-8s%-8.1f\n",1234,"java",5.63);//让空格显示在右侧
//使用%.数字指定小数点后浮点数值
System.out.printf("%.2f",21.211);
}
}
//打印一张表
public class Demo06 {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.printf("%-10s%-10s%-10s%-10s%-10s\n","Degree","Radians","Sine","Cosine","Tangent");
int degrees = 30;
double radians = Math.toRadians(degrees);
System.out.printf("%-10d%-10.4f%-10.4f%-10.4f%-10.4f\n",degrees,radians,Math.sin(radians),Math.cos(radians),Math.tan(radians));
degrees = 60;
radians = Math.toRadians(degrees);
System.out.printf("%-10d%-10.4f%-10.4f%-10.4f%-10.4f\n",degrees,radians,Math.sin(radians),Math.cos(radians),Math.tan(radians));
}
}
public class Demo07 {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.printf("%5d %d\n",1,2,3);
System.out.printf("%.2f\n%.3f%s",1.23456,2.34,"11\n");
System.out.printf("%08d\n",11);
System.out.printf("%f%e\n",32.32,32.32);//科学计数法小数点前默认大于等于1小于10
System.out.printf("%5.2f%%%%5.4e\n",32.327,32.32);
System.out.printf("%6b\n",1 > 2);
System.out.printf("%,5d %,6.1f\n",312342,315562.932);
System.out.printf("%05d %06.1f\n",32,32.32);
}
}
import java.util.Scanner;
//4.2计算五边形的面积
public class Demo08 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.println("请输入五边形中心到定点的距离:");
double r = input.nextDouble();
double s = 2 * r * Math.sin(Math.PI / 5);
double area = 5 * s * s / (4 * Math.tan(Math.PI / 5));
System.out.println("五边形的面积为: " + (int)(area * 100) / 100.0);
}
}
import java.util.Scanner;
//4.2最大圆距离
public class Demo09 {
public static void main(String[] args) {
// TODO Auto-generated method stub
final double R = 6371.07;
Scanner input = new Scanner(System.in);
System.out.println("请输入第一个点的纬度和经度:");
double x1 = Math.toRadians(input.nextDouble());
double y1 = Math.toRadians(input.nextDouble());
System.out.println("请输入第二个点的维度和经度:");
double x2 = Math.toRadians(input.nextDouble());
double y2 = Math.toRadians(input.nextDouble());
double d = R * Math.acos(Math.sin(x1) * Math.sin(x2) + Math.cos(x1) * Math.cos(x2) * Math.cos(y1 - y2));
System.out.println("最大圆距离为:" + (int)(d * 100) / 100.0);
}
}
import java.util.Scanner;
//4.4六边形面积
public class Demo10 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.println("请输入六边形中心到定点的距离:");
double r = input.nextDouble();
double s = 2 * r * Math.sin(Math.PI / 6);
double area = 6 * s * s / (4 * Math.tan(Math.PI / 6));
System.out.println("六边形的面积为: " + (int)(area * 100) / 100.0);
}
}
//圆上的随机点
public class Demo11 {
public static void main(String[] args) {
// TODO Auto-generated method stub
final double R = 40.0;
double erfa = Math.random() * 2 * Math.PI;
double beita = Math.random() * 2 * Math.PI;
double gama = Math.random() * 2 * Math.PI;
double x1 = R * Math.cos(erfa);
double y1 = R * Math.sin(erfa);
double x2 = R * Math.cos(beita);
double y2 = R * Math.sin(beita);
double x3 = R * Math.cos(gama);
double y3 = R * Math.sin(gama);
double a = Math.pow(Math.pow(x3 - x2, 2) + Math.pow(y3 - y2, 2), 0.5);
double b = Math.pow(Math.pow(x3 - x1, 2) + Math.pow(y3 - y1, 2), 0.5);
double c = Math.pow(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2), 0.5);
double A = Math.toDegrees(Math.acos((b * b + c * c - a * a) / (2 * b * c)));
double B = Math.toDegrees(Math.acos((a * a + c * c - b * b) / (2 * a * c)));
double C = Math.toDegrees(Math.acos((b * b + a * a - c * c) / (2 * b * a)));
System.out.println("角度A为: " + A);
System.out.println("角度B为: " + B);
System.out.println("角度C为: " + C);
}
}
2021-01-27
最新推荐文章于 2022-04-18 12:16:06 发布