免责声明:这是一个功课问题.我正在尝试编写一个contains(java.lang.String subString)方法,该方法返回一个int表示主字符串中比较字符串索引的值,用于定制的String类.
一些规则:
length()返回主字符串的长度(这正是它的作用)我的代码:
public int contains(java.lang.String subString) {
this.subString = subString;
char[] arrSubStr = this.subString.toCharArray();
//Create initial fail
int index = -1;
//Make sure comparison subString is the same length or shorter than the primary string
if(arrSubStr.length > length()) {
return index;
}
//Steps to perform if initial conditions are met
else {
//Compare first character of subString to each character in primary string
for(int i = 0; i …Run Code Online (Sandbox Code Playgroud) 我有一个锁屏脚本(通过 i3lock),它可以在终端窗口中正确运行,但在 i3 配置中用作键绑定时却不能。
脚本非常简单:
# Take screenshot
scrot /tmp/screenshot.png
# Pixelate and add central logo
magick /tmp/screenshot.png -scale 10% -scale 1000% -gravity center /home/user/bin/archlogo.png -composite /tmp/lock.png
# Lock using pixelated image & logo
i3lock -e -f -c 000000 -i /tmp/lock.png
Run Code Online (Sandbox Code Playgroud)
脚本被保存~/bin/lock并可执行。该目录也已在我的~/.bash_profile.
没有错误信息,只有终端光标的短暂闪烁。
中的相关行~/.config/i3/config:
# lock screen
bindsym $mod+l exec lock
Run Code Online (Sandbox Code Playgroud)
(删除了 $mod+l 的默认键绑定并重新启动了 i3)。
我正在尝试读取两个文本文件,然后搜索每个文本文件中最常长度为5的字符串.
我写的代码:
db = open("list_of_2","r").read()
lp = open("lastpass","r").read()
word = ''
length = 0
for dbchar in db:
for lpchar in lp:
if dbchar == lpchar:
word += str(dbchar)
length += 1
else:
length = 0
word = ''
if length > 4:
print(word)
Run Code Online (Sandbox Code Playgroud)
代码当前一遍又一遍地打印像'-----'和'55555'这样的字符串,似乎没有打破循环(这些特殊的字符串只出现lp一次).我也不相信这是找到字符串,不一样的角色重复.
如何将代码更改为:
编辑:这是一些模拟文本文件.在这些中,字符串'ghtyty'在file1中出现三次,在file2中出现4次.代码应该打印'ghtyty'一次到控制台.