第六章第二十一题(电话按键盘)(Phone keypads) – 编程练习题答案

*6.21(电话按键盘)国际标准的字母/数字匹配图如编程练习题4.15所示,编写一个方法,返回给定大写字母的数字,如下所示:

int getNumber(char uppercaseLetter)

编写一个测试程序,提示用户输入字符串形式的电话号码。输入的数字可能会包含字母。程序将字母(大写或者小写)翻译成一个数字,然后保持其他字符不变。

*6.21(Phone keypads)The international standard letter/number mapping for telephones is given in Programming Exercise 4.15. Write a method that returns a number, given an uppercase letter, as follows:

int getNumber(char uppercaseLetter)

Write a test program that prompts the user to enter a phone number as a string. The input number may contain letters. The program translates a letter (uppercase or lowercase) to a digit and leaves all other characters intact.

下面是参考答案代码:

import java.util.Scanner;

public class Ans6_21_page201 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a string: ");
        String phoneNumber = input.nextLine().toUpperCase();
        for (int i =0; i < phoneNumber.length(); i++) {
            if (Character.isDigit(phoneNumber.charAt(i))) {
                System.out.print(phoneNumber.charAt(i));
            }
            else if (getNumber(phoneNumber.charAt(i)) == 1)
                System.out.print("-");
            else
                System.out.print(getNumber(phoneNumber.charAt(i)));
        }
    }
    public static int getNumber(char uppercaseLetter) {
        int num = 99;
        switch (uppercaseLetter+"") {
            case "-":
                num = 1;
                break;
            case "A":
                num = 2;
                break;
            case "B":
                num = 2;
                break;
            case "C":
                num = 2;
                break;
            case "D":
                num = 3;
                break;
            case "E":
                num = 3;
                break;
            case "F":
                num = 3;
                break;
            case "G":
                num = 4;
                break;
            case "H":
                num = 4;
                break;
            case "I":
                num = 4;
                break;
            case "J":
                num = 5;
                break;
            case "K":
                num = 5;
                break;
            case "L":
                num = 5;
                break;
            case "M":
                num = 6;
                break;
            case "N":
                num = 6;
                break;
            case "O":
                num = 6;
                break;
            case "P":
                num = 7;
                break;
            case "Q":
                num = 7;
                break;
            case "R":
                num = 7;
                break;
            case "S":
                num = 7;
                break;
            case "T":
                num = 8;
                break;
            case "U":
                num = 8;
                break;
            case "V":
                num = 8;
                break;
            case "W":
                num = 9;
                break;
            case "X":
                num = 9;
                break;
            case "Y":
                num = 9;
                break;
            case "Z":
                num = 9;
        }
        return num;
    }
}

适用Java语言程序设计与数据结构(基础篇)(原书第11版)Java语言程序设计(基础篇)(原书第10/11版)

第六章第二十题(计算一个字符串中字母的个数)(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 static int countLetters(String s)

Write a test program that prompts the user to enter a string and displays the number of letters in the string.

下面是参考答案代码:

import java.util.Scanner;

public class Ans6_20_page201 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter : ");
        String s = input.nextLine();
        System.out.println("The number of letters is "+ countLetters(s));
    }
    public static int countLetters(String s) {
        int count = 0;
        for (int i = 0;i < s.length();i++) {
            if (Character.isLetter(s.charAt(i)))
                count++;
        }
        return count;
    }
}

适用Java语言程序设计与数据结构(基础篇)(原书第11版)Java语言程序设计(基础篇)(原书第10/11版)

Java精确计算闰年

闰年366天(2月中多一天),闰年2月29天
普通闰年:公历年份是4的倍数的,且不是100的倍数,为普通闰年。(如2004年就是闰年)
世纪闰年:公历年份是整百数的,必须是400的倍数才是世纪闰年(如1900年不是世纪闰年,2000年是世纪闰年)

