- 编写一个程序,求1+2+3+4+5…+99+100的和。
public class Result{public static void main(String[] args){int sum =0;for(int i=1; i<= 100;i++){ sum = sum +i;} System.out.println(sum);}}
- 一个程序员为了完成公司任务,连续加班89个小时,编程计算该程序员工作了多少天零多少个小时?
class Time{public static void main(String[] args){int time = 89;int Day = time/24;int Hour = time % 24; System.out.println("程序员共工作了"+ Day+"天" +", "+Hour+"个小时");}}
- 假设今天是周四,100天后是周几?
public class Week{public static void main(String[] agrs){//先计算100天与一周7天取余剩余的周期天数int week = 100 % 7;int result = (4+week )%7; System.out.println(result);}}
- 求三个整数xyz中的最大值,使用三元运算符(此处自定义x,y ,z 的值即可,无需使用键盘输入的方式)
class Max_result{public static void main(String[] args){int x = 3;int y = 15;int z = 5;int max = x >= y ? x : y; max = max >= z ? max : z ; System.out.println("xyz中的最大值是"+max);}}
备注:
Java基本数据类型的运算符:
(1)算术运算符
(2)赋值运算符
(3)比较运算符
(4)逻辑运算符
(5)条件运算符
(6)位运算符