- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在处理凯撒密码问题,到目前为止我的所有测试用例都有效,但是在处理频率列表时出现了我的问题。任何帮助将不胜感激!
public class CaesarCipher
{
//Percentage frequencies for alphabet
static double[] table = {8.2, 1.5, 2.8, 4.3, 12.7, 2.2, 2.0, 6.1, 7.0, 0.2, 0.8, 4.0, 2.4, 6.7,
7.5, 1.9, 0.1, 6.0, 6.3, 9.1, 2.8, 1.0, 2.4, 0.2, 2.0, 0.1};
//convert letter to number
static int let2nat(char c)
{
return ((int) c) - 97;
}
//convert number to letter
static char nat2let(int code)
{
return (char) (code + 97);
}
//shift a letter to another letter
static char shift(int shftAmt, char c)
{
if (let2nat(c) < 0 || let2nat(c) > ((int) ('z' - 'a') + 1) - 1)
{
return c;
}
else
{
// do a safe shift
int result = (let2nat(c) + shftAmt) % ((int) ('z' - 'a') + 1);
result += ((int) ('z' - 'a') + 1);
result %= ((int) ('z' - 'a') + 1);
return nat2let(result);
}
}
//encodes a string using the given shift amount
static String encode(int shftAmt, String str)
{
char[] encodedStr = new char[str.length()];
for(int i = 0; i < str.length(); i++)
{
encodedStr[i] = shift(shftAmt, str.charAt(i));
}
return new String(encodedStr);
}
//performs the inverse method to encode
static String decode(int shftAmt, String str)
{
char[] decodedStr = new char[str.length()];
for(int i = 0; i < str.length(); i++)
{
decodedStr[i] = shift(0 - shftAmt, str.charAt(i));
}
return new String(decodedStr);
}
//Calculates the amount of lowercase letters
static int lowers(String str)
{
int count = 0;
for(int i = 0; i < str.length(); i++)
{
if(let2nat(str.charAt(i)) >= 0 && let2nat(str.charAt(i)) <= 25)
count++;
}
return count;
}
//Calculates the number of times a character appears in a string
static int count(char c, String str)
{
int counter = 0;
for(int i = 0; i < str.length(); i++)
{
if(c == str.charAt(i))
counter++;
}
return counter;
}
//Calculates the percentage of one integer to another
static double percent(int num1, int num2)
{
return ((float) num1/num2 * 100);
}
//Returns the list of percentage frequencies
static double[] freqs(String str)
{
double[] count = new double[26];
for(int i = 0; i < str.length(); i++)
if(let2nat(str.charAt(i)) >= 0 && let2nat(str.charAt(i)) <= 25)
count[let2nat(str.charAt(i))]++;
for(int i = 0; i < 26; i++)
count[i] = percent((int)count[i], lowers(str));
return count;
}
}
上面是我到目前为止的代码,直到弄清楚频率..在执行以下操作时,我当前的输出是[D@2a139a55
System.out.println(freqs("haskellisfun "));
当以下方法的输出应为:
that returns the list of percentage frequencies of each of the lower-case letters
’a’ to ’z’ in a string of characters. For example:
freqs("haskellisfun") ->
{8.33333,0.0,0.0,0.0,8.33333,8.33333,0.0,8.33333,
8.33333,0.0,8.33333,16.6667,0.0,8.33333,0.0,0.0,
0.0,0.0,16.6667,0.0,8.33333,0.0,0.0,0.0,0.0,0.0}
任何帮助将不胜感激,只是找不到我的错误。
最佳答案
打印的是数组的类型和哈希码。 ([D@2a139a55
表示“带有哈希码 2a139a55
的double
数组”)
您可以使用 Arrays.toString()
打印数组像这样:
import java.util.Arrays;
System.out.println(Arrays.toString(freqs("haskellisfun")));
关于java - 凯撒密码(频率列表),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33458698/
我刚刚开始学习 C 语言类(class),并且遇到了命令行参数的问题。分配是这样的(还有更多,但这是开头有关命令行参数的部分): - 你的程序必须接受一个命令行参数,一个非负整数。 - 如果您的程序在
我需要检查命令行参数中是否有非数字字符。例如: ./problem 20x 应该打印出“不是数字”,因为它包含一个 x。我的代码似乎没有循环遍历命令行参数中的所有字符。 我基本上尝试了不同类型的循环,
这里我有从标准输入将字符流输入到数组中的代码。然后将该数组转换为二维数组。然后,它将该数组从行列顺序更改为列行顺序。然后它打印出创建凯撒移位加密的新数组。我遇到的问题是我的数组开始使用第二个用户输入的
我有点被这个问题困住了。当我运行程序时,由于某种原因,循环经过 z 的所有字母都不会打印。问题来自这个链接:http://docs.cs50.net/2016/x/ap/problems/caesar
我是一名优秀的程序员,十分优秀!