import java.util.Scanner;
public class LeapYear {
    public static void main(String[] args) {    
        Scanner input = new Scanner(System.in);
        System.out.print("输入年份:");
        int year = input.nextInt();
         
        if((year % 4 == 0 && year % 100 != 0) || (year%400==0 && year % 3200 != 0) || year % 172800 == 0)
            System.out.print(year + "年是闰年");
        else 
            System.out.print(year + "年不是闰年");
    }
}

IntelliJ IDEA 每日提示 每日一贴

1、要快速完成对位于项目,库或JDK中任何位置的静态方法的方法调用,请输入前缀,然后按两次Ctrl +空格。 您可以按Alt + Enter导入所选方法。

2、您可以从内置终端使用您喜欢的外壳。
在“设置/首选项”对话框(Ctrl + Alt + S)中,转到“工具” |“首选项”。 终端并指定您的Shell可执行文件的路径。

3、在编辑器中按Alt + Enter可修复突出显示的错误或警告,改善或优化代码结构。

4、使用编辑| 复制引用动作可将对字段/方法/类/文件的引用插入编辑器中的当前位置。
将插入符号放在myMethod方法名称中,然后按Ctrl + Alt + Shift + C:

您也可以在“转到类/转到符号/转到文件”对话框中复制引用。 在查找列表中的任何元素上按Ctrl + C。
要粘贴参考,请按Ctrl + V:

5、您要通过版权保护项目中的任何文件吗? 转到“设置/首选项”对话框Ctrl + Alt + S,选择“编辑器” |“选择”。 版权| 版权配置文件,创建版权配置文件,并添加相关的版权变量。
例如,使用$ today.year来使版权年份保持最新。

6、您可以开始引用Ant属性或目标,即使尚未定义。 按Alt + Enter并从意图操作列表中选择“创建属性”以创建必要的标签,而无需保留当前的编辑位置。

7、在编辑器中按Ctrl + D可复制所选的块,或在未选择任何块的情况下复制当前行。

8、使用代码完成功能时,可以通过按Ctrl +. 选择使用第一项。
IntelliJ IDEA社区将插入所选项目,然后插入点或->,具体取决于当前上下文。

9、要扩展选择,请按Ctrl + W。 每次按Ctrl + W,选择范围就会扩展到其他代码区域。
例如,选择范围从方法名称扩展到调用此方法的表达式,然后扩展到整个语句,再扩展到包含块,依此类推。

10、您可以使用专用的编辑器在代码中编辑语言注入。
例如,要编辑正则表达式,请开始输入正则表达式,按Alt + Enter并选择“编辑RegExp片段”。 正则表达式在编辑器的单独选项卡中打开,您可以在其中直接输入反斜杠。
所有更改都与原始正则表达式同步,并且转义字符会自动显示。 准备好后,按Esc键关闭正则表达式编辑器。

11、本地历史记录使您可以跟踪对文件,类,方法或任何代码片段的所有更改,并在必要时回滚到任何稳定点。 要查看本地历史记录,请从主菜单中选择VCS | 当地历史| 显示历史记录。

12、从右到左应用后缀代码完成功能,以避免在编码时向后插入符号。 输入一个点。 要更改的代码片段之后,然后选择所需的选项。
要查看带有后缀模板的建议列表,还可以按Ctrl + J。
Postfix code completion is applied from right to left to avoid backward caret jumps when coding. Type a dot . after the code fragment that you want to change and select the desired option.
To see the suggestion list with postfix templates, you can also press Ctrl+J.

13、要通过从装订线中拖动断点来删除断点,请按Ctrl + Alt + S组合键,转到“构建,执行,部署” |“展开”。 调试器,然后选择“拖动到编辑器”或单击鼠标中键。
单击断点将随后切换其状态(启用/禁用)。
To remove breakpoints by dragging them from the gutter, press Ctrl+Alt+S, go to Build, Execution, Deployment | Debugger and select Drag to the editor or click with middle mouse button.
Clicking a breakpoint will then toggle its status (enabled/disabled).

