javaSE练习2——流程控制

编程入门 行业动态 更新时间:2024-10-10 14:30:48

javaSE练习2——<a href=https://www.elefans.com/category/jswz/34/1770115.html style=流程控制"/>

javaSE练习2——流程控制

一、假设某员工今年的年薪是30000元,年薪的年增长率6%。编写一个Java应用程序计算该员工10年后的年薪,并统计未来10年(从今年算起)总收入。

package com.test;

public class t01 {

public static void main(String[] args) {
double salary = 3000; // 年薪
long sum = 0; // 总工资

for (int i = 1; i <= 10; i++) {
salary = salary * (1 + 0.06); // 计算年薪
sum += salary; // 计算总工资
}

System.out.println("年薪为:" + salary + "\n总工资为:" + sum);
}

}

效果图如下:

 

二、猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下的一半零一个。到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少。程序分析:采取逆向思维的方法,从后往前推断。

package com.test;

public class t02 {

public static void main(String[] args) {
int sum = 1;

for (int i = 2; i <= 10; i++) {
sum = (sum + 1) * 2;
}
System.out.println("猴子第一天摘了 " + sum + " 个桃子");

}

}

效果图如下:

 

三、编写一个程序,计算邮局汇款的汇费。如果汇款金额小于100元,汇费为一元,如果金额在100元与5000元之间,按1%收取汇费,如果金额大于5000元,汇费为50元。汇款金额由命令行输入。

package com.test;

import java.util.Scanner;

public class t03 {

public static void main(String[] args) {
double a, b = 0;
Scanner sc = new Scanner(System.in);
System.out.println("请输入汇款金额:");
a = sc.nextInt();

if (a > 5000) {
b = 50;
} else if (100 <= a) {
b = a * 0.01;
} else if (100 > a) {
b = 1.0;
}
System.out.println("汇费为:" + b);

}

}

效果图如下:

 

四、分别使用for循环,while循环,do循环求1到100之间所有能被3整除的整数的和。

package com.test;

public class t04 {

public static void main(String[] args) {
int i, a = 0; // 全局变量

// for循环
for (i = 1; i <= 100; i++) {
if (i % 3 == 0)
a = a + i;
System.out.println(a);
}

// while循环
/*
* while (i < 101) { if (i % 3 == 0) { a = a + i; } i++; }
* System.out.println(a);
*/

// do...while循环
/*
* do { if (i % 3 == 0) { a = a + i; } i++; } while (i < 100);
* System.out.println(a);
*/

}

}

效果图:忽略······

 

五、 输出0-9之间的数,但是不包括5。

package com.test;

public class t05 {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
if (i == 5) {
continue;
}
System.out.println(i);
}
}

}

效果图如下:

 

六、编写一个程序,求整数n的阶乘,例如5的阶乘是1*2*3*4*5。

package com.test;

import java.util.Scanner;

public class t06 {
public static void main(String[] args) {
int a, b = 0, x = 1;

Scanner sc = new Scanner(System.in);
System.out.println("请输入:");
a = sc.nextInt();

for (b = 1; b <= a; b++) {
x *= b; // 相当于x = x * b;
}
System.out.println(a + " 的阶乘为:" + x);
}

}

效果图如下:

 

七、编写一个程序,找出大于200的最小的质数。

package com.test;

public class t07 {
public static void main(String[] args) {
for (int i = 200; i < 300; i++) {
boolean x = true;
for (int j = 2; j < i; j++) {
if (i % j == 0) {
x = false;
break;
}
}
if (!x) {
continue;
}
System.out.println(i);
break;
}
}

}

效果图如下:

 

八、由命令行输入一个4位整数,求将该数反转以后的数,如原数为1234,反转后的数位4321。

package com.test;

import java.util.Scanner;

public class t08 {
public static void main(String[] args) {
int a, b, c, d, e, x;

Scanner sc = new Scanner(System.in);
System.out.println("输入数字:");
e = sc.nextInt();

a = e / 1000;
b = e / 100 % 10;
c = e / 10 % 10;
d = e % 10;
x = d * 1000 + c * 100 + b * 10 + a;

System.out.println("反转后数为:" + x);
}

}

效果图如下:

 

转载于:.html

更多推荐

javaSE练习2——流程控制

本文发布于:2024-02-07 12:58:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1756809.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:流程   javaSE

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!