java 控制语句练习题,Java练习题 - 流程控制

企业发放的奖金根据利润提成。利润低于或等于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%提成,从键盘输入当月利润,求应发放奖金总数?

public class Answer {

public static void main(String[] args) {

double x = 0,y = 0;

System.out.print("输入当月利润(万):");

Scanner s = new Scanner(System.in);

x = s.nextInt();

if(x > 0 && x <= 10) {

y = x * 0.1;

} else if(x > 10 && x <= 20) {

y = 10 * 0.1 + (x - 10) * 0.075;

} else if(x > 20 && x <= 40) {

y = 10 * 0.1 + 10 * 0.075 + (x - 20) * 0.05;

} else if(x > 40 && x <= 60) {

y = 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + (x - 40) * 0.03;

} else if(x > 60 && x <= 100) {

y = 20 * 0.175 + 20 * 0.05 + 20 * 0.03 + (x - 60) * 0.015;

} else if(x > 100) {

y = 20 * 0.175 + 40 * 0.08 + 40 * 0.015 + (x - 100) * 0.01;

}

System.out.println("应该提取的奖金是 " + y + "万");

}

}

给定一个成绩a,使用switch结构求出a的等级。A:90-100,B:80-89,C:70-79,D:60-69,E:0~59(知识点:条件语句switch)

public class Exercise_03_02 {

public static void main(String[] args) {

int x;

int grade = 0;

Scanner s = new Scanner(System.in);

System.out.print("请输入一个成绩: ");

x = s.nextInt();

if (x > 0 && x <= 100) {//判断成绩是否合法,如果合法,进行比较

grade = x/10;

switch(grade){

case 10:

case 9:System.out.println("等级为A");break;

case 8:System.out.println("等级为B");break;

case 7:System.out.println("等级为C");break;

case 6:System.out.println("等级为D");break;

default:System.out.println("等级为E");break;

}

} else {//判断成绩是否合法,如果非法,进行提示用户

System.out.println("输入的成绩必须在0-100之间" );

}

}

}

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

public class Exercise_03_03 {

public static void main(String[] args) {

double nianxin=30000;

long sum = 0;

for(int i=1;i<=10;i++){

nianxin = nianxin*(1+0.06);

sum+=nianxin;

}

System.out.println("年薪为"+nianxin+"总工资为"+sum);

}

}

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

public class Answer {

public static void main(String[] args) {

int x = 1;

for(int i=2; i<=10; i++) {

x = (x+1)*2;

}

System.out.println("猴子第一天摘了 " + x + " 个桃子");

}

}

输入一个数字,判断是一个奇数还是偶数

public class Exercise_03_05 {

public static void main(String[] args) {

if(i!=0){

System.out.println("0");

}

else if(i%2==1){

System.out.println("奇数");

}else if(i%2==0){

System.out.println("偶数");

}

}

}

编写程序, 判断一个变量x的值,如果是1,输出x=1,如果是5,输出x=5,如果是 10,输出x=10,除了以上几个值,都输出x=none。

public class Exercise_03_05 {

public static void main(String[] args) {

int x=1;

switch(x)

{

case 1:

{

System.out.println("x=1");

break;

}

case 5:

{

System.out.println("x=5");

break;

}

case 10:

{

System.out.println("x=10");

break;

}

default:

{

System.out.println("none");

break;

}

}

}

}

Switch说明

 表达式的值只可以接受int、byte、char、short 型,不接受其他类型的值

不允许有重复的case取值

 switch一旦碰到第一次case匹配,程序就会跳转到这个标签位置,开始顺序执行以后所有的程序代码,而不管后面的case条件是否匹配,直到碰到break语句为止

判断一个数字是否能被5和6同时整除(打印能被5和6整除),或只能被5整除(打印能被5整除),或只能被6整除,(打印能被6整除),不能被5或6整除,(打印不能被5或6整除)

public class Exercise_03_06 {

public static void main(String[] args) {

System.out.println("***********请输入一个整数*********");

Scanner scanner = new Scanner(System.in);

int value = scanner.nextInt();

if (value % 5 == 0 && value % 6 == 0) {

System.out.println("输入的数字" + value + "能被5和6整除");

} else if (value % 5 == 0) {

System.out.println("输入的数字" + value + "能被5整除");

} else if (value % 6 == 0) {

System.out.println("输入的数字" + value + "能被6整除");

} else {

System.out.println("输入的数字不能被5或者6整除");

}

}

}

输入一个年份,判断这个年份是否是闰年

