Java基础程序题Day3.4

1.解一元二次方程
在这里插入图片描述

import java.util.Scanner;
class Demo3_1{
 public static void main(String[] args){
 /*
 数据:a b c x x1 x2 delt
 指令:
 1.输入a b c
 2.判断a==0
  2.1判断b==0
   2.1.1判断c==0 输出任意解
   2.1.2 输出 不是一元二次方程组,且等式不成立
  2.2计算 x=-c/b
 3.计算delt
  3.1判断delt>0 输出x1 x2
  3.2判断delt==0 输出x
  3.3判断delt<0 输出无实数解
 4.程序结束
 */
 Scanner scanner=new Scanner(System.in);
 System.out.print("Enter a b c:");
 double a=scanner.nextDouble();
 double b=scanner.nextDouble();
 double c=scanner.nextDouble();
 if (a==0){
  if(b==0){
   if(c==0){
    System.out.println("此方程不是一元二次方程,但是等式成立,解为任意解。");
    }else{
    System.out.println("此方程不是一元二次方程,且等式不成立,无解。");
    }
  }else{
  double x=(-c)/b;
  System.out.println("此方程不是一元二次方程,但是等式成立,解为"+x);
  }
 }else{
  double delt=b*b-4*a*c;
  if(delt>0){
   double x1=(-b+Math.pow(delt,0.5))/(2*a);
   double x2=(-b-Math.pow(delt,0.5))/(2*a);
   System.out.println("The equation has two roots "+x1+" and "+x2);
  }else if(delt==0){
   double x=(-b+Math.pow(delt,0.5))/(2*a);
   System.out.println("The equation has one root "+x);
  }else{
   System.out.println("The equation has no real roots");
  }
 }
 }
}

2.求解2*2线性方程组
在这里插入图片描述

import java.util.Scanner;
class Demo3_2{
 public static void main(String[] args){
  /*
  数据:a b c d e f delt x y
  指令:
  1.输入a b c d e f 
  2.计算delt
  3.判断delt==0
   3.1输出方程式无解
   3.2计算x y 并输出
  4.程序结束
  */
  Scanner scanner=new Scanner(System.in);
  System.out.print("Enter a b c d e f:");
  double a=scanner.nextDouble();
  double b=scanner.nextDouble();
  double c=scanner.nextDouble();
  double d=scanner.nextDouble();
  double e=scanner.nextDouble();
  double f=scanner.nextDouble();
  double delt=(a*d-b*c);
  if(delt==0){
   System.out.println("The equation has no solution");
  }else{
   double x=(e*d-b*f)/delt;
   double y=(a*f-e*c)/delt;
   System.out.println("x is "+x+" and y is "+y);
  }
 }
}

3.找到将来的日期
在这里插入图片描述

import java.util.Scanner;
class Demo3_3{
 public static void main(String[] args){
  /*
  数据:今天周几,未来几天后,这天星期几
  指令:
  1.输入今天周几(0 1 2 3 4 5 6 )
  2.输入几天后
  3.判断几天后是周几
  4.输出
  */
  Scanner scanner=new Scanner(System.in);
  System.out.print("Enter today's day:");
  int today=scanner.nextInt();
  System.out.print("Enter the number of days elapsed since today:");
  int future=scanner.nextInt();
  int week=(today+future)%7;
  String todayStr="";
  String weekStr="";
  switch(week){
   case 0:
    weekStr="Sunday";
    break;
   case 1:
    weekStr="Monday";
    break;
   case 2:
    weekStr="Tuesday";
    break;
   case 3:
    weekStr="Wednesday";
    break;
   case 4:
    weekStr="Thursday";
    break;
   case 5:
    weekStr="Friday";
    break;
   case 6:
    weekStr="Saturday";
    break; 
  }
  switch(today){
   case 0:
    todayStr="Sunday";
    break;
   case 1:
    todayStr="Monday";
    break;
   case 2:
    todayStr="Tuesday";
    break;
   case 3:
    todayStr="Wednesday";
    break;
   case 4:
    todayStr="Thursday";
    break;
   case 5:
    todayStr="Friday";
    break;
   case 6:
    todayStr="Saturday";
    break; 
  }
  System.out.println("Today is "+todayStr+" and the future day is "+weekStr);
 }
}