14、Press Ctrl+Shift+E to get a list of recently viewed or changed code fragments:
按Ctrl + Shift + E获取最近查看或更改的代码片段的列表:

15、To select a part of your code, drag your mouse while pressing Ctrl .
要选择一部分代码,请在按住Ctrl的同时拖动鼠标。

16、You can use your favorite shell from the built-in Terminal.
In the Settings/Preferences dialog Ctrl+Alt+S , go to Tools | Terminal and specify the path to your shell executable.
您可以从内置终端使用您喜欢的外壳。
在“设置/首选项”对话框Ctrl + Alt + S中,转到“工具” |“设置”。 终端并指定您的Shell可执行文件的路径。

17、One can easily convert any Java class to the Kotlin one with the same semantics.
To do that, just choose Code | Convert Java File to Kotlin File on the main menu:
可以轻松地将任何具有相同语义的Java类转换为Kotlin类。
为此,只需选择代码| 在主菜单上将Java File转换为Kotlin File:

18、When using Code Completion, you can accept the current selection in the suggestions list with Ctrl+Shift+Enter.
IntelliJ IDEA Community will not only insert the selected string, but also turn the current code construct into a syntactically correct one (balance parentheses, add missing braces and semicolons, and so on).
使用“代码补全”时,您可以通过Ctrl + Shift + Enter接受建议列表中的当前选择。
IntelliJ IDEA社区不仅将插入选定的字符串,还将当前的代码构造转换为语法正确的构造(平衡括号,添加缺少的花括号和分号,等等)。

19、You can inject SQL into a string literal (Alt+Enter | Inject language or reference | ) and then use coding assistance for SQL.
您可以将SQL注入字符串文字中(Alt + Enter |注入语言或引用| ),然后对SQL使用编码帮助。

20、To search for a code pattern or a grammatical construct, select Edit | Find | Search Structurally.
要搜索代码模式或语法结构,请选择“编辑” |“语法”。 查找| 结构搜索。

21、To evaluate any expression while debugging your program, select the expression in the editor and press Alt+F8
要在调试程序时求值任何表达式,请在编辑器中选择表达式,然后按Alt + F8

22、To switch between opened files and tool windows, use the Switcher Ctrl+Tab . For navigation, press and hold Ctrl (Windows and Linux) or Command (macOS) and use the Up and Down arrow keys or Tab and Shift+Tab, and Alt.
Press Delete or BackSpace to close an editor tab or hide a tool window.
要在打开的文件和工具窗口之间切换,请使用切换器Ctrl + Tab。 要进行导航,请按住Ctrl键(Windows和Linux)或Command键(macOS)并使用向上和向下箭头键或Tab和Shift + Tab和Alt。
按Delete或BackSpace关闭编辑器选项卡或隐藏工具窗口。

23、Press Ctrl+E (View | Recent Files) to view the list of recently opened files.
按Ctrl + E(查看|最近文件)以查看最近打开的文件列表。
You can also bring up the results of the recently performed usage searches. For this, go to Edit | Find | Recent Find Usages or select Recent Find Usages from the context menu of the Find tool window:
您还可以调出最近执行的用法搜索的结果。 为此,请转到“编辑”。 查找| 最近的查找用法或从“查找”工具窗口的上下文菜单中选择“最近的查找用法”:

24、You can copy text from the editor as rich text to paste it into any other editor that recognizes RTF.
Make sure the Copy as rich text by default checkbox is selected on the Editor | General page of the Settings/Preferences dialog Ctrl+Alt+S .
您可以将文本从编辑器复制为富文本格式,然后将其粘贴到任何其他可识别RTF的编辑器中。
确保在“编辑器” |“编辑器”中选中了“默认复制为富文本格式”复选框 “设置/首选项”对话框的“常规”页面Ctrl + Alt + S。

Math.random()随机整数错误用法

