import java.util.*;
import java.io.*;
public class extraCredit{
public static void main(String args[])
throws FileNotFoundException{
Scanner sc = new Scanner(new File("decrypt.txt"));
while(sc.hasNextLine()){
String line = sc.nextLine();
int tempCount = 0;
int maxCount = 0;
char maxLetter = 'a';
String alphabet = "aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ";
for(int i = 0; i < alphabet.length() - 1 ; i++){
char lLetter = alphabet.charAt(i);
i++;
char uLetter = alphabet.charAt(i);
for(int a = 0; a < line.length(); a++){
if(line.charAt(a) == lLetter | line.charAt(a) == uLetter){
tempCount ++;
}
}
if(tempCount > maxCount){
maxCount = tempCount;
maxLetter = lLetter;
}
}
System.out.println(maxLetter + maxCount);
}
}
}
这是我的额外信用分配代码。该计划的目标是说出最常出现的字母。我从decrypt.txt中的唯一一行创建一个字符串。我的逻辑是进行临时计数以跟踪第一个字母然后转到b,如果它具有更高的临时计数,则它现在是最大计数。我将所有字母大写和小写字母放在一起,这样我就可以使用.charAt(i)创建小写字母和大写字母的lLetter和uLetter。最后它应该输出最高计数。这个java文件编译但它不输出任何东西。任何人都可以帮我弄清楚出了什么问题。我觉得这个逻辑很合理。谢谢。
Kyviv zj r sreb kyrk tivuzkj pfli rttflek vrty dfiezex nzky£86,400。 Zk triizvj fmvi ef srcretv wifd urp kf urp。 Vmvip vmvezex uvcvkvj nyrkvmvi grik fw kyv srcretv pfl wrzcvu kf ljv ulizex kyv urp。 Nyrk nflcu pfl uf? Uirn flk RCC FW ZK,fw tflijv !!!! Vrty fw lj yrj jlty r sreb。 Zkj erdv zj KZDV。 Vmvip dfiezex,zk tivuzkj pfl nzky 86,400 jvtfeuj。 Vmvip ezxyk zk nizkvj fww,rj cfjk,nyrkvmvi fw kyzj pfl yrmv wrzcvu kf zemvjk kf xffu gligfjv。 Zk triizvj fmvi ef srcretv。 Zk rccfnj ef fmviuirwk。 Vrty urp zk fgvej r evn rttflek wfi pfl。 Vrty ezxyk zk sliej kyv ivdrzej fw kyv urp。 Zw pfl wrzc kf ljv kyv urp'j uvgfjzkj,kyv cfjj zj pflij。 Kyviv zj ef xfzex srtb。 Kyviv zj ef uirnzex rxrzejk kyv“kfdfiifn。”Pfl dljk czmv ze kyv givjvek fe kfurp'j uvgfjzkj。 Zemvjk zk jf rj kf xvk wifd zk kyv lkdfjk ze yvrcky,yrggzevjj,reu jlttvjj! Kyv tcftb zj ileezex。 Drbv kyv dfjk fw kfurp。
这是文本,但它全部在txt文件中的一行。
如果可能的话,你可以尝试帮我弄清楚为什么我制造的那个不起作用而不是写出新的程序。我见过的那些包含了许多我们尚未学到的工具。三江源。
import java.util.*;
import java.io.*;
public class extraCredit2{
public static void main(String args[])
throws FileNotFoundException{
//Scanner sc = new Scanner(new File("decrypt.txt"));
//while (sc.hasNextLine()) {
//String line = sc.nextLine();
String line = "ashdbskjdbfkjsnfdkfjssjaandkjbsdkfjnsndkfkjnfwjneijfnweifjwbshdbfjhbaskjdnaskdjbdf";
int tempCount = 0;
int maxCount = 0;
String maxLetter = "a";
String alphabet = "aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ";
for (int i = 0; i < alphabet.length() - 1; i++) {
tempCount = 0;
String lLetter = alphabet.charAt(i) + "";
i++;
String uLetter = alphabet.charAt(i) + "";
for (int a = 0; a < line.length(); a++) {
if ((line.charAt(a) + "").equals(lLetter) | (line.charAt(a) + "").equals(uLetter)) {
tempCount++;
}
}
if (tempCount > maxCount) {
maxCount = tempCount;
maxLetter = lLetter;
}
}
System.out.println(maxLetter + maxCount);
//}
}
}
这个新代码可以解决,但我注释掉了.txt文件部分,只是创建了一个不同的字符串。谢谢大家。
答案 0 :(得分:0)
看看是否有效
public class extraCredit{
public static void main(String args[])
throws FileNotFoundException{
Map<String,Integer> letCounts = new HashMap<String,Integer>();
Scanner sc = new Scanner(new File("decrypt.txt"));
while(sc.hasNextLine()){
String line = sc.nextLine();
for(String s : line.split(""))
if(letCounts.containsKey(s))
letCounts.put(s,letCounts.get(s)+1);
else
letCounts.put(s,1);
}
int maxCount=0;
for(Map.Entry<String, Integer> e : letCounts.entrySet())
{
maxCount=Math.max(maxCount, e.getValue());
}
for(Map.Entry<String, Integer> e : letCounts.entrySet())
{
if(e.getValue()==maxCount)
System.out.println(e.getKey()+e.getValue());
}
}
}
更新了循环
while (sc.hasNextLine()) {
String line = sc.nextLine();
int tempCount = 0;
int maxCount = 0;
String maxLetter = "a";
String alphabet = "aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ";
for (int i = 0; i < alphabet.length() ; i++) {
tempCount = 0;
String lLetter = alphabet.charAt(i) + "";
for (int a = 0; a < line.length(); a++) {
if ((line.charAt(a) + "").equals(lLetter)) {
tempCount++;
}
}
if (tempCount > maxCount) {
maxCount = tempCount;
maxLetter = lLetter;
}
}
System.out.println(maxLetter + maxCount);
}
这给出了
v72
Kyviv zj r sreb kyrk tivuzkj pfli rttflek vrty dfiezex nzky £86,400. Zk triizvj fmvi ef srcretv wifd urp kf urp. Vmvip vmvezex uvcvkvj nyrkvmvi grik fw kyv srcretv pfl wrzcvu kf ljv ulizex kyv urp. Nyrk nflcu pfl uf? Uirn flk RCC FW ZK, fw tflijv!!!! Vrty fw lj yrj jlty r sreb. Zkj erdv zj KZDV. Vmvip dfiezex, zk tivuzkj pfl nzky 86,400 jvtfeuj. Vmvip ezxyk zk nizkvj fww, rj cfjk, nyrkvmvi fw kyzj pfl yrmv wrzcvu kf zemvjk kf xffu gligfjv. Zk triizvj fmvi ef srcretv. Zk rccfnj ef fmviuirwk. Vrty urp zk fgvej r evn rttflek wfi pfl. Vrty ezxyk zk sliej kyv ivdrzej fw kyv urp. Zw pfl wrzc kf ljv kyv urp’j uvgfjzkj, kyv cfjj zj pflij. Kyviv zj ef xfzex srtb. Kyviv zj ef uirnzex rxrzejk kyv “kfdfiifn.” Pfl dljk czmv ze kyv givjvek fe kfurp’j uvgfjzkj. Zemvjk zk jf rj kf xvk wifd zk kyv lkdfjk ze yvrcky, yrggzevjj, reu jlttvjj! Kyv tcftb zj ileezex. Drbv kyv dfjk fw kfurp.
答案 1 :(得分:0)
public class ExtraCredit {
public static void main(String args[]) throws FileNotFoundException {
String line = "this is a test";
String[] splitLine = line.split("");
HashMap<String,Integer> lettersMap = new HashMap<String, Integer>();
// Load HashMap with letters and the number of appearances
for (int i = 0; i < splitLine.length; i++) {
String letter = splitLine[i];
if (!letter.equals("")) {
if (lettersMap.containsKey(letter)) {
int currentValue = lettersMap.get(letter);
lettersMap.put(letter, ++currentValue);
} else {
lettersMap.put(letter, 1);
}
}
}
Iterator<Entry<String, Integer>> mapIterator = lettersMap.entrySet().iterator();
int highestValue = 0;
String highestLetter = new String();
while (mapIterator.hasNext()) {
Entry<String, Integer> pair = mapIterator.next();
if (pair.getValue() > highestValue) {
highestValue = pair.getValue();
highestLetter = pair.getKey();
}
mapIterator.remove();
}
System.out.println("Letter: "+highestLetter+" "+"Times: "+highestValue);
}
}
这个应该有效!