4.回文数字
在这里插入图片描述

import java.util.Scanner;
class Demo3_4{
 public static void main(String[] args){
  /*
  数据:输入一个四位数整数
  指令:
  1.输入一个四位数整数
  2.拆分整数,a b c d
  3.重组整数a*1000+b*100+c*10+d
  4.判断两个数是否相等,并输出
  5.程序结束
  */
  Scanner scanner=new Scanner(System.in);
  System.out.print("Enter the num:");
  int num=scanner.nextInt();
  int oriNum=num;
  int a=num%10;
  num/=10;
  int b=num%10;
  num/=10;
  int c=num%10;
  num/=10;
  int d=num;
  int currentNum=a*1000+b*100+c*10+d;
  if(currentNum==oriNum){
   System.out.printf("%d is a palindrome.",oriNum);
  }else{
   System.out.printf("%d is not a palindrome.",oriNum);
  }
 }
}

5.彩票
在这里插入图片描述

import java.util.Scanner;
class Demo3_5{
 public static void main(String[] args){
 /*
 数据:获取一个三位数的随机数 输入的三位数 
 指令:
 1.获取一个随机数
 2.随机数×1000
 3.强制转换
 4.输入一个三位数
 5.取出随机数的各位数a1 b1和两位数的各位数a2 b2
 6.判断各位数字
  6.1判断a1==a2&&b1==b2&&c1==c2
  6.2判断(a1==a2&&b1==c2&&b2==c1)||(a1==b2&&b1==a2&&c1==c2)||(a1==b2&&b1==c2&&c1==a2)||(a1==c2&b1==a2&&c1==b2)||(a1==c2&&b1==b2&&c1==a2)
  6.3判断a1==a2||a1==b2||a1==c2||b1==a2||b1==b2||b1==c2||c1==a2||c1==b2||c1==c2
 7.程序结束
 */
 int num1=(int)(Math.random()*1000);
 System.out.println(num1);
 int a1=num1%10;
 num1/=10;
 int b1=num1%10;
 num1/=10;
 int c1=num1;
 Scanner scanner=new Scanner(System.in);
 System.out.print("Enter the num2:");
 int num2=scanner.nextInt();
 int a2=num2%10;
 num2/=10;
 int b2=num2%10;
 num2/=10;
 int c2=num2;
 if(a1==a2&&b1==b2&&c1==c2){
  System.out.println("恭喜您中了10000美金!");
 }else if((a1==a2&&b1==c2&&c1==b2)||(a1==b2&&b1==a2&c1==c2)||(a1==b2&&b1==c2&&c1==a2)||(a1==c2&&b1==a2&&c1==b2)||(a1==c2&&b1==b2&&c1==a2)){
  System.out.println("恭喜您中了3000美金!");
 }else if(a1==a2||a1==b2||a1==c2||b1==a2||b1==b2||b1==c2||c1==a2||c1==b2||c1==c2){
     System.out.println("恭喜您中了1000美金!");
 }else{
  System.out.println("谢谢惠顾!"); 
 }   
 }
}

6.游戏(剪刀、石头、布)
在这里插入图片描述