public class Ans3_4_page93{
    public static void main(String[] args){
        //错误用法 导致month的值等于0,永不等于12,可以把12改成13
        //可用于抽奖排除中奖号码
        int month = (int)(Math.random() * 12);

        switch (month) {
            case 1:
                System.out.println("January");
                break;
            case 2:
                System.out.println("February");
                break;
            case 3:
                System.out.println("March");
                break;
            case 4:
                System.out.println("April");
                break;
            case 5:
                System.out.println("May");
                break;
            case 6:
                System.out.println("June");
                break;
            case 7:
                System.out.println("July");
                break;
            case 8:
                System.out.println("August");
                break;
            case 9:
                System.out.println("September");
                break;
            case 10:
                System.out.println("October");
                break;
            case 11:
                System.out.println("November");
                break;
            case 12:
                System.out.println("December");
                break;
        }
    }
}

public class Demo {
    public static void main(String[] args) {
        while (true) {
            double number0 = Math.random();
            double number1 = number0 * 12;
            int number = (int) (number1);
            System.out.println("原始数      " + number0);
            System.out.println("原始数乘以12 " + number1);
            System.out.println(number);
            System.out.println("---------------------");
        }
    }
    }

原始数      3.111564749883611E-4
原始数乘以12 0.003733877699860333
0
---------------------
原始数      0.07859998705330462
原始数乘以12 0.9431998446396554
0
---------------------

Process finished with exit code -1

WordPress 编辑主题文件时报错“未能与站点联系来检查致命错误”

进入WordPress后台编辑文件修改某一段代码,“更新文件”的时候报错:

未能与站点联系来检查致命错误,因此PHP修改已被回滚。您需要采用其他方式(如SFTP)上传您修改的PHP文件。

解决方法:禁用 WP Super Cache 插件,修改完再启用。
如果没有解决,也可能是其他插件的兼容性问题,建议依次禁用插件,经过测试 WordPress 5.3.2版本禁用所有插件依然不行,有时候出现这个问题和主题有关系,可以尝试换到其它主题,再修改PHP文件。

#吴尊老婆曾独自拍婚纱照#

现在国内年轻演员也经常被扒恋情,一大批粉丝失望脱粉闹别扭上热搜等等,演员和爱豆又或者偶像,本质上的差别其实不是那么大,爱豆可以转型做歌手做演员,演员也可以唱歌跳舞全面发展,就算一开始就是卖的单身人设,也应该实事求是,反之对另一半和粉丝都是一种伤害。
吴尊隐婚卖单身人设骗人,加上让老婆刨腹产凑生日出生,又拉着全家人上真人秀,热搜不断,本来吴尊之前的好爸爸好男人人设是挺成功的,我几乎觉得大家原谅他骗粉丝隐婚了,结果又上一次真人秀节目圈钱,但这次彻底傻了。

Java移位运算符>>和无符号右移运算符>>>的区别

Java 移位运算符是一个二元运算符,用来将一个二进制数中的每一位全部都向一个方向移动指定位,溢出的部分将被舍弃,而空缺的部分填入一定的值。
左移使用两个小于符号”<<“表示,右移使用两个大于符号”>>”表示。
应用逻辑移位时,移位后空缺的部分全部填0。原码为整数,补码为负数,补码=反码+1。

无符号右移指的是,右移后前面补零,如:
-1的补码为11111111111111111111111111111111,>>>2=0011111111111111111111111111111111。

最高位 1表示负数,0表示整数。

>> 符号右移时,对于正数,高位补0,对于负数,高位补1。
>>>无符号右移,长度扩展为4字节,最高位都为0,但正数扩展位补0,负数扩展位补1。

   0001(十进制1)
<<    3(左移3位)
 = 1000(十进制8)

    1010(十进制10)
>>    2(右移2位)
 = 0010(十进制2)

 1010           原始数         number
10100           左移一位       number = number << 1;
 1010           右移一位       number = number >> 1;