public class Exercise_03_07 {

public static void main(String[] args) {

int year=2012;

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

System.out.println("闰年");

}else{

System.out.println("不是闰年");

}

}

}

输入一个0~100的分数,如果不是0~100之间,打印分数无效,根据分数等级打印A,B,C,D,E

public class Exercise_03_08 {

public static void main(String[] args) {

int score = 999;

if(score<=100&&score>=90)

System.out.println("A");

else if(score<90&&score>=80)

System.out.println("B");

else if(score<80&&score>=70)

System.out.println("C");

else if(score<70&&score>=60)

System.out.println("D");

else if(score<=70&&score>60)

System.out.println("E");

else

System.out.println("分数无效");

}

}

试写一个三位数,从小到大排列,然后再从大到小排列。

import java.util.Scanner;

public class Answer {

public static void main(String[] args) {

input fnc = new input();

int x=0, y=0, z=0;

System.out.print("输入第一个数字:");

x = fnc.input();

System.out.print("输入第二个数字:");

y = fnc.input();

System.out.print("输入第三个数字:");

z = fnc.input();

if(x > y) {

int t = x;

x = y;

y = t;

}

if(x > z) {

int t = x;

x = z;

z = t;

}

if(y > z) {

int t = y;

y = z;

z = t;

}

System.out.println( "三个数字由小到大排列为: "+x + " " + y + " " + z);

}

}

class input{

public int input() {

int value = 0;

Scanner s = new Scanner(System.in);

value = s.nextInt();

return value;

}

}

有一个不多于5位的正整数,求它是几位数,分别打印出每一位数字。

import java.util.Scanner;

public class Answer {

/**

* @param args the command line arguments

*/

public static void main(String[] args) {

// TODO code application logic here

// int num=12345;

System.out.println("请输入一个不多于五位的正整数:");

int num = input();

String str = String.valueOf(num);

System.out.println(num + "的位数为:" + str.length());

System.out.println("它的各位数分别为:");

for (int i = 0; i < str.length(); i++) {

// System.out.println(str.charAt(i));

System.out.print(str.charAt(i) + " ");

}

System.out.println();

System.out.println("它的各位数逆序分别为:");

for (int i = str.length() - 1; i >= 0; i--) {

// System.out.println(str.charAt(i));

System.out.print(str.charAt(i) + " ");

}

System.out.println();

}

private static int input() {

Scanner scanner = new Scanner(System.in);

int n = scanner.nextInt();

return n;

}

}

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

public class Answer {

public static void main(String[] args) {

double A = Integer.parseInt(args[0]);

double a = 0;

if (A > 5000) {

a = 50;

} else if (100 <= A) {

a = A * 0.01;

} else if (100 > A) {

a = 1.0;

}

System.out.println("汇费为:" + a);

}

}

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

public class Answer {

public static void main(String[] args) {

//for部分

{

int i, a = 0;

for (i = 1; i <= 100; i++) {

if (i % 3 == 0)

a = a + i;

}

System.out.println(a);

}

//while 部分

{

int i = 1, a = 0;

while (i < 101) {

if (i % 3 == 0) {

a = a + i;

}

i++;

}

System.out.println(a);

}

//do while部分

{

int i = 0, a = 0;

do {

if (i % 3 == 0) {

a = a + i;

}

i++;

} while (i < 101);

System.out.println(a);

}

}

}

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

public class Answer {

public static void main(String[] args)

{

for(int i=0;i<10;i++)

{

if(i==5)

{

continue;

}

System.out.println(i);

}

}

}

编写一个程序,求整数n的阶乘,例如5的阶乘是12345

import java.util.Scanner;

public class Answer {

public static void main(String[] args) {

System.out.println("input:");

Scanner in = new Scanner(System.in);

int n = in.nextInt();

int i = 0;

int m = 1;

for (i = 1; i <= n; i++) {

m = m * i;

}

System.out.println("n的阶乘为:"+m);

}

}

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

public class Answer {

public static void main(String[] args) {

// TODO Auto-generated method stub

for (int i = 200; i < 300; i++) {

boolean b = true;

for (int j = 2; j < i; j++) {

if (i % j == 0) {

b = false;

break;

}

}

if (!b) {

continue;

}

System.out.println(i);

break;

}

}

}

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

public class Answer {

public static void main(String[] args) {

int a, b, c, d, s;

int n = Integer.parseInt(args[0]);

a = n / 1000;

b = n / 100 % 10;

c = n / 10 % 10;

d = n % 10;

s = d * 1000 + c * 100 + b * 10 + a;

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

}

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值