*6.23(Occurrences of a specified character)Write a method that finds the number of occurrences of a specified character in a string using the following header:
public static int count(String str, char a)
For example, count(“Welcome”, ‘e’) returns 2. Write a test program that prompts the user to enter a string followed by a character then displays the number of occurrences of the character in the string.
下面是参考答案代码:
import java.util.Scanner;
public class Ans6_23_page202 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a string and a character: ");
String aStr = input.next();
char aChar = input.next().charAt(0);
System.out.println(count(aStr,aChar));
}
public static int count(String str, char a) {
int count = 0;
for (int i = 0; i < str.length(); i ++) {
if (str.charAt(i) == a)
count++;
}
return count;
}
}
**6.22(Math: approximate the square root) There are several techniques for implementing the sqrt method in the Math class. One such technique is known as the Babylonian method. It approximates the square root of a number, n, by repeatedly performing the calculation using the following formula:
nextGuess = (lastGuess + n / lastGuess) / 2
When nextGuess and lastGuess are almost identical, nextGuess is the approximated square root. The initial guess can be any positive value (e.g., 1). This value will be the starting value for lastGuess. If the difference between nextGuess and lastGuess is less than a very small number, such as 0.0001, you can claim that nextGuess is the approximated square root of n. If not, nextGuess becomes lastGuess and the approximation process continues. Implement the following method that returns the square root of n:
public static double sqrt(long n)
下面是参考答案代码:
public class Ans6_22_page202 {
public static void main(String[] args) {
System.out.println(sqrt(2));
}
public static double sqrt(long n) {
double Guess;
double nextGuess = 1;
do {
Guess = nextGuess;
nextGuess = (Guess + n / Guess) / 2;
} while (Guess - nextGuess >= 0.0001 || nextGuess - Guess >= 0.0001);
return nextGuess;
}
}
*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;
}
}
*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;
}
}
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。
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
current_users = ['admin','user1','user2','user3','user4']
new_users = ['aDmin','user1','user5','user6','user7']
for user in new_users:
if user in current_users or user.lower() in current_users or user.title() in current_users or user.capitalize() in current_users or user.upper() in current_users:
print('用户名' + user + '已被注册,请尝试其他用户名!')
else:
print('恭喜,没有人使用' + user + ',您可以注册!')
或者(代码最少)
current_users = ['admin','user1','user2','user3','user4']
new_users = ['aDmin','user1','user5','user6','user7']
for user in new_users:
if user.lower() in [current_user.lower() for current_user in current_users]:
print('用户名' + user + '已被注册,请尝试其他用户名!')
else:
print('恭喜,没有人使用' + user + ',您可以注册!')