- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我的作业是从 7 位数电话号码生成每个可能的单词,并使用 PrintWriter 将其保存为 .txt 文件。我的代码如下,但我的输出(当前仅打印到控制台)是相同的 3 个“单词”2187 次。
package ks2_Lab19;
import java.util.Scanner;
import java.io.PrintWriter;
import java.io.FileNotFoundException;
public class WordGenerator {
private static String[] two = {"a", "b", "c"};
private static String[] three = {"d", "e", "f"};
private static String[] four = {"g", "h", "i"};
private static String[] five = {"j", "k", "l"};
private static String[] six = {"m", "n", "o"};
private static String[] seven = {"p", "r", "s"};
private static String[] eight = {"t", "u", "v"};
private static String[] nine = {"w", "x", "y"};
private static char[] numArray;
private static String[] wordList = new String[2187];
public static void convert(char[] input){
for (int i = 0; i < 2184; i = i + 3){
for (int a = 0; a < 7; a++){
for (int b = 0; b < 3; b++){
if (input[a] == '1' || input[a] == '0') {
wordList[i+b] = wordList[i+b] + " ";
}//if 0 or 1
if (input[a] == '2'){
wordList[i+b] = wordList[i+b] + two[b];
}//if 2
if (input[a] == '3'){
wordList[i+b] = wordList[i+b] + three[b];
}//if 3
if (input[a] == '4'){
wordList[i+b] = wordList[i+b] + four[b];
}//if 4
if (input[a] == '5'){
wordList[i+b] = wordList[i+b] + five[b];
}//if 5
if (input[a] == '6'){
wordList[i+b] = wordList[i+b] + six[b];
}//if 6
if (input[a] == '7'){
wordList[i+b] = wordList[i+b] + seven[b];
}//if 7
if (input[a] == '8'){
wordList[i+b] = wordList[i+b] + eight[b];
}//if 8
if (input[a] == '9'){
wordList[i+b] = wordList[i+b] + nine[b];
}//if 9
}//possible output for loop
}//input array for loop
}//write to wordList for loop
}
public static void main(String[] args) {
//initialize output file name and PrintWriter object
String fileName = "output.txt";
PrintWriter outputStream = null;
String output = "";
//try and catch exception
try {
outputStream = new PrintWriter(fileName);
}
catch (FileNotFoundException e){
System.out.println("Error opening file " + fileName + ".");
System.exit(0);
}
//initialize scanner and wordList array
Scanner kb = new Scanner(System.in);
for (int i=0; i < 2187; i++){
wordList[i] = "";
}
//announce and accept input
System.out.println("Please input a 7 digit phone number without special characters.");
String num = kb.next();
numArray = num.toCharArray();
convert(numArray);
for (int p = 0; p < 2187; p++){
System.out.println(wordList[p]);
}
}
}
最佳答案
我相信以下代码正确解决了问题并演示了尾递归的使用,这听起来像是练习的目标。另请注意,我正在利用 Collection 库中的各种项目,而不仅仅是使用原始数组和大型 if/then/else block 。
<小时/>import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class WordGenerator {
private static Map<Character, char[]> digitMap;
static {
digitMap = new HashMap<Character, char[]>();
digitMap.put(Character.valueOf('0'), new char[] { ' ' });
digitMap.put(Character.valueOf('1'), new char[] { ' ' });
digitMap.put(Character.valueOf('2'), new char[] { 'a', 'b', 'c' });
digitMap.put(Character.valueOf('3'), new char[] { 'd', 'e', 'f' });
digitMap.put(Character.valueOf('4'), new char[] { 'g', 'h', 'i' });
digitMap.put(Character.valueOf('5'), new char[] { 'j', 'k', 'l' });
digitMap.put(Character.valueOf('6'), new char[] { 'm', 'n', 'o' });
digitMap.put(Character.valueOf('7'), new char[] { 'p', 'r', 's' });
digitMap.put(Character.valueOf('8'), new char[] { 't', 'u', 'v' });
digitMap.put(Character.valueOf('9'), new char[] { 'w', 'x', 'y' });
}
public static void convert(String input, String resultSoFar, List<String> allResults) {
if (input.length() == 0) {
// We have hit the end of the input phone number and thus the end of
// recursion
allResults.add(resultSoFar);
} else {
// Strip the next character off the front of the phone number
Character nextDigit = Character.valueOf(input.charAt(0));
// Look up the list of mappings from that digit to all letters
char[] mappingArray = digitMap.get(nextDigit);
// More robust error handling would throw an exception or do
// something else when an unknown character was encountered in the
// phone number.
if (mappingArray != null) {
// We have processed the first digit in the rest of the number,
// so recurse with the rest of the number
String inputTail = input.substring(1);
// By iterating through the array the mapping lists do not all
// have to be the same size.
for (char nextLetter : mappingArray) {
// Put the next mapped letter on the end of the result being
// built and recurse
convert(inputTail, resultSoFar + nextLetter, allResults);
}
}
}
}
public static void main(String[] args) {
// Simplified version that does not ask for input
String num = "8675309";
List<String> results = new ArrayList<String>();
// Starting condition is that the entire input needs to be processed,
// the result so far is empty, and we have nothing in the list of final
// answers
convert(num, "", results);
for (String nextResult : results) {
System.out.println(nextResult);
}
System.out.println("End of results list. Total words generated: " + results.size());
}
}
关于java - 卡在java电话号码单词生成器上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19793247/
我有一个包含姓名、电子邮件和内容文本区域的表单。一切正常,但当我尝试为手机号码添加 1 个表单输入时,表单不会提交。 Javascript: function hgsubmit() { i
所以我正试图在 Scheme 中找出整个 call/cc 的东西。下面是我正在使用的代码: (+ 1 (call/cc (lambda (k) (if (number? k)
所以我正试图在 Scheme 中找出整个 call/cc 的东西。下面是我正在使用的代码: (+ 1 (call/cc (lambda (k) (if (number? k)
我在 list 中有权限: 检查电话是否正在使用的代码可能会为无法接听电话的平板电脑等设备启动安全异常。所以,我制作了这个方法来检查是否设备可以使用 TelephonyManager: priva
我想知道 facebook API 允许 PHP 网页提取以下用户数据的先决条件是什么: 姓名 电子邮件 电话 据我了解,提取电话号码需要您的网站在 facebook/Websense 的白名单中吗?
我如何从我的应用程序调用特定号码的电话?给我一些执行此任务的逻辑或代码... 最佳答案 使用 UIApplication 的 openURL: 方法: [[UIApplication sharedAp
在 URI 中,空格可以编码为 + .既然如此,那么在创建具有国际前缀的 tel URI 时是否应该对前导加号进行编码? 哪个更好?两者在实践中都有效吗? Call me Call me 最佳答案 不
我正在尝试使用 Insomnia 调用 SOAP 电话 - 特别是试图让帖子成功。我将 URL 定义为端点,并将正文类型作为带有 SOAP 内容(信封、标题、正文)的 XML。我在标题中定义了用户 I
服务器有没有办法将一些数据无线无缝地推送到客户端,可能是 Windows(电话)、iPhone、Mac 或 Android 设备,没有任何操作系统集成? 如果是这样,最好的设计模式是什么,最好的技术是
我有一个搜索字段,我希望用户能够通过电话或电子邮件进行搜索。但是,我的 onclick 事件没有触发。我已经尝试过控制台工具,但似乎无法通过那里进行调试。 {% from "_form
如果重新加载接收调用的浏览器窗口,我将面临实时 Twilio 调用被丢弃的情况。有没有办法在不影响实时通话的情况下克服这种挫折? 最佳答案 Twilio 布道者在这里。 根据您的问题,我假设您使用的是
我试图在我的服务中减少我的 promise 对象。我有类似的东西 angular.module('myApp').service('testService', ['Employees','$q',
我想在我的java应用程序中构建一个电话调用器。为此,我使用了 JAIN-SIP 库。第一次 INVITE 后,系统需要代理身份验证。第二个邀请是在“AuthenticationHelperImpl.
两天前我正在开发一个 VOIP 应用程序,并成功地使用 Sinch 实现了一个普通的应用程序到应用程序调用。 .该应用程序运行良好。当我开始时,我在他们的文档中看到他们支持电话 session 。 现
我正在尝试创建一个 java 程序,它将创建 excel 文件并将其上传到谷歌驱动器中。上传后我需要它来授予权限。我已经完成了所有这些,但问题在于尝试将 excel 文件转换为 google 文件,以
我想使用 Android Sip 进行电话 session .那可能吗?有人可以举一个有效的例子吗?使用该库是否有任何限制,比如它可以在 3G 或 4G 上工作? 最佳答案 当前的 Android S
是否可以通过编程方式在 android 中接听电话? 我发现有些地方不可能,但后来安装了应用程序 https://play.google.com/store/apps/details?id=com.a
我试图在单击导航中的某个按钮后将一个类附加到 div。 $navButtons.on('click', navigationClick); // liseten to the navigation a
在 Monotouch 中是否有任何方法可以读取电话或调用者服务的当前状态? 我正在尝试寻找某种方式来读取通话是否处于事件状态或通话是否处于等待状态等。 用谷歌搜索没有找到任何结果。 希望运行一些代码
我可能正在搜索错误的内容,这可能解释了为什么我只能找到有关设置密码自动填充的信息。 我正在寻找用户能够在应用程序中输入电子邮件、电话、名字和姓氏的功能,就像某些网站能够做到的那样,选项出现在键盘上方。
我是一名优秀的程序员,十分优秀!