答案将会陆续更新,敬请期待!!!
Home01:
import java.util.Scanner;
/**
程序要求:读入圆柱体的半径和高,计算圆柱体的体积。
数据:圆柱体半径,高,面积,以及圆柱体的体积。
步骤:
1.读入圆柱体的半径和高;
2.根据公式计算圆柱体的面积;
3.根据公式计算圆柱体的体积;
4.打印出圆柱体的面积和体积。
*/
class Home01{
public static void main(String []args){
Scanner scanner = new Scanner(System.in);//使用Scanner需要引入Scanner包
System.out.print("请输入圆柱体的半径和高:");
double r =scanner.nextDouble();
double h =scanner.nextDouble();
double P =3.14;
double area = r*r*P;
double volume =area*h;
System.out.println("圆柱体面积为:"+area);
System.out.println("圆柱体体积为:"+volume);
}
}
Home02:
import java.util.Scanner;
class Home02{
public static void main(String[] args) {
Scanner input =new Scanner(System.in);
System.out.print("请输入一个在0-1000之间的数:");
int n=input.nextInt();
int sum=0;
while(n!=0){
sum+=n%10;
n/=10;}
System.out.print(n+"的各个数字之和是"+sum);
}
}
Home03:
import java.util.Scanner;
/**
程序要求:将给定的钱分成较小的货币单位。
数据:总钱数,dollar,quarter,dime,nickel,penny。
步骤:
1.读入一个double型的数据代表总钱数;
2.将钱数*100,由美元转换为分币数;
3.将分币数除以100得到美元dollar的个数,除以100取余数得到剩余分币的个数;
4.将剩余分币除以25得到quarter的个数,除以25取余数得到剩余分币的个数;
5.将分币数除以10得到一角dime的个数,除以10取余数得到剩余分币的个数;
6.将分币数除以5得到五分nickel的个数,除以5取余数得到剩余分币的个数;剩余分币数即为1分币为单位的数目;
7.显示结果。
*/
class Home03{
public static void main(String []args){
Scanner scanner = new Scanner(System.in);
System.out.print("请输入需要转换为小货币单位的总钱数:");
double sum = scanner.nextDouble();
double penny = (double)sum*100;//double型和int型不兼容,应将类型强制转换为double才能精确所有的penny。
int dollar = (int)penny/100; //除以100强制转换为int型显示整数dollar的数量,下述同理。
penny = penny%100;
int quarter = (int)penny/25;
penny = penny%25;
int dime =(int)penny/10;
penny =penny%10;
int nickel =(int)penny/5;
penny=penny%5;
System.out.println("美元的数目为"+dollar);
System.out.println("贰角伍分的数目为"+quarter);
System.out.println("一角的数目为"+dime);
System.out.println("五分的数目为"+nickel);
System.out.println("1分币的数目为"+penny);
}
}
Home04:
import java.util.Scanner;
/**
程序要求:根据三角形三个顶点的坐标,输出三角形的面积。
数据:顶点坐标(x1,y1),(x2,y2),(x3,y3),三角形三边的长度,海伦公式的半周长s,以及三角形的面积。
步骤:
1.输入三角形三个顶点的坐标;
2.计算三角形三条边的长度;
3.根据海伦公式求出半周长s;
4.根据公式求出三角形的面积;
5:打印结果。
*/
class Home4{
public static void main(String []args){
Scanner scanner = new Scanner(System.in);
System.out.print("请输入三个顶点的横纵坐标:");
double x1 = scanner.nextDouble();
double y1 = scanner.nextDouble();
double x2 = scanner.nextDouble();
double y2 = scanner.nextDouble();
double x3 = scanner.nextDouble();
double y3 = scanner.nextDouble();
double a =Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));//也可用Math.pow((Math.pow(x1-x2,2)+Math.pow(y1-y2,2)),0.5);
double b =Math.sqrt((x2-x3)*(x2-x3)+(y2-y3)*(y2-y3));
double c =Math.sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3));
double s =(a+b+c)/2;
double area =Math.sqrt(s*(s-a)*(s-b)*(s-c));
System.out.println("这个三角形的面积为:"+area);
}
}
Home05:
import java.util.Scanner;
class Home05{
public static void main(String[] args) {
Scanner input =new Scanner(System.in);
System.out.print("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 delt=a*d-b*c;
if(delt==0){
System.out.print("方程无解");
}else{
System.out.print("x1 is"+(e*d-b*f)/delt+",x2 is"+(a*f-e*c)/delt);
}
}
}
Home06:
import java.util.Random;
import java.util.Scanner;
class Home06{
public static void main(String[] args) {
Random random =new Random();
int caipiao=random.nextInt(900)+100;
Scanner input=new Scanner(System.in);
System.out.print("请输入你猜测的彩票数:");
System.out.println(caipiao);
int your=input.nextInt();
int caipiao1=((caipiao/100)%10);
int caipiao2=((caipiao/10)%10);
int caipiao3=caipiao%10;
int your1=((your/100)%10);
int your2=((your/10)%10);
int your3=your%10;
int temp=0;
if(caipiao1<caipiao2){
temp=caipiao1;
caipiao1=caipiao2;
caipiao2=temp;
}
if(caipiao1<caipiao3){
temp=caipiao1;
caipiao1=caipiao3;
caipiao3=temp;
}
if(caipiao2<caipiao3){
temp=caipiao2;
caipiao2=caipiao3;
caipiao3=temp;
}
if(your1<your2){
temp=your1;
your1=your2;
your2=temp;
}
if(your1<your3){
temp=your1;
your1=your3;
your3=temp;
}
if(your2<your3){
temp=your2;
your2=your3;
your3=temp;
}
if(caipiao==your){
System.out.print("恭喜获得一等奖,奖金10000元。");
}
else if(caipiao1==your1&&caipiao2==your2&&caipiao3==your3){
System.out.print("获得二等奖,奖金3000元");
}
else if(caipiao1!=your1&&caipiao2!=your2&&caipiao3!=your3){
System.out.print("未获奖");
}else{
System.out.print("获得三等奖,奖金1000元。");
}
}
}
Home07:
import java.util.Scanner;
class Home07{
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.print("请输入三角形的三边:");
double a=input.nextDouble();
double b=input.nextDouble();
double c=input.nextDouble();
if(istrue(a,b,c)){
System.out.print("三角形的周长是:"+(a+b+c));
}else{
System.out.print("输入不合法!");
}
}
public static boolean istrue(double a,double b,double c){
double s=Math.max(a, b);
s=Math.max(s, c);
if(s==c){
if(c>a+b){
return true;
}else{
return false;
}
}else if(s==b){
if(b>a+c){
return true;
}else{
return false;
}
}else{
if(a>b+c){
return true;
}else{
return false;
}
}
}
}
Home08:
import java.util.Scanner;
class Home08{
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.print("请输入年:");
int year=input.nextInt();
System.out.print("请输入月份:");
int month=input.nextInt();
System.out.print("请输入是该月第几天:");
int days=input.nextInt();
int h=((days+(26*(month+1))/10+year%100+(year%100)/4+year/400+year/20))%7;
String[] s={"周6","周R","周1","周2","周3","周4","周5"};
System.out.print("该天是"+s[h]);
}
}
Home09:
import java.util.Scanner;
class Home09{
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.print("请输入需要判断的点坐标x和y:");
double x=input.nextDouble();
double y=input.nextDouble();
if(x<=5&&x>=-5&&y<=2.5&&y>=-2.5){
System.out.print("该点("+x+","+y+")在矩形内");
}else{
System.out.print("该点("+x+","+y+")在矩形外");
}
}
}
Home10:
import java.util.Scanner;
class Home10{
public static void main(String[] args) {
Scanner input =new Scanner(System.in);
System.out.print("请输入big矩形的参数x,y,w,h:");
double x1=input.nextDouble();
double y1=input.nextDouble();
double w1=input.nextDouble();
double h1=input.nextDouble();
System.out.print("请输入small矩形的参数x,y,w,h:");
double x2=input.nextDouble();
double y2=input.nextDouble();
double w2=input.nextDouble();
double h2=input.nextDouble();
if(Math.abs(x1-x2)<=(w1-w2/2)&&Math.abs(y1-y2)<=(h1-h2/2)){
System.out.print("r1包含r2");
}else if(Math.abs(x1-x2)>=(w1+w2/2)&&Math.abs(y1-y2)>=(h1+h2/2)){
System.out.print("r1和r2相离");
}else{
System.out.print("r1和r2相切");
}
}
}
Home11:
import java.util.Scanner;
class Home11{
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.print("请输入第一个圆的圆心坐标x,y和r:");
double x1=input.nextDouble();
double y1=input.nextDouble();
double r1=input.nextDouble();
System.out.print("请输入第二个圆的圆心坐标x,y和r:");
double x2=input.nextDouble();
double y2=input.nextDouble();
double r2=input.nextDouble();
double distance=Math.pow(Math.pow(x1-x2,2)+Math.pow(y1-y2, 2),0.5 );
if(distance>r1+r2){
System.out.print("两圆相离");
}else if(distance<Math.abs(r1-r2)){
if(r1>r2){
System.out.print("r1包含r2");
}else{
System.out.print("r2包含r1");
}
}else{
System.out.print("两圆重叠");
}
}
}
Home12:
import java.util.Scanner;
class Home12{
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.print("请输入需要转换为16进制的10进制数:");
int number=input.nextInt();
String s="";
int t=0;
while(true){
t=number%16;
if(t>=10){
s=(char)(t+55)+s;
}else{
s=t+s;
}
number/=16;
if(number==0){
break;
}
}
System.out.print("0X"+s);
}
}
Home13:
import java.util.Scanner;
class Home13{
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.print("请输入一组数字,已0作为结束:");
int number=1;
int sum=0;
int zheng=0;
int fu=0;
double count=1;
while((number=input.nextInt())!=0){
count++;
if(number>0){
zheng++;
sum+=number;
}else{
fu++;
sum+=number;
}
}
System.out.println("正数数目:"+zheng);
System.out.println("负数数目:"+fu);
System.out.println("总数目:"+count);
System.out.println("平均值为:"+(sum/(count-1)));
}
}
Home14:
class Home14{
public static void main(String[] args) {
int i=1;
while(i*i<12000){
i--;
}
System.out.println("满足n*n>12000的最小整数:"+i);
i=1;
while(i*i*i<12000){
i++;
}
System.out.println("满足n*n*n<12000的最小整数:"+i);
}
}
Home15:
class Home15{
public static void main(String[] args) {
System.out.println("图案1:");
for(int i=1;i<=6;i++){
for(int j=1;j<=i;j++){
System.out.print(j+" ");
}
System.out.println();
}
System.out.println("图案2:");
for(int i=1;i<=6;i++){
for(int j=1;j<=6-i+1;j++){
System.out.print(j+" ");
}
System.out.println();
}
System.out.println("图案3:");
for(int i=1;i<=6;i++){
for(int j=1;j<=6-i;j++){
System.out.print(" ");
}
for(int k=i;k>0;k--){
System.out.print(k+" ");
}
System.out.println();
}
System.out.println("图案4:");
for(int i=1;i<=6;i++){
for(int j=1;j<i;j++){
System.out.print(" ");
}
for(int k=1;k<=6-i+1;k++){
System.out.print(k+" ");
}
System.out.println();
}
}
}
Home16:
class Home16{
public static void main(String[] args) {
for(int i=1;i<=8;i++){
for(int j=1;j<=8-i+1;j++){
System.out.print(" ");
}
for(int k=1;k<=i;k++){
System.out.printf("%4d",(int)Math.pow(2,k-1));
}
for(int h=i-1;h>=1;h--){
System.out.printf("%4d",(int)Math.pow(2,h-1));
}
System.out.println();
}
}
}
Home17:
class Home17{
public static void main(String[] args) {
int count=0;
for(int i=101;i<=2000;i++){
if(i%4==0){
if(count%10==0){
System.out.println();
}
if(i%100==0&&i%400==0){
System.out.printf("%5d",i);
count++;
continue;
}else if(i%100==0){
continue;
}else{
System.out.printf("%5d",i);
count++;
}
}
}
}
}
Home18:
import java.util.Scanner;
class Home18{
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.print("请输入一串数字,0作为结束:");
int number=1;
int count=1;
int max=input.nextInt();
while((number=input.nextInt())!=0){
if(number==max){
count++;
}else if(number>max){
max=number;
count=1;
}else{
}
}
System.out.println("最大数为:"+max);
System.out.println("出现次数为:"+count);
}
}
Home19:
import java.util.Scanner;
class Home19{
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.print("请输入第一个字符串:");
String s1=input.nextLine();
System.out.print("请输入第二个字符串:");
String s2=input.nextLine();
int length=Math.min(s1.length(), s2.length());
int i=0;
for(i=0;i<length;i++){
if(s1.charAt(i)!=s2.charAt(i))
break;
}
String s=s1.substring(0,i);
System.out.print("共同前缀:"+s);
}
}