![](https://img-blog.csdnimg.cn/20190927151117521.png?x-oss-process=image/resize,m_fixed,h_224,w_224)
🤣JavaSE
Java是一门面向对象编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承、指针等概念,因此Java语言具有功能强大和简单易用两个特征。
putongdeyang
这个作者很懒,什么都没留下…
展开
-
第七章第五题(打印不同的教)(Print distinct numbers) - 编程练习题答案
编写一个程序,读人10 个数并且显示互不相同的数(即一个数出现多次,但仅显示一次)。(提示,读人一个数,如果它是一个新数,则将它存储在数组中。如果该数已经在数组中,则忽略它。)输入之后,数组包含的都是不同的数。下面是这个程序的运行示例:Write a program that reads in ten numbers and displaysthe number of distinct numbers and the distinct numbers separated by exactly ones原创 2020-07-09 17:09:37 · 1481 阅读 · 6 评论 -
第七章第三题(计算数字的出现次数)(Count occurrence of numbers) - 编程练习题答案
编写程序,读取在1到100 之间的整数,然后计算每个数出现的次数。假定输入是以0 结束的。下面是这个程序的一个运行示例:Write a program that reads the integers between 1and 100 and counts the occurrences of each. Assume the input ends with 0.Note that if a number occurs more than one time, the plural word “times”原创 2020-07-03 19:09:48 · 1262 阅读 · 0 评论 -
第七章第二题(倒置输入的数)(Reverse the numbers entered) - 编程练习题答案
编写程序,读取10 个整数,然后按照和读入顺序相反的顺序将它们显示出来。Write a program that reads ten integers and displaysthem in the reverse of the order in which they were read.// https://cn.fankuiba.comimport java.util.Scanner;public class Ans7_2_page236 { public static void m原创 2020-06-18 16:20:41 · 510 阅读 · 0 评论 -
第七章第一题(指定等级)(Assign grades) - 编程练习题答案
(指定等级)编写一个程序,读入学生成绩,获取最髙分best, 然后根据下面的规则陚等级值• 如果分数>=best-10, 等级为A• 如果分数>=best-20, 等级为B• 如果分数>=best-30, 等级为C• 如果分数>=卜68140, 等级为D• 其他情况下,等级为F程序提示用户输入学生总数,然后提示用户输入所有的分数,最后显示等级得出结论。下面是一个运行示例:(Assign grades) Write a program that reads studen原创 2020-06-12 16:21:05 · 805 阅读 · 0 评论 -
第六章第三十九题(几何:点的位置)(Geometry: point position) - 编程练习题答案
6.39(几何:点的位置)编程练习题3.32显示如何测试一个点是否在一个有向直线的左侧、右侧,或在该直线上。使用下面的方法头编写该方法:public static boolean leftOfTheLine(double x0, double y0, double x1, double y1, double x2, double y2)public static boolean onTheSameLine(double x0, double y0, double x1, double y1, doubl原创 2020-06-12 03:34:30 · 317 阅读 · 0 评论 -
第六章第三十八题(生成随机字符)(Generate random characters) - 编程练习题答案
*6.38(生成随机字符)使用程序清单6-10RandomCharacter中的方法,打印100个大写字母及100个一位数字,每行打印10个。*6.38(Generate random characters) Use the methods in RandomCharacter in Listing 6.10 to print 100 uppercase letters and then 100 single digits, printing ten per line.下面是参考答案代码:// htt原创 2020-06-01 23:51:55 · 283 阅读 · 1 评论 -
Java提示错误: 找不到或无法加载主类
C:\Java\IdeaProjects\java>java xxxx错误: 找不到或无法加载主类 xxxx检查java在Idea终端中是否可用,如不可用,编辑编译器输出路径:文件→项目结构→模块→路径→输出目录 或 设置→工具→终端→项目设置→环境变量// https://cn.fankuiba.comC:\Java\IdeaProjects\java>java -versionjava version "1.8.0_231"Java(TM) SE Runtime Environ原创 2020-05-27 16:30:12 · 610 阅读 · 0 评论 -
第六章第三十七题(格式化整数)(Format an integer) - 编程练习题答案
6.37(格式化整数)使用下面的方法头编写一个方法,用于将整数格式化为指定宽度:public static String format(int number, int width)方法为数字number返回一个带有一个或多个以0作为前缀的字符串。字符串的位数就是宽度。比如,format(34,4)返回0034,format(34,5)返回00034。如果数字宽于指定宽度,方法返回该数字的字符串表示。比如,format(34,1)返回34。6.37(Format an integer)Write a m原创 2020-05-26 22:16:54 · 441 阅读 · 0 评论 -
第六章第三十六题(几何:正多边形的面积)(Geometry: area of a regular polygon) - 编程练习题答案
*6.36(几何:正多边形的面积)正多边形是一个n条边的多边形,它的每条边的长度都相等,而且所有角的角度也相等(即多边形既是等边又等角的)。计算正多边形面积的公式是:使用下面的方法头编写方法,返回正多边形的面积:public static double area(int n, double side)编写一个main方法,提示用户输入边的个数以及正多边形的边长,然后显示它的面积。下面是一个运行示例:Enter the number of sides: 5Enter the side:6.5Th原创 2020-05-25 15:01:38 · 566 阅读 · 0 评论 -
第六章第三十五题(几何:五边形的面积)(Geometry: area of a pentagon) - 编程练习题答案
6.35(几何:五边形的面积)五边形的面积可以使用下面的公式计算:编写一个方法,使用下面的方法头来返回五边形的面积。public static double area(double side)编写一个主方法,提示用户输入五边形的边,然后显示它的面积。下面是一个运行示例:Enter the side:5.5The area of the pentagon is 52.0444416.35(Geometry: area of a pentagon)The area of a pentagon ca原创 2020-05-24 17:00:38 · 503 阅读 · 0 评论 -
第六章第三十四题(打印日历)(Print calendar) - 编程练习题答案
**6.34(打印日历)编程练习题3.21使用Zeller一致性原理来计算某天是星期几。使用Zeller的算法简化程序清单6-12以获得每月开始的第一天是星期几。**6.34(Print calendar) Programming Exercise 3.21 uses Zeller’s congruence to calculate the day of the week. Simplify Listing 6.12, PrintCalendar.java, using Zeller’s algorith原创 2020-05-23 16:38:42 · 391 阅读 · 0 评论 -
第六章第三十三题(当前日期和时间)(Current date and time) - 编程练习题答案
**6.33(当前日期和时间)调用System.currentTimeMillis()返回从1970年1月1日0点开始至今为止的毫秒数。编写程序,显示当前日期和时间。下面是运行示例:Current date and time is May 16, 2012 10:34:23**6.33(Current date and time) Invoking System.currentTimeMillis() returns the elapsed time in milliseconds since mid原创 2020-05-22 16:04:08 · 409 阅读 · 0 评论 -
第六章第三十二题(游戏:赢取双骰子赌博游戏的机会)(Game: chance of winning at craps) - 编程练习题答案
**6.32(游戏:赢取双骰子赌博游戏的机会)修改编程练习题6.30使该程序运行10000次,然后显示赢得游戏的次数**6.32(Game: chance of winning at craps)Revise Exercise 6.30 to run it 15,000 times and display the number of winning games.下面是参考答案代码:// https://cn.fankuiba.compublic class Ans6_32_page205 {原创 2020-05-21 18:52:35 · 331 阅读 · 0 评论 -
第六章第三十一题(金融应用:信用卡号的合法性验证)(Financial: credit card number validation) - 编程练习题答案
**6.31(金融应用:信用卡号的合法性验证)信用卡号遵循某种模式。一个信用卡号必须是13到16位的整数。它的开头必须是:4,指Visa卡5,指Master卡37,指American Express 卡6,指Discover卡1954年,IBM的Hans Luhn提出一种算法,用于验证信用卡号的有效性。这个算法在确定输入的卡号是否正确,或者这张信用卡是否被扫描仪正确扫描方面是非常有用的。遵循这个合法性检测可以生成所有的信用卡号,通常称之为Luhn检测或者Mod 10检测,可以如下描述(为了方便解释原创 2020-05-20 18:29:44 · 771 阅读 · 0 评论 -
第六章第三十题(游戏:双骰子赌博)(Game: craps) - 编程练习题答案
**6.30(游戏:双骰子赌博)执双骰子游戏是赌场中非常流行的骰子游戏。编写程序,玩这个游戏的一个变种,如下所描述:执两个骰子。每个骰子有六个面,分别表示值1,2,…,6。检查这两个骰子的和。如果和为2、3或12(称为掷骰子(crap)),你就输了;如果和是7或者11(称作自然(natural)),你就赢了;但如果和是其他数字(例如:4、5、6、8、9或者10),就确定了一个点。继续掷骰子,直到掷出一个7或者掷出和刚才相同的点数。如果掷出的是7,你就输了。如果掷出的点数和你前一次掷出的点数相同,你就赢了。原创 2020-05-20 14:03:30 · 1365 阅读 · 0 评论 -
第六章第二十九题(双素数)(Twin primes) - 编程练习题答案
**6.29(双素数)双素数是指一对差值为2的素数。例如:3和5就是一对双素数,5和7是一对双素数,而11和13也是一对双素数。编写程序,找出小于1000的所有双素数。如下所示显示结果:(3,5)(5,7)…**6.29(Twin primes)(Twin primes) Twin primes are a pair of prime numbers that differ by 2. For example, 3 and 5 are twin primes, 5 and 7 are twin pr原创 2020-05-19 18:15:58 · 1214 阅读 · 0 评论 -
第六章第二十八题(梅森素数)(Mersenne prime) - 编程练习题答案
**6.28(梅森素数)如果一个素数可以写成的形式,其中p是某个正整数,那么这个素数就称作梅森素数。编写程序,找出p31的所有梅森素数,然后如下显示输入结果:**6.28(Mersenne prime) A prime number is called a Mersenne prime if it can be written in the form for some positive integer p. Write a program that finds all Mersenne pri原创 2020-05-19 18:09:07 · 894 阅读 · 0 评论 -
第六章第二十七题(反素数)(Emirp) - 编程练习题答案
**6.27(反素数)反素数(反转拼写的素数)是指一个非回文素数,将其反转之后也是一个素数。例如:17是一个素数,而31也是一个素数,所以17和71是反素数。编写程序,显示前100个反素数。每行显示10个,并且数字间用空格隔开,如下所示:13 17 31 37 71 73 79 97 107 113149 157 167 179 199 311 337 347 359 389…**6.27(Emirp)An emirp (prime spelled backward) is a nonpalindr原创 2020-05-18 17:26:49 · 1082 阅读 · 0 评论 -
第六章第二十六题(回文素数)(Palindromic prime) - 编程练习题答案
**6.26(回文素数)回文素数是指一个数同时为素数和回文数。例如:131是一个素数,同时也是一个回文素数。数学313和757也是如此。编写程序,显示前100个回文素数。每行显示10个数,数字中间用一个空格隔开。如下所示:2 3 5 7 11 101 131 151 181 191313 353 373 383 727 757 787 797 919 929…**6.26(Palindromic prime) A palindromic prime is a prime number and als原创 2020-05-17 14:24:42 · 1323 阅读 · 0 评论 -
第六章第二十五题(将毫秒数转换成小时数、分钟数和秒数)(Convert milliseconds to hours, minutes, and seconds) - 编程练习题答案
**6.25(将毫秒数转换成小时数、分钟数和秒数)使用下面的方法头,编写一个将毫秒数转换成小时数、分钟数和秒数的方法。public static String convertMillis(long millis)该方法返回形如“小时:分钟:秒”的字符串。例如:convertMillis(5500)返回字符串0:0:5,convertMillis(100000)返回字符串0:1:40,convertMillis(555550000)返回字符串154:19:10。编写一个测试程序,提示用户输入一个long型原创 2020-05-15 12:18:18 · 833 阅读 · 0 评论 -
第六章第二十四题(显示当前日期和时间)(Display current date and time) - 编程练习题答案
**6.24(显示当前日期和时间)程序清单2-7显示当前时间。改进这个例子,显示当前的日期和时间。程序清单6-12中日历例子可以提供一些如何求年、月和日的思路。**6.24(Display current date and time) Listing 2.7, ShowCurrentTime.java, displays the current time. Revise this example to display the current date and time. The calendar exam原创 2020-05-14 14:20:47 · 617 阅读 · 0 评论 -
第六章第二十三题(指定字符的出现次数)(Occurrences of a specified character) - 编程练习题答案
*6.23(指定字符的出现次数)使用下面的方法头编写一个方法,找到一个字符串中指定字符的出现次数。public static int count(String str, char a)例如,count(“Welcome”,‘e’)返回2。编写一个测试程序,提示用户输入一个字符串以及一个字符,显示该字符在字符串中出现的次数。*6.23(Occurrences of a specified character)Write a method that finds the number of occurren原创 2020-05-12 21:10:04 · 364 阅读 · 0 评论 -
第六章第二十二题(数学:平方根的近似求法)(Math: approximate the square root) - 编程练习题答案
**6.22(数学:平方根的近似求法)有几种实现Math类中sqrt方法的技术。其中一个称为巴比伦法。它通过使用下面的公式反复计算近似地得到一个数字n的平方根:nextGuess = (lastGuess + n / lastGuess) / 2当nextGuess和lastGuess几乎相同时,nextGuess就是平方根的近似值。最初的猜测值可以是任意一个正值(例如1)。这个值就是lastGuess的初始值。如果nextGuess和lastGuess的差小于很小的数,比如0.0001,就可以认为ne原创 2020-05-12 15:41:55 · 702 阅读 · 0 评论 -
第六章第二十一题(电话按键盘)(Phone keypads) - 编程练习题答案
*6.21(电话按键盘)国际标准的字母/数字匹配图如编程练习题4.15所示,编写一个方法,返回给定大写字母的数字,如下所示:int getNumber(char uppercaseLetter)编写一个测试程序,提示用户输入字符串形式的电话号码。输入的数字可能会包含字母。程序将字母(大写或者小写)翻译成一个数字,然后保持其他字符不变。*6.21(Phone keypads)The international standard letter/number mapping for telephones i原创 2020-05-12 14:57:56 · 1383 阅读 · 1 评论 -
第六章第二十题(计算一个字符串中字母的个数)(Count the letters in a string) - 编程练习题答案
*6.20(计算一个字符串中字母的个数)编写一个方法,使用下面的方法头计算字符串中的字母个数:public static int countLetters(String s)编写一个测试程序,提示用户输入字符串,然后显示字符串中的字母个数。*6.20(Count the letters in a string) Write a method that counts the number of letters in a string using the following header:public原创 2020-05-09 21:12:38 · 2304 阅读 · 0 评论