import java.util.Scanner;
class Demo3_6{
 public static void main(String[] args){
  /*
  数据:随机数(0 1 2)用户输入一个数(0 1 2)
  指令:
  1.计算机获取随机数
  2.用户输入一个数
  3.判断用户和计算机的值,给相对应的字符串赋值
  4.判断com==usr
   4.1输出平局
  5.判断(com+1)%3==usr 
   5.1usr赢
   5.2com赢
  6.程序结束
  */
  Scanner scanner=new Scanner(System.in);
  int com=(int)(Math.random()*3);
  System.out.print("scissor(0),rock(1),paper(2):");
  int usr=scanner.nextInt();
  String comSRP="";
  String usrSRP="";
  if(com==0){
   comSRP="scissor";
  }else if(com==1){
   comSRP="rock";
  }else{
   comSRP="paper";
  }
  if(usr==0){
   usrSRP="scissor";
  }else if(usr==1){
   usrSRP="rock";
  }else{
   usrSRP="paper";
  }
  if(com==usr){
   System.out.printf("The computer is %s.You are %s too.It is a draw.",comSRP,usrSRP);
  }else if((com+1)%3==usr){
   System.out.printf("The computer is %s.You are %s.You won.",comSRP,usrSRP);
  }else{
   System.out.printf("The computer is %s.You are %s.You lose.",comSRP,usrSRP);
  }
 }
}

7.几何(点是否在圆内)
在这里插入图片描述

import java.util.Scanner;
class Demo3_7{
 public static void main(String[] args){
  /*
  数据:圆心坐标,半径,另一个点的坐标,两点间距离
  指令:
  1.输入圆心坐标及半径
  2.输入另一个点的坐标
  3.计算两点间距离
  4.判断distance>radius
  */
  Scanner scanner=new Scanner(System.in);
  System.out.print("输入圆心坐标及半径:");
  double ox=scanner.nextDouble();
  double oy=scanner.nextDouble();
  double radius=scanner.nextDouble();
  System.out.print("Enter a point with two coordinates:");
  double x=scanner.nextDouble();
  double y=scanner.nextDouble();
  double distance=Math.sqrt(Math.pow(x-ox,2)+Math.pow(y-oy,2));
  if(distance>radius){
   System.out.printf("Point (%.1f,%.1f) is not in the circle",x,y);
  }else{
   System.out.printf("Point (%.1f,%.1f) is in the circle",x,y);
  }
 }
}

8.点是否在矩形内
在这里插入图片描述

import java.util.Scanner;
class Demo3_8{
 public static void main(String[] args){
  /*
  数据:矩形重心坐标,另一个点的坐标,长,高
  指令:
  1.输入矩形重心坐标
  2.输入矩形的长length,高hight
  3.输入另一个点的坐标
  4.计算x-ox,y-oy
  5.判断(x-ox)>(length/2)||(y-oy)>(hight/2)
  6.判断(x-ox)<=(length/2)&&(y-oy)<=(hight/2)
  7.程序结束
  */
  Scanner scanner=new Scanner(System.in);
  System.out.print("Enter rectangle's ox and oy:");
  double ox=scanner.nextDouble();
  double oy=scanner.nextDouble();
  System.out.print("Enter rectangle's length and hight:");
  double length=scanner.nextDouble();
  double hight=scanner.nextDouble();
  System.out.print("Enter a point with two coordinates:");
  double x=scanner.nextDouble();
  double y=scanner.nextDouble();
  if((x-ox)>(length/2)||(y-oy)>(hight/2)){
   System.out.printf("Point (%.1f,%.1f) is not in the rectangle.",x,y);
  }else if ((x-ox)<=(length/2)&&(y-oy)<=(hight/2)){
   System.out.printf("Point (%.1f,%.1f) is in the rectangle.",x,y);
  }
 }
}

9.使用操作符&&、||、^
在这里插入图片描述

import java.util.Scanner;
class Demo3_9{
 public static void main(String[] args){
  /*
  数据:输入一个整数值
  指令:
  1.输入一个整数值
  2.判断能否被5和6整除
  3.判断能否被5或6整除
  4.判断能否被5或6整除,但不能同时被整除
  5.程序结束
  */
  Scanner scanner=new Scanner(System.in);
  System.out.print("Enter a number:");
  int num=scanner.nextInt();
  boolean flag1=(num%5==0)&&(num%6==0);
  boolean flag2=(num%5==0)||(num%6==0);
  boolean flag3=(num%5==0)^(num%6==0);
  System.out.println("Is "+num+" divisible by 5 and 6?"+flag1);
  System.out.println("Is "+num+" divisible by 5 or 6?"+flag2);
  System.out.println("Is "+num+" divisible by 5 or 6,but not booth?"+flag3);
 }
}

