- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试编写一个程序,该程序接受两个单词或短语,并通过查看它们的 unicode 值是否匹配来测试它们是否是字谜词。 “搜索”方法仅在它们长度相同时才运行。我遇到了问题,但现在已解决。
这是修改后的版本:
我想知道您对代码布局的风格有何看法。清楚吗?我应该采取不同的做法吗?或者你觉得它很容易阅读?您对我如何使其他人更清楚有什么建议吗?
如果我添加注释,它们应该很短还是应该在多行注释中解释该部分如何工作?
我想让它看起来尽可能简单,但我对此几乎没有得到真正的建议。所以如果有人有任何想法......
import java.util.Scanner;
public class AnagramCount {
public static void main(String[] args) {
System.out.println("Please enter two words, one per line, to test if it is an anagram");
Scanner userInput = new Scanner(System.in);
String word1 = userInput.nextLine();
String word2 = userInput.nextLine();
int count = 0;
int[] char_code = new int[word1.length()];
int[] char_code2 = new int[word2.length()];
char[] temp = word2.toCharArray();
boolean match = true;
if (word1.length() == word2.length()){
search(word1, word2, count, char_code, char_code2, match, temp);
if (match == true){
if (char_code[word1.length()-1] == 0){
match = false;
}
else {
// if match remains true after this final check, information about it will print
System.out.print("word1 unicode values: ");
for(int i = 0; i < word1.length(); i++){
System.out.print(char_code[i] + " ");
}
System.out.println();
System.out.print("word2 unicode values: ");
for(int i = 0; i < word1.length(); i++){
System.out.print(char_code2[i] + " ");
}
}
}
}
else {
match = false;
}
System.out.println("\n" + "Anagram? t/f?: " + match);
}
public static void search(String word1, String word2, int count, int[] char_code, int[] char_code2, boolean match, char[] temp)
{
StringBuilder word1check = new StringBuilder(word1);
StringBuilder word2check = new StringBuilder(word2);
int word1_unicode = 0;
int word2_unicode = 0;
if(count >= word1.length())
return;
else
{
for(int i = 0; i < word2.length(); i++){
if (word1.charAt(count) == word2.charAt(i)){
word1_unicode = word1check.codePointAt(count);
char_code[count] = word1_unicode;
temp[i] = 0;
String str = new String(temp);
word2 = str;
word2_unicode = word2check.codePointAt(i);
char_code2[count] = word2_unicode;
if(count==word1.length()-1)
break;
search(word1, word2, ++count, char_code, char_code2, match, temp);
}
}
}
return;
}
}
最佳答案
您的问题发生在您正在使用的递归中,而不是您认为的地方。在调用“search”方法之前,您需要增加计数变量。
search(word1, word2, ++count, char_code, char_code2, match);
解决此问题的最简单方法是在调用自身方法之前添加检查
if(count==word1.length()-1)
break;
search(word1, word2, ++count, char_code, char_code2, match);
这样,如果计数已到达单词末尾,您就不会调用搜索方法,并且它永远不会通过越界来打破它。
我在测试过程中发现的另一个问题在这里
for(int i = 0; i < word1.length()-1; i++)
这样,当您搜索时,您将永远不会到达第二个单词的末尾,并且如果您的第二个单词以在单词中仅使用一次的字符结尾,则它永远不会进入此处
if (word1.charAt(count) == word2.charAt(i)){
// I think the problem is right around here, but I don't know what to change
word1_unicode = word1check.codePointAt(count);
char_code[count] = word1_unicode;
if(char_code2[count] == 0) { //prevents double counting of letters
word2_unicode = word2check.codePointAt(i);
char_code2[count] = word2_unicode;
search(word1, word2, ++count, char_code, char_code2, match);
}
if((count==0)&&(i == word1.length()-1)){
match = false;
}
}
为了解决这个问题,我只是删除了“for”循环中的“-1”。
此方法中的最后一个奇怪的问题是,它实际上返回一个您在程序开始时预定义的 boolean 类型,但它实际上从未达到返回 false 的程度。此时我发现你的重复计算预防也不起作用。基本上大约一半的线路并没有真正做任何事情。
希望我能帮助您解决我在程序中发现的问题。此时,如果第一个单词中的所有字母都可以在第二个单词中找到,则会说 2 个单词是字谜词。我能想到的防止重复计算单词的最简单方法就是简单地覆盖此位置的字母。
下面的代码实际上与我的防止重复计算字母的想法配合得很好:
import java.util.Scanner;
public class test {
public static void main(String[] args) {
System.out.println("Please enter two words, one per line, to test if it is an anagram");
Scanner userInput = new Scanner(System.in);
String word1 = userInput.nextLine();
String word2 = userInput.nextLine();
int count = 0;
boolean match = true;
if (word1.length() == word2.length()){
match = search(word1, word2, count,match);
}
else {
match = false;
}
if(match)
System.out.println("The words are anagrams");
else
System.out.println("The words are not anagrams");
}
public static boolean search(String word1, String word2, int count,boolean match)
{
if(count >= word1.length()-1)
return match;
else
{
for(int i = 0; i < word1.length(); i++)
{
if (word1.charAt(count) == word2.charAt(i)){
char[] temp = word2.toCharArray();
temp[i] = 0;
word2 = temp.toString();
search(word1, word2, ++count, match);
}
else
match = false;
}
}
return match;
}
}
关于java - 样式(递归和越界异常已得到解答,这是新的),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13433872/
我遇到了一个奇怪的问题。我的应用程序的 Release 版本似乎运行良好,但最近当我切换到 Debug 版本时,我在启动时立即遇到访问冲突。当释放分配的内存块时,就会发生访问冲突。所有这些都发生在静态
我在 C# 中偶然发现了这种奇怪的语法形式,并试图弄清楚它的含义以及如何使用它。网络上似乎没有关于此的任何文档。 object data = new { var1 = someValue, var2
我正在尝试使用浏览器的内置类型 CSSStyleDeclaration 以编程方式传递和修改样式(由于 .cssText 属性,这很方便)。 但是,new CSSStyleDeclaration()
我有现成的代码: internal bool firstAsSymbol(out Symbol s) { return (s = first as Symbol) !=
在新的 Eclipse 版本 2022-03 中,一些(但不是全部)java 项目在 Project Explorer View 中的外观发生了变化。尽管 Package Presentation 设
我正在尝试使用 FormData 通过获取 API 在 POST 请求中发送用户输入的数据。问题是,当我用我创建的表单创建一个新的 FormData 对象时,它一直在创建一个空对象——没有条目/键/值
我有一个用一些 intel-intrinsincs 编写的 C 代码。在我先用 avx 然后用 ssse3 标志编译后,我得到了两个完全不同的汇编代码。例如: AVX: vpunpckhbw %xm
最近,discord 为您自己的应用程序添加了对斜杠命令的支持。我通读了它的文档,并尝试搜索一些视频(但是该功能刚刚出现),但我不明白我实际上需要做什么才能使其正常工作。我正在使用 WebStorm(
我想使用 JRI 从 Java 调用 R。 我在 eclipse 下在主类中运行它: Rengine c = new Rengine(new String[] { "--vanilla" },
我正在使用新的 Place Autocomplete那是来自新的静态Google Places SDK 客户端库 (here)。所以它真的很容易使用,我刚得到this tutorial它按预期工作。
我刚刚更新到 flutter 版本 1.25.0-5.0.pre.92,我的代码中出现了很多与空安全相关的错误,这些错误以前运行良好。我没有以任何方式选择空安全,我所做的只是运行 flutter 升级
我已经使用 React Native 有一段时间了,但我想我会在网络上试用 React。所以我遵循了这个指南:https://reactjs.org/docs/create-a-new-react-a
周六早上在这里。尝试学习新的 Scala 编译器 dotty。 安装在我的 Mac 上使用 brew install lampepfl/brew/dotty 安装成功。我有版本 dotr -versi
我使用了谷歌地方的新依赖。单击自动完成 View 时应用程序崩溃。错误如下。, java.lang.NullPointerException: Place Fields must be set.
我关注了这个博客-> https://medium.com/@teyou21/training-your-object-detection-model-on-tensorflow-part-2-e9e
在哪里可以找到用于在此架构上进行组装的新寄存器的名称? 我指的是 X86 中的寄存器,如 EAX、ESP、EBX 等。但我希望它们是 64 位的。 我认为它们与我反汇编 C 代码时不同,我得到的是 r
新的服务总线库 Azure.Messaging.ServiceBus 使用 ServiceBusReceivedMessage 来接收消息 https://learn.microsoft.com/en
需要使用实时流媒体 channel 的实时编码类型在新的 Azure 门户中配置广告插入和石板图像。请帮忙解决这个问题,因为我找不到该功能。 最佳答案 此处描述了 Azure 媒体服务的广告插入选项
我正在使用新的 GitHub 操作,下面的工作流程的想法是在打开或同步 pr 时运行,它应该首先检查并安装依赖项,然后运行一些 yarn 脚本 name: PR to Master on: pul
我听说 DMD 2.058 中将有一个用于匿名函数的新语法,但我找不到任何相关信息。新语法是什么?旧语法是否会被弃用? 最佳答案 我相信它就像 C#'s . 以下内容是等效的: delegate(i,
我是一名优秀的程序员,十分优秀!