




已阅读5页,还剩33页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
5_1public class Exercise01 public static void main(String args) final int PENTAGONAL_NUMBERS_PER_LINE = 10;final int PENTAGONAL_NUMBERS_TO_PRINT = 100;int count = 1;int n = 1;while (count = PENTAGONAL_NUMBERS_TO_PRINT) int pentagonalNumber = getPentagonalNumber(n);n+;if (count % PENTAGONAL_NUMBERS_PER_LINE = 0)System.out.printf(%-7dn, pentagonalNumber);elseSystem.out.printf(%-7d, pentagonalNumber);count+;public static int getPentagonalNumber(int n) return n * (3 * n - 1) / 2;5_2import java.util.Scanner;public class Exercise02 public static void main(String args) Scanner input = new Scanner(System.in);/Prompt the user to enter an integerSystem.out.print(Enter an interger: );long number = input.nextLong();System.out.println(The sum of the digits in + number + is + sumDigits(number);public static int sumDigits(long n) int sum = 0;long remainingN = n;do long digit = remainingN % 10;remainingN = remainingN / 10;sum += digit; while (remainingN != 0);return sum;第03题import java.util.Scanner;public class Exercise03 public static void main(String args) Scanner input = new Scanner(System.in);/Prompt the user to enter an integerSystem.out.print(Enter an integer: );int number = input.nextInt();/Display resultSystem.out.println(Is + number + a palindrome? + isPalindrome(number);public static boolean isPalindrome(int number) if (number = reverse(number)return true;elsereturn false;public static int reverse(int number) int reverseNumber = 0;do int digit = number % 10;number = number / 10;reverseNumber = reverseNumber * 10 + digit; while (number != 0);return reverseNumber;第04题import java.util.Scanner;public class Exercise04 public static void main(String args) Scanner input = new Scanner(System.in);/Prompt the user to enter an integerSystem.out.print(Enter an integer: );int number = input.nextInt();/Display resultSystem.out.print(The reversal of + number + is );reverse(number);public static void reverse(int number) int reverseNumber = 0;do int digit = number % 10;number = number / 10;reverseNumber = reverseNumber * 10 + digit; while (number != 0);System.out.println(reverseNumber);第05题import java.util.Scanner;public class Exercise05 public static void main(String args) Scanner input = new Scanner(System.in);/Prompt the user to enter three numbersSystem.out.print(Enter three numbers: );double num1 = input.nextDouble();double num2 = input.nextDouble();double num3 = input.nextDouble();System.out.print(num1 + + num2 + + num3 + in increasing order: );displaySortedNumbers(num1, num2, num3);public static void displaySortedNumbers(double num1, double num2, double num3) double max = Math.max(Math.max(num1, num2), num3);double min = Math.min(Math.min(num1, num2), num3);double second = 0;if (num1 != max & num1 != min)second = num1;if (num2 != max & num2 != min)second = num2;if (num3 != max & num3 != min)second = num3;System.out.println(min + + second + + max);56import java.util.Scanner;public class Exercise06 public static void main(String args) Scanner input = new Scanner(System.in);/Prompt the user to enter an integerSystem.out.print(Enter an integer: );int number = input.nextInt();displayPattern(number);public static void displayPattern(int n) int i;int j;for (i = 1; i = n; i+) for (j = 0; j n - i; j+)System.out.print( );for (j = 0; j = i - 1; j+)System.out.printf(%-5d, i - j);System.out.println();5.7import java.util.Scanner;public class Exercise07 public static void main(String args) Scanner input = new Scanner(System.in);/Prompt the user to enter investment amountSystem.out.print(Enter the investment amount: );double investmentAmount = input.nextDouble();/Prompt the user to enter interest rateSystem.out.print(Enter the annual interest rate: );double annualInterestRate = input.nextDouble();/Prompt the user to enter yearsSystem.out.print(Enter number of years: );int years = input.nextInt();System.out.println(nThe amount invested: + investmentAmount);System.out.println(Annual interest rate: + annualInterestRate + %);System.out.println(YearstFuture Value);for (int i = 1; i = 31 & fahrenheit = 30; celsius-, fahrenheit -= 10) System.out.println(celsius + t + (int)(celsiusToFahrenheit(celsius) * 100) / 100.0 + tt + fahrenheit + tt + (int)(fahrenheitToCelsius(fahrenheit) * 100) / 100.0);public static double celsiusToFahrenheit(double celsius) return (9.0 / 5) * celsius + 32;public static double fahrenheitToCelsius(double fahrenheit) return (fahrenheit - 32) * 5.0 / 9;5.9public class Exercise09 public static void main(String args) System.out.println(FeettMeterstMeterstFeet);for (double feet = 1, meters = 20; feet = 10 & meters = 65; feet+, meters += 5) System.out.println(feet + t + (int)(footToMeter(feet) * 1000) / 1000.0 + t + meters + t + (int)(meterToFoot(meters) * 1000) / 1000.0);public static double footToMeter(double foot) return foot * 0.305;public static double meterToFoot(double meter) return meter / 0.305;5.10public class Exercise10 public static void main(String args) int count = 0;int number = 1;for (number = 1; number 10000; number+) if (isPrime(number)count+;System.out.println(The number of prime numbers less than 10000 is + count);public static boolean isPrime(int number) for (int i = 2; i = number / 2; i+) if (number % i = 0)return false;return true;5.11public class Exercise11 public static void main(String args) System.out.println(Sales AmounttCommission);for (double salesAmount = 10000.0; salesAmount = 100000; salesAmount += 5000) System.out.println(salesAmount + tt + computeCommission(salesAmount);public static double computeCommission(double salesAmount) if (salesAmount = 5000)return salesAmount * 0.08;else if (salesAmount = 10000)return 5000 * 0.08 + (salesAmount - 5000) * 0.10;elsereturn 5000 * 0.08 + (10000 - 5000) * 0.10 + (salesAmount - 10000) * 0.12;5.12public class Exercise12 public static void main(String args) char ch1 = 1;char ch2 = Z;int number = 10;printChars(ch1, ch2, number);public static void printChars(char ch1, char ch2, int numberPerLine) int count = 1;for (char i = ch1; i = ch2; i+) if (count % numberPerLine = 0)System.out.println(i);elseSystem.out.print(i + );count+;5.13public class Exercise13 public static void main(String args) System.out.println(itm(i);for (int i = 1; i = 1; i-)sum += i / (i + 1);return sum;5.14public class Exercise14 public static void main(String args) System.out.println(itm(i);for (int i = 10; i = 0; i -= 2) sum += 4 * (1 / (2 * i + 1) - 1 / (2 * (i + 1) + 1);return sum;5.15public class Exercise15 public static void main(String args) System.out.println(Taxble Single Married Married Head of);System.out.println(Income Joint Separate a House);int taxableIncome;int status;for (taxableIncome = 50000; taxableIncome = 60000; taxableIncome += 50) System.out.printf(%-5d, taxableIncome);System.out.print( );for (status = 0; status = 3; status+) System.out.printf(%-5d, (int)computetax(status, taxableIncome);System.out.print( );System.out.println();public static double computetax(int status, double taxableIncome) double tax = 0;double income = taxableIncome;if (status = 0) if (income = 8350)tax = income * 0.10;else if (income = 33950)tax = 8350 * 0.10 + (income - 8350) * 0.15;else if (income = 82250)tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (income - 33950) * 0.25;else if (income = 171550)tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (82250 - 33950) * 0.25 + (income - 82250) * 0.28;else if (income = 372950)tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (82250 - 33950) * 0.25 + (171550 - 82250) * 0.28 + (income - 171550) * 0.33;elsetax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (82250 - 33950) * 0.25 + (171550 - 82250) * 0.28 + (372950 - 171550) * 0.33 + (income - 372950) * 0.35;else if (status = 1) if (income = 16700)tax = income * 0.10;else if (income = 67900)tax = 16700 * 0.10 + (income - 16700) * 0.15;else if (income = 137050)tax = 16700 * 0.10 + (67900 - 16700) * 0.15 + (income - 67900) * 0.25;else if (income = 208850)tax = 16700 * 0.10 + (67900 - 16700) * 0.15 + (137050 - 67900) * 0.25 + (income - 137050) * 0.28;else if (income = 372950)tax = 16700 * 0.10 + (67900 - 16700) * 0.15 + (137050 - 67900) * 0.25 + (208850 - 137050) * 0.28 + (income - 208850) * 0.33;elsetax = 16700 * 0.10 + (67900 - 16700) * 0.15 + (137050 - 67900) * 0.25 + (208850 - 137050) * 0.28 + (372950 - 208850) * 0.33 + (income - 372950) * 0.35;else if (status = 2) if (income = 8350)tax = income * 0.10;else if (income = 33950)tax = 8350 * 0.10 + (income - 8350) * 0.15;else if (income = 68525)tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (income - 33950) * 0.25;else if (income = 104425)tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (68525 - 33950) * 0.25 + (income - 68525) * 0.28;else if (income = 186475)tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (68525 - 33950) * 0.25 + (104425 - 68525) * 0.28 + (income - 104425) * 0.33;elsetax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (68525 - 33950) * 0.25 + (104425 - 68525) * 0.28 + (186475 - 104425) * 0.33 + (income - 186475) * 0.35;else if (status = 3) if (income = 11950)tax = income * 0.10;else if (income = 45500)tax = 11950 * 0.10 + (income - 11950) * 0.15;else if (income = 117450)tax = 11950 * 0.10 + (45500 - 11950) * 0.15 + (income - 45500) * 0.25;else if (income = 190200)tax = 11950 * 0.10 + (45500 - 11950) * 0.15 + (117450 - 45500) * 0.25 + (income - 117450) * 0.28;else if (income = 372950)tax = 11950 * 0.10 + (45500 - 11950) * 0.15 + (117450 - 45500) * 0.25 + (190200 - 117450) * 0.28 + (income - 190200) * 0.33;elsetax = 11950 * 0.10 + (45500 - 11950) * 0.15 + (117450 - 45500) * 0.25 + (190200 - 117450) * 0.28 + (372950 - 190200) * 0.33 + (income - 372950) * 0.35;return tax;5.16public class Exercise16 public static void main(String args) for (int year = 2000; year = 2010; year+) System.out.println(Year + year + has + numberOfDaysInAYear(year) + days.);public static int numberOfDaysInAYear(int year) if (isLeapYear(year)return 366;elsereturn 365;public static boolean isLeapYear(int year) boolean isLeapYear = false;if (year % 4 = 0 & year % 100 != 0) | year % 400 = 0)isLeapYear = true;return isLeapYear;5.17import java.util.Scanner;public class Exercise17 public static void main(String args) Scanner input = new Scanner(System.in);System.out.print(Enter an integer number: );int n = input.nextInt();printMatrix(n);public static void printMatrix(int n) for (int i = 1; i = n; i+) for (int j = 1; j = n; j+)System.out.print(int)(Math.random() * 2) + );System.out.println();5.18public class Exercise18 public static void main(String args) System.out.println(NumbertSquareRoot);for (int i = 0; i side3 & side1 + side3 side2 & side2 + side3 side1)isValid = true;return isValid;/* * Returns the area of the triangle */public static double area(double side1, double side2, double side3) double s = (side1 + side2 + side3) / 2;double area = Math.sqrt(s * (s - side1) * (s - side2) * (s - side3);return area;5.20public class Exercise20 public static void main(String args) System.out.println(Degree tSin tCos);for (int degree = 0; degree 0.0001) lastGuess = nextGuess;nextGuess = (lastGuess + (n / lastGuess) / 2;return nextGuess;5.23public class Exercise23 public static void main(String args) final int NUMBERS_PER_LINE = 10;int count = 1;for (int i = 0; i 100; i+) if (count % NUMBERS_PER_LINE != 0)System.out.print(com.jackfangqi.chapter05.examples.RandomCharacter.getRandomUpperCaseLetter() + );if (count % NUMBERS_PER_LINE = 0)System.out.println(com.jackfangqi.chapter05.examples.RandomCharacter.getRandomUpperCaseLetter();count+;count = 1;for (int i = 0; i 100; i+) if (count % NUMBERS_PER_LINE != 0)System.out.print(com.jackfangqi.chapter05.examples.RandomCharacter.g
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 联合营销活动执行协议书
- 童年的味道记一次童年趣事作文8篇范文
- 护理学专业人才培养模式与影响因素分析
- 产品配方表格-原料配比
- 个人用户隐私保护条款合作协议
- 高中以定格为话题作文9篇范文
- 教育行业教师资格证书持有证明(8篇)
- 英语听力与口语技能试题集
- 区域性与文化差异在地理学人才培养中的适应性策略
- 生活中的好习惯值得培养议论文作文(5篇)
- 电力咨询费合同协议
- 2025-2030海洋环境监测行业市场深度调研及发展前景与投资研究报告
- 2025年中学生离队入团活动实施方案
- 玻璃基板制备技术考核试卷
- 南极磷虾油与红曲、辅酶Q10联用降低血脂效果研究
- 2025年上海市安全员C3证(专职安全员-综合类)考试题库
- 钱大妈加盟合同协议
- 《建筑工程识图》课件-梁平法施工图识读一
- 上海杨浦区社区工作者考试真题2024
- 汽车智能制造技术考核试卷
- 新公司法试题及答案
评论
0/150
提交评论