流程控制语句练习
标签 :Java入坑之旅
0x00. 题目
- 企业发放的奖金根据利润提成。利润低于或等于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%提成,在程序中设定一个变量为当月利润,求应发放奖金总数?(知识点:条件语句) [必做题]
- 给定一个成绩a,使用switch结构求出a的等级。A:90-100,B:80-89,C:70-79,D:60-69,E:0~59(知识点:条件语句switch)[必做题]
- 假设某员工今年的年薪是30000元,年薪的年增长率6%。编写一个Java应用程序计算该员工10年后的年薪,并统计未来10年(从今年算起)总收入。(知识点:循环语句for)[选做题]
- 猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个,第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下的一半零一个。到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少。(知识点:循环语句 while)[选做题]
- 输入一个数字,判断是一个奇数还是偶数(知识点:条件语句) [必做题]
- 编写程序, 判断一个变量x的值,如果是1,输出x=1,如果是5,输出x=5,如果是10,输出x=10,除了以上几个值,都输出x=none。(知识点:条件语句) [必做题]
- 判断一个数字是否能被5和6同时整除(打印能被5和6整除),或只能被5整除(打印能被5整除),或只能被6整除,(打印能被6整除),不能被5或6整除,(打印不能被5或6整除)(知识点:条件语句) [必做题]
- 输入一个年份,判断这个年份是否是闰年(知识点:条件、循环语句) [必做题]
- 输入一个0~100的分数,如果不是0~100之间,打印分数无效,根据分数等级打印A,B,C,D,E(知识点:条件语句if elseif) [必做题]
- 输入三个整数x,y,z,请把这三个数由小到大输出(知识点:条件语句) [必做题]
- 有一个不多于5位的正整数,求它是几位数,分别打印出每一位数字。(知识点:条件语句) [必做题]
- 编写一个程序,计算邮局汇款的汇费。如果汇款金额小于100元,汇费为一元,如果金额在100元与5000元之间,按1%收取汇费,如果金额大于5000元,汇费为50元。汇款金额由命令行输入。(知识点:条件语句) [选做题]
- 分别使用for循环,while循环,do循环求1到100之间所有能被3整除的整数的和。(知识点:循环语句) [选做题]
- 输出0-9之间的数,但是不包括5。 [选做题]
- 编写一个程序,求整数n的阶乘,例如5的阶乘是12345 [选做题]
- 编写一个程序,找出大于200的最小的质数[选做题]
- 由命令行输入一个4位整数,求将该数反转以后的数,如原数为1234,反转后的数位4321 [选做题]
0x01. 题01-06
企业发放的奖金根据利润提成。利润低于或等于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%提成,在程序中设定一个变量为当月利润,求应发放奖金总数?(知识点:条件语句) [必做题]
package com.ryanjie.test; import java.util.Scanner; /** * @ClassName:TotalPrizes * @Description: total prizes * @author: Ryanjie * @date:2018年8月8日 下午10:31:27 * * @Copyright: 2018 Ryanjie Inc. All rights reserved. */ public class TotalPrizes { /** * @Title: total * @Description: the total of prizes of profit * * @param profit * @return: void * @throws */ public static void total(double profit) { if (profit <= 10) { System.out.print(0.1 * profit); } else if (profit <= 20) { System.out.print(0.1 * 10 + 0.075 * (profit - 10) ); } else if (profit <= 40) { System.out.print(0.1 * 10 + 0.075 * 10 + 0.05 * (profit - 20)); } else if (profit <= 60) { System.out.print(0.1 * 10 + 0.075 * 10 + 0.05 * 20 + 0.03 * (profit - 40)); } else if (profit <= 100) { System.out.print(0.1 * 10 + 0.075 * 10 + 0.05 * 20 + 0.03 * 20 + 0.015 * (profit - 60)); } else { System.out.print(0.1 * 10 + 0.075 * 10 + 0.05 * 20 + 0.03 * 20 + 0.015 * 40 + 0.01 * (profit - 100)); } } public static void main(String[] args) { while (true) { System.out.println("Please input the profit<int number> :"); Scanner in = new Scanner(System.in); double profit = in.nextInt(); System.out.print("the total prizes is "); total(profit); System.out.println("(万元)"); } } }
给定一个成绩a,使用switch结构求出a的等级。A:90-100,B:80-89,C:70-79,D:60-69,E:0~59(知识点:条件语句switch)[必做题]
package com.ryanjie.test; import java.util.Scanner; /** * @Title: ScoresGrade.java * @Package com.ryanjie.test * @ClassName:ScoresGrade * @Description:Scores Grade * @author: Ryanjie * @date:2018年8月8日 下午4:21:02 * * @Copyright: 2018 Ryanjie Inc. All rights reserved. */ public class ScoresGrade { /** * @Title: grade * @Description: 判断成绩等级 * * @param score * @return: void * @throws */ public static void grade(double score) { if (score < 60) { System.out.println("garde: E"); } else if (score < 70) { System.out.println("grade: D"); } else if (score < 80) { System.out.println("grade: C"); } else if (score < 90) { System.out.println("grade:B"); } else if (score <=100) { System.out.println("grade:A"); } else { System.out.println("您输入的成绩有错!"); } } /** * @Description: ScoreGrade */ public static void main(String[] args) { // TODO 自动生成的方法存根 System.out.println("Please input your score <0~100>:"); Scanner in = new Scanner(System.in); double score = in.nextInt(); grade(score); } }
假设某员工今年的年薪是30000元,年薪的年增长率6%。编写一个Java应用程序计算该员工10年后的年薪,并统计未来10年(从今年算起)总收入。(知识点:循环语句for)[选做题]
package com.ryanjie.test; /** * @ClassName:TotalRevenue * @Description:the total of income * @author: Ryanjie * @date:2018年8月8日 下午11:43:10 * * @Copyright: 2018 Ryanjie Inc. All rights reserved. */ public class TotalRevenue { /** * @Title: total * @Description: the wages and total revenue after 10 years * * @param sal * @param rate * @param years * @return: void * @throws */ public static void total(double sal, double rate, int years) { double sum = 0; for(int i = 0; i <= years; i++) { sum += sal; sal += (rate * sal); } System.out.println("wages after " + years + "years: " + sal + "(元)"); System.out.println("total revenue after " + years + "years: " + sum + "(元)"); } public static void main(String[] args) { System.out.println(); double sal = 30000; double rate = 0.06; int years = 10; total(sal, rate, years); } }
猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个,第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下的一半零一个。到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少。(知识点:循环语句 while)[选做题]
package com.ryanjie.test; /** * @ClassName:MonkeyPickPeach * @Description:monkey picking peach * @author: Ryanjie * @date:2018年8月8日 下午11:58:45 * * @Copyright: 2018 Ryanjie Inc. All rights reserved. */ public class MonkeyPickPeach { /** * @Title: sumPeach * @Description: the sum of peach before 10 days * * @param days * @return: void * @throws */ public static void sumPeach(int days) { int sum = 1; for(int i = 1; i < days; ++i) { sum = (++ sum) * 2; } System.out.println("Before " + days + " days, the sum of peach is " + sum); } public static void main(String[] args) { int days = 10; sumPeach(days); } }
输入一个数字,判断是一个奇数还是偶数(知识点:条件语句) [必做题]
package com.ryanjie.test; import java.util.Scanner; /** * @ClassName:JudgeOddEvenNumber * @Description: Judge a number is odd number or even number * @author: Ryanjie * @date:2018年8月8日 下午5:14:01 * * @Copyright: 2018 Ryanjie Inc. All rights reserved. */ public class JudgeOddEvenNumber { /** * @Title: judge * @Description: Judge number is odd number or even number * * @param num * @return: void * @throws */ public static void judge(int num) { if(num % 2 == 0) { System.out.println("even number"); } else { System.out.println("odd number"); } } public static void main(String[] args) { // TODO 自动生成的方法存根 System.out.println("Please input a number:"); Scanner in = new Scanner(System.in); int number = in.nextInt(); judge(number); } }
编写程序, 判断一个变量x的值,如果是1,输出x=1,如果是5,输出x=5,如果是10,输出x=10,除了以上几个值,都输出x=none。(知识点:条件语句) [必做题]
package com.ryanjie.test; import java.util.Scanner; /** * @ClassName:JudgeValue * @Description:Judge Numer Value * @author: Ryanjie * @date:2018年8月8日 下午5:24:01 * * @Copyright: 2018 Ryanjie Inc. All rights reserved. */ public class JudgeValue { /** * @Title: judge * @Description: Judge Numer Value * * @param num * @return: void * @throws */ public static void judge(int num) { switch (num) { case 1: System.out.println("1"); break; case 5: System.out.println("5"); break; case 10: System.out.println("10"); break; default: System.out.println("none"); } } public static void main(String[] args) { System.out.println("Please input a number:"); Scanner in = new Scanner(System.in); int number = in.nextInt(); judge(number); } }
0x02. 题07-12
判断一个数字是否能被5和6同时整除(打印能被5和6整除),或只能被5整除(打印能被5整除),或只能被6整除,(打印能被6整除),不能被5或6整除,(打印不能被5或6整除)(知识点:条件语句) [必做题]
package com.ryanjie.test; import java.util.Scanner; /** * @ClassName:Divisible * @Description:Divisible of n * @author: Ryanjie * @date:2018年8月8日 下午10:11:40 * * @Copyright: 2018 Ryanjie Inc. All rights reserved. */ public class Divisible { /** * @Title: divisibleByN * @Description: divisible by n * * @param num1 * @param num2 * @return * @return: boolean * @throws */ public static boolean divisibleByN(int num1, int num2) { boolean temp = false; if (num1 % num2 == 0) { temp = true; } return temp; } public static void main(String[] args) { while (true) { System.out.println("Please input a number <int>:"); Scanner in = new Scanner(System.in); int number = in.nextInt(); if(divisibleByN(number, 5) && divisibleByN(number, 6)) { System.out.println(number + " divisible by 5 and 6"); } else if (divisibleByN(number, 5)) { System.out.println(number + " divisible by 5"); } else if (divisibleByN(number, 6)) { System.out.println(number + " divisible by 6"); } else { System.out.println(number + " cannot divisible by 5 or 6"); } } } }
输入一个年份,判断这个年份是否是闰年(知识点:条件、循环语句) [必做题]
package com.ryanjie.test; import java.util.Scanner; /** * @ClassName:LeapYear * @Description: Judge year is leap year * @author: Ryanjie * @date:2018年8月8日 下午5:34:06 * * @Copyright: 2018 Ryanjie Inc. All rights reserved. */ public class LeapYear { /** * @Title: isLeapYear * @Description: Judge Year Is LeepYear * * @param year * @return: void * @throws */ public static void isLeapYear(int year) { if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) { System.out.println("Is LeepYear"); } else { System.out.println("Is not Leepyear"); } } public static void main(String[] args) { System.out.println("Please input year:"); Scanner in = new Scanner(System.in); int year = in.nextInt(); isLeapYear(year); } }
输入一个0~100的分数,如果不是0~100之间,打印分数无效,根据分数等级打印A,B,C,D,E(知识点:条件语句if elseif) [必做题]
package com.ryanjie.test; import java.util.Scanner; /** * @Title: ScoresGrade.java * @Package com.ryanjie.test * @ClassName:ScoresGrade * @Description:Scores Grade * @author: Ryanjie * @date:2018年8月8日 下午4:21:02 * * @Copyright: 2018 Ryanjie Inc. All rights reserved. */ public class ScoresGrade { /** * @Title: grade * @Description: 判断成绩等级 * * @param score * @return: void * @throws */ public static void grade(double score) { if (score < 60) { System.out.println("garde: E"); } else if (score < 70) { System.out.println("grade: D"); } else if (score < 80) { System.out.println("grade: C"); } else if (score < 90) { System.out.println("grade:B"); } else if (score <=100) { System.out.println("grade:A"); } else { System.out.println("您输入的成绩有错!"); } } /** * @Description: ScoreGrade */ public static void main(String[] args) { // TODO 自动生成的方法存根 System.out.println("Please input your score <0~100>:"); Scanner in = new Scanner(System.in); double score = in.nextInt(); grade(score); } }
输入三个整数x,y,z,请把这三个数由小到大输出(知识点:条件语句) [必做题]
package com.ryanjie.test; import java.util.Scanner; /** * @ClassName:ThreeNumberSort * @Description:TODO * @author: Ryanjie * @date:2018年8月8日 下午8:36:46 * * @Copyright: 2018 Ryanjie Inc. All rights reserved. */ public class ThreeNumberSort { // /** // * @Title: swap // * @Description:Swap two numbers // * // * @param num1 // * @param num2 // * @return: void // * @throws // */ // public static void swap(double num1, double num2) { // double temp; // temp = num1; // num1 = num2; // num2 = temp; // } // // /** // * @Title: sort // * @Description: Three numbers sort // * // * @param number1 // * @param number2 // * @param number3 // * @return: void // * @throws // */ // public static void sort(double number1, double number2, double number3) { // if (number1 < number2) { // swap(number1, number2); // } // if (number1 < number3) { // swap(number1, number3); // } // if (number2 < number3) { // swap(number2, number3); // } // System.out.printf(number3 + " > " + number2 + " > " + number1); // } public static void main(String[] args) { while(true) { System.out.println("Please input three numbers:"); Scanner in = new Scanner(System.in); double number1 = in.nextInt(); double number2 = in.nextInt(); double number3 = in.nextInt(); if (number1 < number2) { double temp = number1; number1 = number2; number2 = temp; } if (number1 < number3) { double temp = number1; number1 = number3; number3 = temp; } if (number2 < number3) { double temp = number2; number2 = number3; number3 = temp; } System.out.printf(number1 + " > " + number2 + " > " + number3); System.out.println(); } } }
有一个不多于5位的正整数,求它是几位数,分别打印出每一位数字。(知识点:条件语句) [必做题]
package com.ryanjie.test; import java.util.Scanner; /** * @ClassName:PrintBitDigitNumber * @Description:print a 5 digit number for each bit * @author: Ryanjie * @date:2018年8月9日 上午12:12:01 * * @Copyright: 2018 Ryanjie Inc. All rights reserved. */ public class PrintBitDigitNumber { /** * @Title: getNumLength * @Description: get the number length * * @param number * @return: void * @throws */ public static void getNumLength(long number) { System.out.println("The number<" + number + "> has " + (int)(Math.log10(number) + 1) + " bits"); } /** * @Title: divideBitNumber * @Description: divide the number each bit * * @param number * @return: void * @throws */ public static void divideBitNumber(long number) { int length = (int)(Math.log10(number)); int highBit = 1; for(int i = 0; i < length; ++i) { highBit *= 10; } System.out.print("From high to low ,each bit is "); while (number > 0) { System.out.print(number/highBit + ","); number -= (number/highBit)*highBit; highBit /= 10; } System.out.println(); } public static void main(String[] args) { while (true) { System.out.println("Please input a number<long>: "); Scanner in = new Scanner(System.in); long number = in.nextInt(); getNumLength(number); divideBitNumber(number); System.out.println(); } } }
编写一个程序,计算邮局汇款的汇费。如果汇款金额小于100元,汇费为一元,如果金额在100元与5000元之间,按1%收取汇费,如果金额大于5000元,汇费为50元。汇款金额由命令行输入。(知识点:条件语句) [选做题]
package com.ryanjie.test; import java.util.Scanner; /** * @ClassName:RemittanceFee * @Description:remittance fee * @author: Ryanjie * @date:2018年8月9日 上午1:15:01 * * @Copyright: 2018 Ryanjie Inc. All rights reserved. */ public class RemittanceFee { /** * @Title: sumFee * @Description: remittance fee * * @param amount * @return: void * @throws */ public static void sumFee(double amount) { double sum; if (amount < 100) { sum = 1.0; } else if (amount <= 5000) { sum = 0.01 * amount; } else { sum = 50.0; } System.out.println("The remittance fee is " + sum +"(RMB)"); } public static void main(String[] args) { while (true) { System.out.println("Please input the amount<double>:"); Scanner in = new Scanner(System.in); double amount = in.nextInt(); sumFee(amount); System.out.println(); } } }
0x03. 题13-17
分别使用for循环,while循环,do循环求1到100之间所有能被3整除的整数的和。(知识点:循环语句) [选做题]
package com.ryanjie.test; import java.util.Scanner; /** * @ClassName:SumOfNumbersDivisibleByThree * @Description: the sum of the numbers that can be divisible by three * @author: Ryanjie * @date:2018年8月8日 下午9:16:17 * * @Copyright: 2018 Ryanjie Inc. All rights reserved. */ public class SumOfNumbersDivisibleByThree { /** * @Title: sumDivisibleByThree * @Description: the sum of the numbers that can be divisible by three * * @param num1 * @param num2 * @return: void * @throws */ public static void sumDivisibleByThree(int num1,int num2) { int sum = 0; for(int i = num1; i <= num2; i++) { if(i % 3 == 0) { sum += i; } } System.out.println(sum); } public static void main(String[] args) { System.out.println(""); Scanner in = new Scanner(System.in); // int start = in.nextInt(); // int end = in.nextInt(); int start = 1; int end = 100; sumDivisibleByThree(start, end); } }
输出0-9之间的数,但是不包括5。 [选做题]
package com.ryanjie.test; import java.util.Scanner; /** * @ClassName:PrintfNineNumbers * @Description:printf 0-9 apart from 5 * @author: Ryanjie * @date:2018年8月8日 下午9:25:06 * * @Copyright: 2018 Ryanjie Inc. All rights reserved. */ public class PrintfNineNumbers { /** * @Title: printfNumbers * @Description: printf 0-9 apart from 5 * * @param number1 * @param number2 * @return: void * @throws */ public static void printfNumbers(int number1, int number2) { int temp = 0; for(int i = number1; i <= number2; i++) { if (i != 5) { System.out.printf(i + "\t"); if((++ temp) % 5 == 0) { System.out.println(); } } } } public static void main(String[] args) { System.out.println(""); Scanner in = new Scanner(System.in); // int start = in.nextInt(); // int end = in.nextInt(); int start = 0; int end = 9; printfNumbers(start, end); } }
编写一个程序,求整数n的阶乘,例如5的阶乘是12345 [选做题]
package com.ryanjie.test; import java.util.Scanner; /** * @ClassName:Factorial * @Description:Factorial * @author: Ryanjie * @date:2018年8月8日 下午9:59:47 * * @Copyright: 2018 Ryanjie Inc. All rights reserved. */ public class Factorial { /** * @Title: factorialOfN * @Description: Factorial of n * * @param n * @return: void * @throws */ public static void factorialOfN(int n) { int sum = 1; for(int i = 1; i <= n; ++i) { sum *= i; } System.out.printf("Factorial of " + n + " is " + sum ); } public static void main(String[] args) { System.out.println("Please input a number <int> :"); Scanner in = new Scanner(System.in); int number = in.nextInt(); factorialOfN(number); } }
编写一个程序,找出大于200的最小的质数[选做题]
package com.ryanjie.test; /** * @ClassName:Prime * @Description:找出大于200的最小的质数 * @author: Ryanjie * @date:2018年8月8日 下午3:02:18 * * @Copyright: 2018 Ryanjie Inc. All rights reserved. */ public class Prime { /** * @Title: isPrime * @Description: 判断一个数是否为质数 * * @param num * @return: boolean * @throws */ public static boolean isPrime(long num) { if(num > 2 && (num & 1) ==0) { return false; } for(int i = 3; i * i <= num; i += 2) { if(num % i ==0 ) { return false; } } return true; } /** * @Description: 找出大于200的最小的质数 */ public static void main(String[] args) { int start = 200; int end = 300; int temp = 0; for(int i = start; i <= end; ++i) { if(isPrime(i)) { System.out.printf(i + "\t"); if ((++ temp) % 10 ==0) { System.out.println(); } } } } }
由命令行输入一个4位整数,求将该数反转以后的数,如原数为1234,反转后的数位4321 [选做题]
package com.ryanjie.test; import java.util.Scanner; /** * @ClassName:ReverseNumer * @Description:reverse number * @author: Ryanjie * @date:2018年8月9日 上午1:27:24 * * @Copyright: 2018 Ryanjie Inc. All rights reserved. */ public class ReverseNumer { /** * @Title: reverseMethod1 * @Description:reverse method1 * * @param number * @return: void * @throws */ public static void reverseMethod1(long number) { System.out.print("Method1:"); while(number > 0) { System.out.print(number % 10); number /= 10; } System.out.println(); } /** * @Title: reverseMethod2 * @Description: reverse method2 * * @param number * @return: void * @throws */ public static void reverseMethod2(long number) { System.out.print("Method2:"); String reserveNumber = ""; while(number > 0) { reserveNumber += (number % 10); number /= 10; } System.out.println(reserveNumber); } public static void main(String[] args) { while (true) { System.out.println("Please input a number<long>:"); Scanner in = new Scanner(System.in); long number = in.nextInt(); reverseMethod1(number); reverseMethod2(number); System.out.println(); } } }