import java.util.Scanner;
public class Demo17{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("请输入以身高(英镑)和体重(英寸)");
double pounds = input.nextDouble();
double inches = input.nextDouble();
double kilogram = pounds * 0.45359237 ;
double meter = inches * 0.0254;
double BMI = kilogram / meter;
System.out.println(BMI);
if(BMI<18.5){
System.out.println("偏瘦");
}else if(BMI<25.0){
System.out.println("正常");
}else if(BMI<30.0){
System.out.println("超重");
}else{
System.out.println("过胖");
}
}
}
import java.util.Scanner;
public class Demo18{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入年份");
int year = input.nextInt();
if((year % 4 == 0 && year % 100 !=0) || year % 400 ==0){
System.out.println("是闰年");
}else{
System.out.println("不是闰年");
}
}
}
import java.util.Scanner;
public class Demo19{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("请输入一个两位数");
int num = input.nextInt();
int c = num % 10 ;//个位
int d = num / 10 ;//十位
int ran = (int)(Math.random() * 100);
int a = ran % 10;//个位
int b = ran / 10;//十位
System.out.println(b+""+a);
if(a == c && a == d){
System.out.println("10000美元");
}else if(c == b && d==a){
System.out.println("3000美元");
}else if((a == c && b != d) || ( a != c && b ==d)){
System.out.println("1000美元");
}else{
System.out.println("没中奖");
}
}
}
import java.util.Scanner;
public class Demo20{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Enter a,b,c: ");
double a = input.nextDouble();
double b = input.nextDouble();
double c = input.nextDouble();
double delt = Math.pow(b,2) - 4 * a * c;
if(delt > 0 ){
double r1 = (-b + Math.sqrt(delt)) / (2 * a);
double r2 = (-b - Math.sqrt(delt)) / (2 * a);
System.out.println("The equation has two roots "+r1+"and"+r2);
}else if(delt == 0){
double r = -b / (2 * a);
System.out.println("The equation has one root "+r);
}else{
System.out.println("The equation has no real roots");
}
}
}
import java.util.Scanner;
public class Demo21{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Enter a,b,c,d,e,f: ");
double a = input.nextDouble();
double b = input.nextDouble();
double c = input.nextDouble();
double d = input.nextDouble();
double e = input.nextDouble();
double f = input.nextDouble();
double yy = a * d - b * c ;
double x = (e * d - b * f) / yy;
double y = (a * f - e * c) / yy;
if(yy == 0){
System.out.println("The equation has no solution");
}else{
System.out.println("x is "+x+" and y is "+y);
}
}
}
import java.util.Scanner;
public class Demo22{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Enter today's day: ");
int day = input.nextInt();
System.out.println("Enter the number of days elapsed since today: ");
int nextday = input.nextInt();
if(day == 0){
System.out.print("Today is Sunday");
}else if(day == 1){
System.out.print("Today is Monday");
}else if(day == 2){
System.out.print("Today is Tuesday");
}else if(day == 3){
System.out.print("Today is Wednesday");
}else if(day == 4){
System.out.print("Today is Thursday");
}else if(day == 5){
System.out.print("Today is Friday");
}else{
System.out.print("Today is Saturday");
}
int today = (day + nextday) % 7 ;
if(today == 0){
System.out.print(" and the future day is Sunday");
}else if(today == 1){
System.out.print(" and the future day is Monday");
}else if(today == 2){
System.out.print(" and the future day is Tuesday");
}else if(today == 3){
System.out.print(" and the future day is Wednesday");
}else if(today == 4){
System.out.print(" and the future day is Thursday");
}else if(today == 5){
System.out.print(" and the future day is Friday");
}else{
System.out.print(" and the future day is Saturday");
}
}
}
import java.util.Scanner;
public class Demo23{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Enter a three-digit integer: ");
int pal = input.nextInt();
int pel = pal;
int a = pel % 10;
pel /= 10;
int b = pel % 10;
pel /= 10;
int c = pel;
if(a == c ){
System.out.println(pal+" is a palindrome");
}else{
System.out.println(pal+" is not a palindrome");
}
}
}
import java.util.Scanner;
public class Demo24{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("scissor(0),rock(1),paper(2)");
int num = input.nextInt();
int com = (int)(Math.random() * 3);
while(num == 0){
if(com == 0){
System.out.print("The computer is scissor. You are scissor. It is a draw");
break;
}else if(com == 1){
System.out.print("The computer is rock. You are scissor. you lost ");
break;
}else{
System.out.print("The computer is paper. You are scissor. you won");
break;
}
}
while(num == 1){
if(com == 0){
System.out.print("The computer is scissor. You are scissor. you won");
break;
}else if(com == 1){
System.out.print("The computer is rock. You are scissor. It is a draw");
break;
}else{
System.out.print("The computer is paper. You are scissor. you lost");
break;
}
}
while(num == 2){
if(com == 0){
System.out.print("The computer is scissor. You are scissor. you lost");
break;
}else if(com == 1){
System.out.print("The computer is rock. You are scissor. you won");
break;
}else{
System.out.print("The computer is paper. You are scissor. It is a draw");
break;
}
}
}
}
import java.util.Scanner;
public class Demo25{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Enter year: ");//提示输入年份
int year = input.nextInt();
System.out.println("Enter month: ");//提示输入月份
int m = input.nextInt();
System.out.println("Enter the day of the month: ");//提示输入该月的某一天
int q = input.nextInt();
if (m == 1){ m = 13;year -= 1;} //如果为1月,则记为上一年的13
if (m == 2){ m = 14;year -= 1;}//如果为2月。则记为上一年的14
int j = Math.abs(year / 100);
int k = year % 100;
int h = ( q + ((26 * (m+1)) / 10) + k + k / 4 + j / 4 + 5 * j) % 7;
System.out.println(h);
if(h == 0){
System.out.println("Day of the week is Saturday");
}else if(h == 1){
System.out.println("Day of the week is Sunday");
}else if(h == 2){
System.out.println("Day of the week is Monday");
}else if(h == 3){
System.out.println("Day of the week is Tuesday");
}else if(h == 4){
System.out.println("Day of the week is Wednesday");
}else if(h == 5){
System.out.println("Day of the week is Thursday");
}else if(h == 6){
System.out.println("Day of the week is Friday");
}
}
}
import java.util.Scanner;
public class Demo26{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Enter a point with two coordinates: ");
double x = input.nextDouble();
double y = input.nextDouble();
double distance = Math.sqrt(Math.pow(x - 0,2) + Math.pow(y - 0,2));
if(distance <= 10){
System.out.println("Point ("+x+","+y+")is in the circle");
}else{
System.out.println("Point ("+x+","+y+")is not in the circle");
}
}
}
import java.util.Scanner;
public class Demo27{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Ener a point with two coordinates: ");
double x = input.nextDouble();
double y = input.nextDouble();
if( x <= 10/2 && y <= 5.0/2){
System.out.println("Point ("+ x +","+ y +") is in the rectangle");
}else{
System.out.println("Point ("+ x +","+ y +") is not in the rectangle");
}
}
}
import java.util.Scanner;
public class Demo28{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Enter a point's x- and y-coordinates: ");
double x = input.nextDouble();
double y = input.nextDouble();
double k = y / (200.0 - x);
if(k <= 100.0/200){
System.out.println("The point is in the triangle");
}else{
System.out.println("The point is not in the triangle");
}
}
}
import java.util.Scanner;
public class Demo29{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter r1's center x-,y-coordinates,width,and height: ");
double x1 = input.nextDouble();
double y1 = input.nextDouble();
double w1 = input.nextDouble();
double h1 = input.nextDouble();
System.out.println("Enter r2's center x-,y-coordinates,width,and height: ");
double x2 = input.nextDouble();
double y2 = input.nextDouble();
double w2 = input.nextDouble();
double h2 = input.nextDouble();
double distance = Math.sqrt(Math.pow(x2-x1,2) + Math.pow(y2-y1,2));
double distance1 = Math.sqrt(Math.pow(h1-h2,2) + Math.pow(w1-w2,2));
if(distance1 > distance){
System.out.println("r2 is inside r1");
}else if(distance1 < distance){
System.out.println("r2 does not overlap r1");
}else{
System.out.println("r2 overlaps r1");
}
}
}
import java.util.Scanner;
public class Demo30{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
//提示用户输入x1,y1,r1,y1,y2,r2.
System.out.println("Enter circle1's center x-,y-coordinates, and radius: ");
double x1 = input.nextDouble();
double y1 = input.nextDouble();
double r1 = input.nextDouble();
System.out.println("Enter circle2's center x-,y-coordinates, and radius: ");
double x2 = input.nextDouble();
double y2 = input.nextDouble();
double r2 = input.nextDouble();
//计算两个圆心的距离
double distance = Math.sqrt(Math.pow(x2-x1,2) + Math.pow(y2-y1,2));
if(distance <= Math.abs(r1-r2)){
System.out.println("circle2 is inside circle1");
}else if(distance <= (r1+ r2)){
System.out.println("circle2 overlaps circle1");
}else{
System.out.println("circle2 does not overlap circle1");
}
}
}
import java.util.Scanner;
public class Demo31{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Enter an integer: ");
int num = input.nextInt();
if(num % 5 == 0 && num % 6 ==0){
System.out.println("Is "+ num +" divisible by 5 and 6? true");
}else{
System.out.println("Is "+ num +" divisible by 5 and 6? false");
}
if(num % 5 == 0 || num % 6 ==0){
System.out.println("Is "+ num +" divisible by 5 or 6? true");
}else{
System.out.println("Is "+ num +" divisible by 5 or 6? false");
}
if((num % 5 == 0 && num % 6 !=0) || (num % 5 != 0 && num % 6 == 0)){
System.out.println("Is "+ num +" divisible by 5 or 6,but not both? true");
}else{
System.out.println("Is "+ num +" divisible by 5 or 6,but not both? false");
}
}
}
import java.util.Scanner;
public class Demo32{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Guess a magic number between 0 and 100");
//产生随机数字
int com = (int)(Math.random() * 101);
while(true){
//提示用户输入数字
System.out.println("Enter your guess: ");
int num = input.nextInt();
if(num < com){
System.out.println("Your guess is too low");
}else if(num > com){
System.out.println("Your guess is too high");
}else{
System.out.println("Yes,the number is "+ num);
break;
}
}
}
}
import java.util.Scanner;
public class Demo33{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
//提示用户输入n1和n2
System.out.println("Enter n1 , n2");
int n1 = input.nextInt();
int n2 = input.nextInt();
//用三位运算找出最小的数
int min = n1 < n2? n1 : n2;
//给变量gcd赋值1
int gcd = 1;
//遍历循环
for (int k = 2 ; k <= min; k++){
if(n2 % k == 0 && n1 % k ==0){
gcd = k;
}
}
System.out.println("最大公约数为: "+gcd);
}
}
将十进制转换为十六进制
import java.util.Scanner;
public class Demo34{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
//1.提示用户输入十进制数
System.out.println("输出十制数");
int num = input.nextInt();
//2.创建一个空字符串
String s = "";
//将十进制转换为十六进制
while(num != 0){
int hexValue = num % 16 ;
//三目运算符,如果是真的,取冒号左边的,假的取冒号右边的
char hexDigit = (hexValue >=0 && hexValue <=9)?(char)(hexValue + '0'): (char)(hexValue -10 + 'A');
//拼接字符串
s = hexDigit + s;
num /= 16;
}
System.out.println("十六进制数为: "+s);
}
}
import java.util.Scanner;
public class Demo35{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Enter a word");
String word = input.nextLine();
int l = 0;
int r = word.length() - 1;
boolean flag = true;
while(l < r){
if(word.charAt(l) == word.charAt(r)){
l++;
r--;
}else{
flag = false;
break;
}
}
if(flag){
System.out.println(word+"这句话是回文");
}else{
System.out.println(word+"这句话不是回文");
}
}
}
public class Demo36{
public static void main(String[] args){
int count = 0;
int number = 2 ;
while(count<50){
boolean flag = true;
for(int i = 2 ; i <= number/2; i++){
if(number % i == 0){
flag = false;
break;
}
}
if(flag){
System.out.print(number + "\t");
count++;
if(number % 10 == 0){
System.out.println();
}
}
number++;
}
}
}
import java.util.Scanner;
public class Demo37{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Enter an integer,the input ends if it is 0:" );
int n ,count1 = 0,count2 = 0;
double ave,sum = 0,count = 0;
n = input.nextInt();
while(n!= 0){
sum += n;
++count;
if(n>0){
++count1;
}
if(n<0){
++count2;
}
n = input.nextInt();
}
System.out.println("The number of positives is : "+count1);
System.out.println("The number of negatives is : "+count2);
System.out.println("The total is : "+sum);
ave = sum / count;
System.out.println("The average is : "+ave);
}
}
public class Demo38{
public static void main(String[] args){
int count = 0;
for(int i = 100 ; i <=1000 ; i++){
if(i % 5 ==0 && i % 6 ==0){
count++;
if(count % 10 ==0){
count = 0;
System.out.println(i);
}else{
System.out.print(i+"\t");
}
}
}
}
}
public class Demo39{
public static void main(String[] args){
int count = 0;
for(int i = 100; i <= 200 ; i++){
if((i % 5 == 0 && i % 6 != 0) || (i % 5 != 0 && i % 6 == 0)){
count++;
if(count % 10 ==0){
System.out.println(i);
}else{
System.out.print(i+"\t");
}
}
}
}
}
import java.util.Scanner;
public class Demo40{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Enter a number");
int number = input.nextInt();
while(true){
boolean flag = true;
for(int i = 2 ; i <= number / 2 ; i++){
if(number % i == 0){
System.out.print(i+"\t");
flag = false;
number /= i;
break;
}
}
if(flag){
System.out.println(number);
break;
}
}
}
}
import java.util.Scanner;
public class Demo41{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Enter a number(1~15):");
int number = input.nextInt();
// 1
// 2 1 2
// 3 2 1 2 3
// 4 3 2 1 2 3 4
//-3 -2 -1 0 1 2 3 x∈(-(i-1),i-1) f(x)=|i|+1
for(int i = 1 ; i<= number ; i++){
for(int k = 1; k < number - i;k++){
System.out.print(" ");
}
for(int j = 1-i ;j <= i-1 ; j++){
int num = Math.abs(j)+1;
System.out.printf("%3d",num);
}
System.out.println();
}
}
}
//图案1
public class Demo42{
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();
}
}
}
//图案2
public class Demo42{
public static void main(String[] args){
for(int i = 6 ; i >0 ;i--){
for(int j = 1 ; j <= i;j++){
System.out.print(j+" ");
}
System.out.println();
}
}
}
//图案3
public class Demo42{
public static void main(String[] args){
for(int i = 1 ; i <= 6 ;i++){
for(int k = 1 ; k<= 6-i;k++){
System.out.print(" ");
}
for(int j = i; j>=1;j--){
System.out.printf("%d",j);
System.out.print(" ");
}
System.out.println();
}
}
}
//图案4
public class Demo42{
public static void main(String[] args){
for(int i = 6 ; i> 0 ; i--){
for(int k = 1 ;k <=6 - i;k++){
System.out.print(" ");
}
for(int j = 1 ; j <= i ;j++){
System.out.printf("%d",j);
System.out.print(" ");
}
System.out.println();
}
}
}
public class Demo43{
public static void main(String[] args){
// 0
// 0 1 0
// 0 1 2 1 0
//0 1 2 3 2 1 0
//-3 -2 -1 0 1 2 3 f(x)=|i|-1-|x| x∈(-(i-1),i-1))
for(int i = 1;i <= 8 ; i++){
for(int k = 1; k <= 8 -i;k++){
System.out.print(" ");
}
for(int j= 1-i; j <= i-1 ;j++ ){
int num =(int)(Math.pow(2,Math.abs(i)-1-Math.abs(j)));
System.out.printf("%-4d",num);
}
System.out.println();
}
}
}
public class Demo44{
public static void main(String[] args){
int count = 0;//素数的个数
for(int i = 2;i <=1000 ;i++){
boolean flag = true;
for(int j = 2 ;j <= i/2;j++){
if(i % j ==0){
flag = false;
break;
}
}
if(flag){
System.out.print(i +"\t");
count++;
if(count % 8 == 0){
System.out.println();
}
}
}
}
}
public class Demo45{
public static void main(String[] args){
double sum = 0;
for(double i = 1.0 ; i <= 97 ;i+=2){
double j = i+2;
sum += i / j ;
}
System.out.print(sum);
}
}
import java.util.Scanner;
public class Demo46{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Enter n");
double n = input.nextDouble();
double number =0;
for(int i = 1 ; i <=n;i++ ){
number += Math.pow(-1,i+1) / (2 * i - 1);
}
System.out.println("PI="+number * 4);
}
}
import java.util.Scanner;
public class Demo47{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Enter");
double i = input.nextDouble();
double e = 1;
double item = 1;
for(int j = 1 ; j <=i ;j++){
item *= j;
e += 1.0 / item;
}
System.out.println(e);
}
}
public class Demo48{
public static void main(String[] args){
for(int number = 1; number <= 10000 ;number++){
int sum = 0 ;
for(int j = 1 ; j <= number / 2 ; j++ ){
if(number % j == 0){
sum += j;
}
}
if(sum == number){
System.out.println(sum);
}
}
}
}
import java.util.*;
public class Demo49{
public static void main(String[] args){
String []arr = {"石头","剪刀","布"};//0 1 2 arr[index]
Scanner input = new Scanner(System.in);
int usrCount = 0;
int comCount = 0 ;
while(true){
//1.获取一个数字0~2
System.out.println("剪刀(0) 石头(1) 布(2):");
int usr = input.nextInt();
//2.随机一个数字
Random random = new Random();
int com = random.nextInt(3);
//3.开始比较
if(usr == com){
//平局
System.out.println("用户是"+ arr[usr]+",电脑是"+arr[com]+",平局");
}else if (usr - com == 1 || usr - com == -2){
//用户赢
System.out.println("用户是"+arr[usr]+",电脑是"+arr[com]+",用户赢");
usrCount++;
}else{
//电脑赢
System.out.println("用户是"+arr[usr]+",电脑是"+arr[com]+",电脑赢");
comCount++;
}
if(usrCount == 2|| comCount == 2){
break;
}
}
if(usrCount == 2){
System.out.println("最终,用户胜利!");
}else{
System.out.println("最终,电脑胜利");
}
}
}
import java.util.Scanner;
public class Demo50{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("请输入十进制");
int num = input.nextInt();
String binnum = "";
while(num != 0){
binnum = num % 2 + binnum;
num /= 2;
}
System.out.println(binnum);
}
}
import java.util.Scanner;
public class Demo51{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("请输入十进制");
int num = input.nextInt();
String binNum = "";
while(num != 0){
binNum = num % 8 + binNum;
num /= 8;
}
System.out.println(binNum);
}
}
import java.util.Scanner;
class Demo52{
public static void main(String[] args){
int count = 0;
int max = 0;
Scanner input = new Scanner(System.in);
System.out.println("Enter number");
while(true){
int number = input.nextInt();
if(number == 0){
break;
}
if(number > max){
max = number;
count = 1;
}else if(number == max){
count++;
}
}
if(count == 0){
System.out.println("None");
}else{
System.out.println("Max = "+max+",Count = "+count);
}
}
}