我需要我的程序遍历while循环并向用户询问无限数量的字符串......直到他们输入“Done”。如果他们内嵌的字符串不是“完成”,那么它会计算整个字符串的哈希码值并将其排序到正确的层中。一旦用户输入完成,程序将打印每层中的标签以及哪一层是最大的。
我的问题:程序要求用户输入一个字符串,计算每个字母的哈希值,将这些值排序到一个层中,打印标签,打印哪个等级最大。然后询问用户另一个字符串并继续此过程 - > 程序永远不会终止。
FYI:我从while循环中取出了打印语句(在for循环之后),我得到一个编译错误:“找不到符号”指向我的所有“层”变量。我认为这意味着我需要声明它们在while循环的外部,但是如果我这样做,那么同样的问题就会发生,因为它们没有在内部声明 while循环。 / p>
我需要程序做什么:向用户询问字符串,计算哈希码,在适当的位置计算(进入适当的层)。继续此循环,直到用户输入“完成”,然后程序打印每层中的计算值,最后打印哪一层是最大的。 输入字符串:Foo **此外,我知道有一种更简单的方法来确定哪一层是最大的(第64-109行),但我无法弄清楚。我们没有在课堂上谈论过数组或类似内容,所以我不确定如何使用if / else语句来简化所有这些。** 示例输出:
输入字符串:完成
大于或等于2,000,000,000:0
1,500,000,000至1,999,999,999:0
1,000,000,000至1,499,999,999:0
500,000,000至999,999,999:0
小于或等于499,999,999:1
最大存储桶小于或等于499,999,999,大小为1 import java.util.Scanner;
public class StringHashCodeLoop {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
String strEntered;
// Prompt user for a string, store string in "strEntered."
System.out.println("Enter a String:");
strEntered = sc.next();
// Perform a while loop as long as the entered string does not equal "Done."
while (!strEntered.equals("Done")) {
// Convert all letters in the string to lower case.
strEntered = strEntered.toLowerCase();
// Declare the integer "length" and set it equal to the length of the entered string, to be used in the for loop.
int length = strEntered.length();
// Initialize each tier/bucket value to 0 before performing calculations.
int tier1,tier2,tier3,tier4,tier5;
tier1 = 0;
tier2 = 0;
tier3 = 0;
tier4 = 0;
tier5 = 0;
for(int i = 0; i < length; ++i) {
int hashValue = 0;
hashValue += strEntered.charAt(i) * Math.pow(31, length - 1 - i);
// Sort the Hash Code for the entered string into its corresponding bucket.
if (hashValue >= 2000000000) {
tier1++;
}
else if ((1500000000 < hashValue) && (hashValue < 1999999999)) {
tier2++;
}
else if ((1000000000 < hashValue) && (hashValue < 1499999999)) {
tier3++;
}
else if ((500000000 < hashValue) && (hashValue < 999999999)) {
tier4++;
}
else {
tier5++;
}
} // end of for loop
System.out.println("Enter a String:");
strEntered = sc.next();
} // end of while loop
// Print the tallied totals of each bucket.
System.out.println("Greater than 2,000,000,000: " + tier1);
System.out.println("1,500,000,000 to 1,999,999,999: " + tier2);
System.out.println("1,000,000,000 to 1,499,999,999: " + tier3);
System.out.println("500,000,000 to 999,999,999: " + tier4);
System.out.println("Less than or equal to 499,999,999: " + tier5);
// Print which is the largest bucket and the total of its contents.
if (tier1 > tier2 && tier1 > tier3 && tier1 > tier4 && tier1 > tier5) {
System.out.println("The largest bucket is Greater than 2,000,000,000 with a size of " + tier1);
}
else if (tier2 > tier1 && tier2 > tier3 && tier2 > tier4 && tier2 > tier5) {
System.out.println("The largest bucket is 1,500,000,000 to 1,999,999,999 with a size of " + tier2);
}
else if (tier3 > tier1 && tier3 > tier2 && tier3 > tier4 && tier3 > tier5) {
System.out.println("The largest bucket is 1,000,000,000 to 1,499,999,999 with a size of " + tier3);
}
else if (tier4 > tier1 && tier4 > tier2 && tier4 > tier3 && tier4 > tier5) {
System.out.println("The largest bucket is 500,000,000 to 999,999,999 with a size of " + tier4);
}
else if (tier5 > tier1 && tier5 > tier2 && tier5 > tier3 && tier5 > tier4) {
System.out.println("The largest bucket is Less than or equal to 499,999,999 with a size of " + tier5);
}
else if (tier1 == tier2) {
System.out.println("The largest bucket is Greater than 2,000,000,000 with a size of " + tier1);
}
else if (tier1 == tier3) {
System.out.println("The largest bucket is Greater than 2,000,000,000 with a size of " + tier1);
}
else if (tier1 == tier4) {
System.out.println("The largest bucket is Greater than 2,000,000,000 with a size of " + tier1);
}
else if (tier1 == tier5) {
System.out.println("The largest bucket is Greater than 2,000,000,000 with a size of " + tier1);
}
else if (tier2 == tier3) {
System.out.println("The largest bucket is Greater than 2,000,000,000 with a size of " + tier2);
}
else if (tier2 == tier4) {
System.out.println("The largest bucket is Greater than 2,000,000,000 with a size of " + tier2);
}
else if (tier2 == tier5) {
System.out.println("The largest bucket is Greater than 2,000,000,000 with a size of " + tier2);
}
else if (tier3 == tier4) {
System.out.println("The largest bucket is Greater than 2,000,000,000 with a size of " + tier3);
}
else if (tier3 == tier5) {
System.out.println("The largest bucket is Greater than 2,000,000,000 with a size of " + tier3);
}
else if (tier4 == tier5) {
System.out.println("The largest bucket is Greater than 2,000,000,000 with a size of " + tier4);
}
}
}
答案 0 :(得分:0)
这应该做的工作:
import java.util.Scanner;
public class StringHashCodeLoop {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
String strEntered;
// Prompt user for a string, store string in "strEntered."
System.out.println("Enter a String:");
strEntered = sc.next();
// Perform a while loop as long as the entered string does not equal "Done."
while (!strEntered.equals("Done")) {
// Convert all letters in the string to lower case.
strEntered = strEntered.toLowerCase();
// Declare the integer "length" and set it equal to the length of the entered string, to be used in the for loop.
int length = strEntered.length();
// Initialize each tier/bucket value to 0 before performing calculations.
int tier1,tier2,tier3,tier4,tier5;
tier1 = 0;
tier2 = 0;
tier3 = 0;
tier4 = 0;
tier5 = 0;
for(int i = 0; i < length; ++i) {
int hashValue = 0;
hashValue += strEntered.charAt(i) * Math.pow(31, length - 1 - i);
// Sort the Hash Code for the entered string into its corresponding bucket.
if (hashValue >= 2000000000) {
tier1++;
}
else if ((1500000000 < hashValue) && (hashValue < 1999999999)) {
tier2++;
}
else if ((1000000000 < hashValue) && (hashValue < 1499999999)) {
tier3++;
}
else if ((500000000 < hashValue) && (hashValue < 999999999)) {
tier4++;
}
else {
tier5++;
}
} // end of for loop
System.out.println("Enter a String:");
System.out.println("Greater than 2,000,000,000: " + tier1);
System.out.println("1,500,000,000 to 1,999,999,999: " + tier2);
System.out.println("1,000,000,000 to 1,499,999,999: " + tier3);
System.out.println("500,000,000 to 999,999,999: " + tier4);
System.out.println("Less than or equal to 499,999,999: " + tier5);
// Print which is the largest bucket and the total of its contents.
if (tier1 > tier2 && tier1 > tier3 && tier1 > tier4 && tier1 > tier5) {
System.out.println("The largest bucket is Greater than 2,000,000,000 with a size of " + tier1);
}
else if (tier2 > tier1 && tier2 > tier3 && tier2 > tier4 && tier2 > tier5) {
System.out.println("The largest bucket is 1,500,000,000 to 1,999,999,999 with a size of " + tier2);
}
else if (tier3 > tier1 && tier3 > tier2 && tier3 > tier4 && tier3 > tier5) {
System.out.println("The largest bucket is 1,000,000,000 to 1,499,999,999 with a size of " + tier3);
}
else if (tier4 > tier1 && tier4 > tier2 && tier4 > tier3 && tier4 > tier5) {
System.out.println("The largest bucket is 500,000,000 to 999,999,999 with a size of " + tier4);
}
else if (tier5 > tier1 && tier5 > tier2 && tier5 > tier3 && tier5 > tier4) {
System.out.println("The largest bucket is Less than or equal to 499,999,999 with a size of " + tier5);
}
else if (tier1 == tier2) {
System.out.println("The largest bucket is Greater than 2,000,000,000 with a size of " + tier1);
}
else if (tier1 == tier3) {
System.out.println("The largest bucket is Greater than 2,000,000,000 with a size of " + tier1);
}
else if (tier1 == tier4) {
System.out.println("The largest bucket is Greater than 2,000,000,000 with a size of " + tier1);
}
else if (tier1 == tier5) {
System.out.println("The largest bucket is Greater than 2,000,000,000 with a size of " + tier1);
}
else if (tier2 == tier3) {
System.out.println("The largest bucket is Greater than 2,000,000,000 with a size of " + tier2);
}
else if (tier2 == tier4) {
System.out.println("The largest bucket is Greater than 2,000,000,000 with a size of " + tier2);
}
else if (tier2 == tier5) {
System.out.println("The largest bucket is Greater than 2,000,000,000 with a size of " + tier2);
}
else if (tier3 == tier4) {
System.out.println("The largest bucket is Greater than 2,000,000,000 with a size of " + tier3);
}
else if (tier3 == tier5) {
System.out.println("The largest bucket is Greater than 2,000,000,000 with a size of " + tier3);
}
else if (tier4 == tier5) {
System.out.println("The largest bucket is Greater than 2,000,000,000 with a size of " + tier4);
}
strEntered = sc.next();
} // end of while loop
// Print the tallied totals of each bucket.
}
}
如果您需要用户输入仅为Done,则使用equals方法本身。
关于移动线路: 你最初做的是使用strEntered = sc.next()获得输入;在while循环之前。检查输入 - &gt;等于完成然后继续。
在while循环中: 扫描下一个输入后,它被转换为小写 - &gt;在下一次迭代中暗示,它不可能在前面用大写字母D“完成”。这就是我要求将扫描部分移动到while循环结束的原因
答案 1 :(得分:0)
移动线
int tier1,tier2,tier3,tier4,tier5;
在while循环之前,复制行
tier1 = 0;
…
tier5 = 0;
并在移动int tier1…
行之后(仍然在while循环之上/之外)粘贴它。
这将消除编译器错误并让您的应用程序运行。
发生的事情是因为你在while语句和if语句中使用了层变量,你需要在包含两者的范围中声明变量。同时声明它们只意味着 while可以看到/使用它们。这就是你必须将代码移出的原因。
你必须复制那些初始化行(所有“tier#= 0”行)的原因是因为变量肯定在if中使用if而不管while是否运行(例如,如果用户怎么办?将“完成”作为第一个输入)。但是,如果while循环永远不会运行,那么tier1等将全部未初始化,ifs将失败。
这就是为什么你必须给他们一些初始价值。在这种情况下零工作正常。
您只复制该代码(同时将其留在while中)的原因是因为您仍然需要将这些值重置为每个循环归零。
希望这是有道理和有帮助的。