10.几何,点是否在三角形内
在这里插入图片描述

import java.util.Scanner;
class Demo3_10{
 public static void main(String[] args){
  /*
  数据:输入一个点的坐标
  指令:
  1.输入一个点的坐标(x1,y1)
  2.判断(x1>=0&&x1<=200)&&(y1>=0&&y1<=100)
  3.计算x1,y2;(y=0.5x-100)
  4.判断y1>y2;
  5.程序结束
  */
  Scanner scanner=new Scanner(System.in);
  System.out.print("Enter x1 and y1:");
  double x1=scanner.nextDouble();
  double y1=scanner.nextDouble();
  if ((x1>=0&&x1<=200)&&(y1>=0&&y1<=100)){
   double y2=(-0.5)*x1+100;
   if (y1>y2){
    System.out.println("The point is not in the triangle.");
   }else{
    System.out.println("The point is in the triangle.");
   }
  }else{
   System.out.println("The point is not in the triangle.");
  }
 }
}

11.几何,两个圆
在这里插入图片描述

import java.util.Scanner;
class Demo3_11{
 public static void main(String[] args){
  /*
  数据:两个圆的圆心坐标,两个圆的半径
  指令:
  1.输入两个圆的圆心坐标(x1,y1)(x2,y2)
  2.输入两个圆的半径r1,r2:默认r1>r2;
  3.计算两个圆心坐标之间的距离distance;
  4.判断distance>r1+r2;
  5.程序结束
  */
  Scanner scanner=new Scanner(System.in);
  System.out.print("Enter circle1's center and radius:");
  double x1=scanner.nextDouble();
  double y1=scanner.nextDouble();
  double r1=scanner.nextDouble();
  System.out.print("Enter circle2's center and radius:");
  double x2=scanner.nextDouble();
  double y2=scanner.nextDouble();
  double r2=scanner.nextDouble();
  double distance=Math.sqrt(Math.pow(x2-x1,2)+Math.pow(y2-y1,2));
  if (distance>(r1+r2)){
   System.out.println("circle2 is out of circle1");
  }else if (distance<=(r1-r2)){
   System.out.println("circle2 is inside circle1");
  }else{
   System.out.println("circle2 overlap circle1");
  }
 }
}

12.统计正负数个数求取平均值
在这里插入图片描述

import java.util.Scanner;
class Demo3_12{
 public static void main(String[] args){
  /*
  数据:正数,负数,正负数的个数,总和,平均值
  指令:
  1.循环输入整数;
  2.判断正负数
  3.计算正负数的个数
  4.求和
  5.计算平均值
  */
  Scanner scanner=new Scanner(System.in);
  double sum=0;
  int positiveCount=0;
  int negativeCount=0;
  System.out.print("Enter an inteage,the input ends if it is:");
  while(true){
   int num=scanner.nextInt();
   if(num>0){
    positiveCount++;
   }else if(num<0){
    negativeCount++;
   }else{
    break;
   }
   sum+=num;
  }
  double average=sum/(positiveCount+negativeCount);
  System.out.println("The number of positiveCount is "+positiveCount);
  System.out.println("The number of negativeCount is "+negativeCount);
  System.out.println("The total is "+sum);
  System.out.println("The average is "+average);
 }
}

13.找出能被5或6整除,但不能被两者同时整除
在这里插入图片描述

class Demo3_13{
 public static void main(String[] args){
  /*
  指令:
  1.遍历100~200的整数
  2.判断是否满足需求
  3.每十个换行
  4.输出
  5.程序结束
  */
  int j=0;
  for(int i=100;i<=200;i++){
   if(i%5==0^i%6==0){
    System.out.print(i+" ");
    j++;
    if(j%10==0){
     System.out.println();   
    }
   }
  }
 }
}

