JAVA 编程练习3

题目:有1、2、3、4四个数字,能组成多少个互不相同且一个数字中无重复数字的三位数?并把他们都输入。  

public class lianxi {
	public static void main(String[] args) {
            int [] a={1,2,3,4};
            int count=0;
            for(int x=0;x<a.length;x++){
            	for(int y =0;y<a.length;y++){
            		for(int z=0;z<a.length;z++){
            			if(a[x]!=a[y]&&a[x]!=a[z]&&a[y]!=a[z]){
						int sum=a[x]*100+a[y]*10+a[z];
						count++;
						System.out.println(sum);
            			}
            		}
            	}
            }   
            System.out.println("一共有"+count+"三位数");
	}

}

需要三位数 那就三个循环 一个个寻找 如果每个数都不相同就输出了它 并且累加1 


题目:企业发放的奖金根据利润提成。利润(I)低于或等于10万元时,奖金可提10%;利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可可提成7.5%;20万到40万之间时,高于20万元的部分,可提成5%;40万到60万之间时高于40万元的部分,可提成3%;60万到100万之间时,高于60万元的部分,可提成1.5%,高于100万元时,超过100万元的部分按1%提成,从键盘输入当月利润,求应发放奖金总数?  

import java.util.*;
public class lianxi {
	public static void main(String[] args) {
		Scanner input= new Scanner(System.in);
		System.out.println("请输入你获得的奖金:(万)");
		double lirun =input.nextDouble();
		if(lirun<=10)
			System.out.println(lirun*0.1+"万");
		else if(lirun>10&&lirun<=20)
			System.out.println(10*0.1+(lirun-10)*0.075+"万");
		else if(lirun>20&&lirun<=40)
			System.out.println(10*0.1+10*0.075+(lirun-20)*0.05+"万");
		else if(lirun>40&&lirun<=60)
			System.out.println(10*0.1+10*0.075+20*0.05+(lirun-40)*0.03+"万");
		else if(lirun>60&&lirun<=100)
			System.out.println(20*0.175+20*0.05+20*0.03+(lirun-60)*0.001+"万");
		else if(lirun>100) 
			System.out.println(20*0.175+40*0.08+40*0.015+(lirun-100)*0.01+"万");     

	}
	} 

别忘记把之前得到的利润加进去


题目:一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?   

public class lianxi {
	public static void main(String[] args) {
		for(int i=21;;i++){
			if(Math.sqrt(i+100)%1==0)
				if(Math.sqrt(i+168)%1==0)
				   System.out.println(i+"加上100后是一个完全平方数,再加上168又是一个完全平方数");
		}   
	}
	} 

我循环是从21开始的  因为 100是10的平方 因为11的平方等于121 21=121-100 所以最先应该从21开始寻找 

先寻找这个数加上100之后开根号是否为整数 如果是整数进行下一个判断 这个数如果加上168之后再开根号还是整数 那么这个数就是答案


题目:输入某年某月某日,判断这一天是这一年的第几天?  

import java.util.*;
public class lianxi {
public static void main(String[] args) {
     int year, month, day;
     int days = 0;
     int d = 0;
     int e;
     Scanner input =new Scanner(System.in);
     do {
     e = 0;
     System.out.print("输入年 月 日:");
     year =input.nextInt();
     month = input.nextInt();
     day = input.nextInt();
     if (year < 0 || month < 0 || month > 12 || day < 0 || day > 31) {
     System.out.println("输入错误,请重新输入!");
     e=1 ; 
     }
     }while( e==1);


      for (int i=1; i <month; i++) {
      switch (i) {
      case 1:  case 3:   case 5:   case 7:  case 8:   case 10:   case 12:
       days = 31;  break;
      case 4:  case 6:  case 9:  case 11:
       days = 30;   break;
      case 2:
       if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) {
        days = 29;
       } else {
        days = 28;
       }
       break;
      }
      d += days;
      }
     System.out.println(year + "-" + month + "-" + day + "是这年的第" + (d+day) + "天。");
}
}

用for循环控制switch 语句 比如找的是4 月 那么需要把4月之前的天份 加 4月日期的天粉 那么就可以得到 答案

记住闰年的 公式  

if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))


题目:输入三个整数x,y,z,请把这三个数由小到大输出

import java.util.*;
public class lianxi {
public static void main(String[] args) {
	int[] a=new int[3]; 
    Scanner input =new Scanner(System.in);
    System.out.println("请输入3个整数:");
    for(int i=0;i<3;i++)
    	a[i]=input.nextInt();
    Arrays.sort(a);
    for(int i=0;i<3;i++)
    	System.out.print(a[i]+" ");
}
}

定义一个长度为3的一维数组  然后使用 JAVA内部函数 

Arrays.sort(a); 进行排序即可 



猜你喜欢

转载自blog.csdn.net/qq_41398448/article/details/80735152
今日推荐