0

我正在尝试将其从输入框更改为 Scanner 类,但我在这样做时遇到了麻烦。

它是一个接受单词并将它们变成电话号码的程序,这里是这样做的代码。任何帮助将不胜感激,如果有什么我可以做的作为回报,我很乐意这样做。

// declare imports
import java.util.Scanner;
import javax.swing.JOptionPane;
import java.util.*;
public class Telephone {
    public static void main(String[] args) {
        // ask for the phone number (in letters)
        char letter;
        String inputMessage = "Please enter the number in Letters " + "or enter '#' to stop the program ";
        String inputString = JOptionPane.showInputDialog(inputMessage);
        String outputString = "";
        String outputMessage = "";
        int digit = 0;
        int x = 0;
        for (int i = 0; i < inputString.length(); i++)
        System.out.print(inputString.charAt(x)); {
            while (inputString.charAt(x) != '#') {
                letter = Character.toUpperCase(inputString.charAt(x));
                x++;
                // make sure its not a number
                if (letter >= 'a' && letter <= 'z') if (letter >= 'A' && letter <= 'Z') {
                    digit++;
                    switch (letter) {
                        case 'A':
                        case 'B':
                        case 'C':
                            outputString += "2";
                            break;
                        case 'D':
                        case 'E':
                        case 'F':
                            outputString += "3";
                            break;
                        case 'G':
                        case 'H':
                        case 'I':
                            outputString += "4";
                            break;
                        case 'J':
                        case 'K':
                        case 'L':
                            outputString += "5";
                            break;
                        case 'M':
                        case 'N':
                        case 'O':
                            outputString += "6";
                            break;
                        case 'P':
                        case 'Q':
                        case 'R':
                        case 'S':
                            outputString += "7";
                            break;
                        case 'T':
                        case 'U':
                        case 'V':
                            outputString += "8";
                            break;
                        case 'W':
                        case 'X':
                        case 'Y':
                        case 'Z':
                            outputString += "9";
                    }
                    if (digit == 7) {
                        break;
                    }
                    if (digit == 3) {
                        outputString += "-";
                    }
                }
                inputMessage = "Enter another set of telephone letters";
            }
            JOptionPane.showMessageDialog(null, outputString, "Telephone Program", JOptionPane.PLAIN_MESSAGE);

        }
4

1 回答 1

1

修改显示 JOptionPane 的行,如下所示,

//String inputString = JOptionPane.showInputDialog(inputMessage);
System.out.println(inputMessage);
Scanner sc = new Scanner(System.in);
String inputString = sc.nextLine();

所以你可以执行以下操作,

package test;

import java.util.Scanner;

public class Telephone {

    public static void main(String[] args) {
        // ask for the phone number (in letters)
        char letter;
        String inputMessage = "Please enter the number in Letters " + "or enter '#' to stop the program ";
//        String inputString = JOptionPane.showInputDialog(inputMessage);

        System.out.println(inputMessage);
        Scanner sc = new Scanner(System.in);
        String inputString = sc.nextLine();

        String outputString = "";
        String outputMessage = "";
        int digit = 0;
        int x = 0;
        for (int i = 0; i < inputString.length(); i++) {
            System.out.print(inputString.charAt(x));
        }

        while (inputString != null && inputString.trim().length() > 0 && inputString.charAt(x) != '#') {
            letter = Character.toUpperCase(inputString.charAt(x));
            x++;
            // make sure its not a number
//                if (letter >= 'a' && letter <= 'z') {
            if (x >= inputString.length()) {
                x = 0;
                System.out.println("\n" + outputString);
//                JOptionPane.showMessageDialog(null, outputString, "Telephone Program", JOptionPane.PLAIN_MESSAGE);
//                inputString = JOptionPane.showInputDialog(inputMessage);
                System.out.println(inputMessage);
                inputString = sc.nextLine();
            } else if (letter >= 'A' && letter <= 'Z') {
                digit++;
                switch (letter) {
                    case 'A':
                    case 'B':
                    case 'C':
                        outputString += "2";
                        break;
                    case 'D':
                    case 'E':
                    case 'F':
                        outputString += "3";
                        break;
                    case 'G':
                    case 'H':
                    case 'I':
                        outputString += "4";
                        break;
                    case 'J':
                    case 'K':
                    case 'L':
                        outputString += "5";
                        break;
                    case 'M':
                    case 'N':
                    case 'O':
                        outputString += "6";
                        break;
                    case 'P':
                    case 'Q':
                    case 'R':
                    case 'S':
                        outputString += "7";
                        break;
                    case 'T':
                    case 'U':
                    case 'V':
                        outputString += "8";
                        break;
                    case 'W':
                    case 'X':
                    case 'Y':
                    case 'Z':
                        outputString += "9";
                }
                if (digit == 7) {
                    break;
                }
                if (digit == 3) {
                    outputString += "-";
                }
            }
//                }
            inputMessage = "Enter another set of telephone letters";
        }
        System.out.println("\n" + outputString);
//        JOptionPane.showMessageDialog(null, outputString, "Telephone Program", JOptionPane.PLAIN_MESSAGE);

    }
}
于 2013-10-16T22:54:11.333 回答