14.
在这里插入图片描述

class Demo3_14{
 public static void main(String[] args){
  /*
  需求:遍历一个平方大于12000的最小正整数
  */
  int n=1;
  while(true){
   if(n*n>12000){
    System.out.println("n="+n);
    break;
   }
   n++;
  }
  n=1;
  while(true){
   if(n*n*n>12000){
    System.out.println("n="+(n-1));
    break;
   }
   n++;
  }
 }
}

15.显示金字塔
在这里插入图片描述

class Demo3_15{
 public static void main(String[] args){
  /*
      1 
     2 1 2         
    3 2 1 2 3
   4 3 2 1 2 3 4 
   4  -3 -2 -1
  */
  for(int i=1;i<=7;i++){
   for(int k=1;k<=7-i;k++){
    System.out.print(" ");
   }
   for(int j=-(i-1);j<i;j++){
    System.out.print(Math.abs(j)+1);
   }
    System.out.println();
  }
 }
}

16.使用循环语句打印4个图案
在这里插入图片描述

class Demo3_16{
 public static void main(String[] args){
  /*
  1
  12
  123
  1234
  12345
  123456 
  */
  for(int i=1;i<=7;i++){
   for(int j=1;j<=i;j++){
    System.out.print(j);
   }
    System.out.println();
  }
  /*
  123456
  12345
  1234
  123
  12
  1
  */
  for(int i=1;i<=7;i++){
   for(int j=1;j<=8-i;j++){
    System.out.print(j);
   }
    System.out.println();
  }
  /*
       1
      12
     123
    1234
   12345 
  */
  for(int i=1;i<=7;i++){
   for(int k=1;k<=7-i;k++){
    System.out.print(" ");
   }
   for(int j=1;j<=i;j++){
    System.out.print(j);
   }
   System.out.println();
  }
  /*
  123456
   12345
    1234
     123
      12
    1
  */
  for(int i=1;i<=7;i++){
   for(int k=1;k<=i-1;k++){
    System.out.print(" ");
   }
   for(int j=1;j<=8-i;j++){
    System.out.print(j);
   }
   System.out.println();
  }
 }
}

17.打印金字塔形的数据
在这里插入图片描述

class JinZiTa{
 public static void main(String[] args){
  /*
      1
    1 2 1
  1 2 4 2 1
1 2 4 8 4 2 1
 规律:
 i    j
 1    2^0 1 0
 2    2 0 1 2 1 0
  */
  for(int i=1;i<=6;i++){
   for(int k=1;k<=7-i;k++){
    System.out.print("   ");
   }
   for(int j=-(i-1);j<=i-1;j++){
    System.out.printf("%-3d",(int)(Math.pow(2,i-1-Math.abs(j))));
   }
   System.out.println();
  }
 }
}

18.打印2~1000之间的素数
在这里插入图片描述

class Demo3_18{
 public static void main(String[] args){
  /*
  1.遍历2~1000的整数
  2.判断是否是素数
  3.每八个一行输出
  4.程序结束
  */
  int count=0;
  boolean flag=true;
  for(int i=2;i<=1000;i++){
   for(int j=2;j<=i/2;j++){
    if(i%j==0){
     flag=false;
     break;
    }
   }
   if(flag){
    System.out.print(i+" ");
    count++;
    if(count%8==0){
     System.out.println();
    }
   }
   flag=true;
  }
 }
}

19.显示闰年
在这里插入图片描述

class Demo3_19{
 public static void main(String[] args){
  /*
  1.遍历101~2100之间的所有闰年
  2.判断是否是闰年
  3.每行十个输出
  */
  int count=0;
  for(int year=101;year<=2100;year++){
   if(year%4==0||year%100==0&&year%400==0){
    System.out.print(year+" ");
    count++;
    if(count%10==0){
     System.out.println();
    }
   }
  }
 }
}

