Java 练习题01(运算符)

1. 从键盘输入一个四位数,将该数字反转,与原数相加后输出。


import java.util.Scanner;
public class Test4_05 {
public static void main(String[] args) {    
    Scanner scanner=new Scanner(System.in);
    System.out.println("请输入一个四位数:");
    int num=scanner.nextInt();
    
    int num1=num/1000%10; //千位上的数
    int num2=num/100%10;  //百位上的数
    int num3=num/10%10;  //十位上的数
    int num4=num%10;    //个位上的数
    int a=num4*1000+num3*100+num2*10+num1;
    System.out.println(a+num);
    
}
}

2.  托运行李计算费用:

  实验要求:

(1)货车在计算托运行李费用时以kg为单位计算费用(12元/kg),忽略重量中的小数部分,即忽略不足1kg的部分。

(2)汽车在计算托运行李费用时以kg为单位计算费用(22元/kg),将重量中的小数部分进行四舍五入,即不足1kg的部分进行四舍五入。(3)飞机在计算托运行李费用时以g为单位计算费用(0.062元/g)(62元/kg),将重量中的小数部分,即不足1g的部分进行四舍五入。

用double型变量weight存放行李重量,用charge存放托运行李费用,程序让用户从键盘输入weight的值,该值是行李的重量(kg)为单位,然后程序将分别计算出用货车、汽车和飞机托运行李的费用。

提示:为了实现四舍五入,只需将浮点数加上0.5,再将结果进行int型转换即可。类型转换运算符的级别是2级,因此,(int)15.9+0.5的结果是15.5,即((int)15.9)+0.5,而(int)(15.9+0.5)的结果才是16。


import java.util.Scanner
public class Test4_06 {
public static void main(String[] args) {  
    Scanner sc=new Scanner(System.in);
    System.out.println("请输入weight:");  //(kg)为单位
    double weight = sc.nextDouble();

   int truck=(int)(weight*12);
   float bus=(int )(weight+0.5)*22;//四舍五入
   double fly=(int)(weight*1000+0.5)*0.062;//四舍五入
   System.out.println("货车的费用是:"+truck);
   System.out.println("汽车的费用是:"+bus);
   System.out.println("费用的费用是:"+fly);
}
}     

3.  根据给定的字符(char)变量,输出是相应信息[大写字母、小写字母、数字、特殊字符];

提示:如何从键盘获取一个char字符

Scanner sc=new Scanner(System.in);

char st=(char)sc.next().charAt(0); 


import java.util.Scanner;
public class Test4_07 {
public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
    System.out.println("请输入一个字符:");
    char c=(char)sc.next().charAt(0);
    if(c>='A'&&c<='Z') {
        System.out.println("该字符是大写字母");
} else if(c>='a'&&c<='z') {
        System.out.println("该字符是小写字母");
}else if(c>=0&&c<=9) {
        System.out.println("该字符是数字");
}else {
        System.out.println("该字符是特特殊字符");
}
}
}

4.  某电信公司的市内通话费计算标准如下:

      三分钟内0.2元,三分钟后每增加一分钟增加0.1元,不足一分钟的按一分钟计算。

      要求编写程序,给定一个通话时间(单位:秒),计算出应收费金额。


import java.util.Scanner;
public class Test4_08 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("请输入一个通话时间(秒):"); int second=sc.nextInt(); int min=(second+59)/60; //不足一分钟的按一分钟计算 double price=0; if(min<=3) { price = 0.2; }else if(min>3) { price = 0.2; price+=(min-3)*0.1; } System.out.println("市内通话费总金额:"+price); } }

5. 接收命令行参数年、月、日,判断这一天是当年的第几天

   注:判断一年是否是闰年的标准:

       1)可以被4整除,但不可被100整除

       2)可以被400整除      

package zuoye;
import java.util.Scanner;
public class Test4_10 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("请输入年:"); int year=sc.nextInt(); System.out.println("请输入月:"); int month=sc.nextInt(); System.out.println("请输入日:"); int day=sc.nextInt();
int total=0;
//2020.5.2 年份用于判断该年2月是否是闰年 ,月份用于累加该月前的天数, 日期用于累加天数
switch(month-1) {
//case 12:
//    total+=31;
case 11:
    total+=30;
case 10:
    total+=31;
case 9:
    total+=30;
case 8:
    total+=31;
case 7:
    total+=31;
case 6:
    total+=30;
case 5:
    total+=31;
case 4:
    total+=30;
case 3:
    total+=31;
case 2:
    if(year%4==0&&year%100!=0||year%400==0) {
        total+=29;
    }else {
        total+=28;
    }
case 1:
    total+=31;    
}
total+=day;  //因为这里加上了天数,所以上面的月份需要-1
System.out.println("该日期是当年第"+total+"");
}
}

 

posted @ 2020-05-01 22:35  娜梓  阅读(2187)  评论(0编辑  收藏  举报