20.完全数
在这里插入图片描述

import java.util.Scanner;
class Demo20{
 public static void main(String[] args){
  /*
  数据:输入一个0~1000之间的整数,把它们各位相加
  指令:
  1.输入一个0~1000之间的整数number
  2.取出个位数
  3.取出十位数
  4.取出百位数
  5.输出
  */
  Scanner scanner=new Scanner(System.in);
  System.out.print("Enter a number between 0 and 1000:");
  int number=scanner.nextInt();
  int sum=0;
  sum+=number%10;
  number=number/10;
  sum+=number%10;
  number=number/10;
  sum+=number%10;
  System.out.println("The sum of the digits is "+sum);
 }
}

21.游戏:剪刀、石头、布
在这里插入图片描述

import java.util.Scanner;
class Demo21{
 public static void main(String[] args){
  /*
  数据:半径 高 底面积 体积 圆周率
  指令:
  1.输入半径和高
  2.计算底面积=半径*半径*圆周率
  3.计算体积=底面积*高
  4.输出体积
  */  
  Scanner scanner=new Scanner(System.in);
  System.out.print("Enther the radius and length of a cyclinder:");
  double radius=scanner.nextDouble();
  double length=scanner.nextDouble();
  double area=radius*radius*3.14159265;
  double volume=area*length;
  System.out.printf("The area is %.4f \n",area);
  System.out.printf("The volume is %.1f",volume);
 }
}

22.最大数的出现次数
在这里插入图片描述

import java.util.Scanner;
class Demo3_22{
 public static void main(String[] args){
  /*
  1.循环输入一组数字
  2.找出最大数,并记录他出现的次数
  3.输出
  */
  Scanner scanner=new Scanner(System.in);
  System.out.print("Enter the number:");
  int count=0;
  int max=0;
  while(true){
   int num=scanner.nextInt();
   if(max<num){
    max=num;
    count=1;
   }else if(max==num){
    count++;
   }
   if(num==0){
    break;
   }
  }
  System.out.printf("max=%d count=%d",max,count);
 }
}
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java语法基础练习包括九九乘法表、数组小代码练习、运算符练习和分支结构练习。以下是这些练习的示例代码: 九九乘法表: ``` package day05; public class MultiTable { public static void main(String[] args) { for(int num=1;num<=9;num++) { //控制行 for(int i=1;i<=num;i++) { //控制列 System.out.print(i + "*" + num + "=" + i*num + "\t"); } System.out.println(); //换行 } } } ``` 数组小代码练习: ``` int[] array1 = {1, 2, 3, 4, 5};int[] array2 = new int = 6; array2 = 7; array2 = 8; ``` 运算符练习: ``` int a = 5, b = 10, c = 5; boolean b1 = b > a; System.out.println(b1); //true System.out.println(c < b); //true System.out.println(a >= c); //true System.out.println(b <= a); //false System.out.println(b == c); //false System.out.println(a != c); //false System.out.println(a > 10); //false System.out.println(b % 2 == 0); //true System.out.println(c++ > 5); //false------c自增1变为6 System.out.println(c++ > 5); //true-------c自增1变为7 ``` 分支结构练习: ``` int num = 5; int flag = num > 0 ? 1 : -1; System.out.println(flag); //1 int a = 8, b = 5; int max = a > b ? a : b; System.out.println("max=" + max); int number = 10; if(number % 2 == 0) { System.out.println(number + "是偶数"); } else { System.out.println(number + "是奇数"); } double price = 600; if(price >= 500) { double discount = price * 0.8; System.out.println("满500打8折,优惠后价格为" + discount); } else { System.out.println("未满500,无优惠"); } ``` 希望以上代码可以帮助到您进行Java语法基础练习。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [java基础练习(含答案共39道)-语言基础篇](https://blog.csdn.net/u013488276/article/details/124110443)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值