phone 键盘格式的字母到数字转换器
letter to number converter in phone key pad format
我的程序有问题。
该指令是将用户输入的字母转换并在phone键盘中输出其对应的数字等值。
我一直有同样的错误:不兼容的类型
这是我目前所拥有的:
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.text.DecimalFormat;
public class phone extends JFrame implements ActionListener
{
JLabel num = new JLabel("Telephone Number(phones Format): ");
JButton btn1 = new JButton(" Convert ");
JLabel tel = new JLabel("Number Format: ");
JTextField txt1 = new JTextField(10);
JTextField txt2 = new JTextField(10);
Container c = getContentPane();
public phone()
{
super("Telephone");
c.setLayout(new FlowLayout());
c.add(num);
c.add(txt1);
c.add(btn1);
c.add(tel);
c.add(txt2);
btn1.addActionListener(this);
this.setSize(160,260);
this.setVisible(true);
this.setDefaultCloseOperation(3);
this.setResizable(false);
}
public void actionPerformed(ActionEvent a)
{
String txt=txt1.getText();
int x=0;
String [] phones = new String[8];
for( int i = 0; i<=8;i++)
{
if (phones=="A" || phones=="B" || phones== "C")
{
x=2;
}
else if(phones=="D" || phones=="E" || phones== "F")
{
x=3;
}
else if (phones=="G" || phones=="H" || phones== "I")
{
x=4;
}
else if (phones=="J" || phones=="K" || phones== "L")
{
x=5;
}
else if (phones=="M" || phones=="N" || phones== "O")
{
x=6;
}
else if (phones=="P" || phones=="Q" || phones== "R" || phones== "S")
{
x=7;
}
else if (phones=="T" || phones=="U" || phones== "V")
{
x=8;
}
else if (phones=="W" || phones=="X" || phones== "Y" || phones== "Z")
{
x=9;
}
}
txt2.setText(x);
}
public static void main(String args[])
{
phone M = new phone();
}
}
更新:
我设法做了一些更改,但我现在的问题是格式化部分。
格式应为:###-####
这里是更新:
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.text.DecimalFormat;
public class phone extends JFrame implements ActionListener
{
JLabel num = new JLabel("Telephone Number(txt Format): ");
JButton btn1 = new JButton(" Convert ");
JLabel tel = new JLabel("Number Format: ");
JTextField txt1 = new JTextField(10);
JTextField txt2 = new JTextField(10);
Container c = getContentPane();
public phone()
{
super("Telephone");
c.setLayout(new FlowLayout());
c.add(num);
c.add(txt1);
c.add(btn1);
c.add(tel);
c.add(txt2);
btn1.addActionListener(this);
this.setSize(160,260);
this.setVisible(true);
this.setDefaultCloseOperation(3);
this.setResizable(false);
}
public void actionPerformed(ActionEvent a)
{
String pattern="###-####";
DecimalFormat df=new DecimalFormat(pattern);
String txt= txt1.getText();
char[] data = txt.toCharArray();
for (int i=0; i<data.length; ++i) {
char c = data[i];
c = Character.toLowerCase(c);
switch (c) {
case 'a':
case 'b':
case 'c': data[i] = '2';
break;
case 'd':
case 'e':
case 'f': data[i] = '3';
break;
case 'g':
case 'h':
case 'i': data[i] = '4';
break;
case 'j':
case 'k':
case 'l': data[i] = '5';
break;
case 'm':
case 'n':
case 'o': data[i] = '6';
break;
case 'p':
case 'q':
case 'r':
case 's': data[i] = '7';
break;
case 't':
case 'u':
case 'v': data[i] = '8';
break;
case 'w':
case 'x':
case 'y':
case 'z': data[i] = '9';
}
}
String l=df.format(String.valueOf(data));
txt2.setText(l);
}
public static void main(String args[])
{
phone M = new phone();
}
}
phones
是字符串数组 (String []
),而不是 String
。您正在尝试将它与字符串进行比较,这就是您收到 incompatible types
错误
的原因
其他几个notes/problems:
- 您的
phone
class 应命名为 Phone
。 "Class names should be nouns, in mixed case with the first letter of each internal word capitalized"
phones=="A"
。字符串比较不是这样工作的。请参阅 this Whosebug 问题,了解有关为什么这不起作用以及应该如何比较字符串的更多详细信息
txt2.setText(x);
会给你另一个错误,因为 x
是一个 int
,并且该方法将 String
作为输入
this.setDefaultCloseOperation(3);
- 您应该使用 WindowConstants
中的常量之一,而不是硬编码的 3
。这将转化为 this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
我的程序有问题。 该指令是将用户输入的字母转换并在phone键盘中输出其对应的数字等值。 我一直有同样的错误:不兼容的类型
这是我目前所拥有的:
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.text.DecimalFormat;
public class phone extends JFrame implements ActionListener
{
JLabel num = new JLabel("Telephone Number(phones Format): ");
JButton btn1 = new JButton(" Convert ");
JLabel tel = new JLabel("Number Format: ");
JTextField txt1 = new JTextField(10);
JTextField txt2 = new JTextField(10);
Container c = getContentPane();
public phone()
{
super("Telephone");
c.setLayout(new FlowLayout());
c.add(num);
c.add(txt1);
c.add(btn1);
c.add(tel);
c.add(txt2);
btn1.addActionListener(this);
this.setSize(160,260);
this.setVisible(true);
this.setDefaultCloseOperation(3);
this.setResizable(false);
}
public void actionPerformed(ActionEvent a)
{
String txt=txt1.getText();
int x=0;
String [] phones = new String[8];
for( int i = 0; i<=8;i++)
{
if (phones=="A" || phones=="B" || phones== "C")
{
x=2;
}
else if(phones=="D" || phones=="E" || phones== "F")
{
x=3;
}
else if (phones=="G" || phones=="H" || phones== "I")
{
x=4;
}
else if (phones=="J" || phones=="K" || phones== "L")
{
x=5;
}
else if (phones=="M" || phones=="N" || phones== "O")
{
x=6;
}
else if (phones=="P" || phones=="Q" || phones== "R" || phones== "S")
{
x=7;
}
else if (phones=="T" || phones=="U" || phones== "V")
{
x=8;
}
else if (phones=="W" || phones=="X" || phones== "Y" || phones== "Z")
{
x=9;
}
}
txt2.setText(x);
}
public static void main(String args[])
{
phone M = new phone();
}
}
更新:
我设法做了一些更改,但我现在的问题是格式化部分。
格式应为:###-####
这里是更新:
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.text.DecimalFormat;
public class phone extends JFrame implements ActionListener
{
JLabel num = new JLabel("Telephone Number(txt Format): ");
JButton btn1 = new JButton(" Convert ");
JLabel tel = new JLabel("Number Format: ");
JTextField txt1 = new JTextField(10);
JTextField txt2 = new JTextField(10);
Container c = getContentPane();
public phone()
{
super("Telephone");
c.setLayout(new FlowLayout());
c.add(num);
c.add(txt1);
c.add(btn1);
c.add(tel);
c.add(txt2);
btn1.addActionListener(this);
this.setSize(160,260);
this.setVisible(true);
this.setDefaultCloseOperation(3);
this.setResizable(false);
}
public void actionPerformed(ActionEvent a)
{
String pattern="###-####";
DecimalFormat df=new DecimalFormat(pattern);
String txt= txt1.getText();
char[] data = txt.toCharArray();
for (int i=0; i<data.length; ++i) {
char c = data[i];
c = Character.toLowerCase(c);
switch (c) {
case 'a':
case 'b':
case 'c': data[i] = '2';
break;
case 'd':
case 'e':
case 'f': data[i] = '3';
break;
case 'g':
case 'h':
case 'i': data[i] = '4';
break;
case 'j':
case 'k':
case 'l': data[i] = '5';
break;
case 'm':
case 'n':
case 'o': data[i] = '6';
break;
case 'p':
case 'q':
case 'r':
case 's': data[i] = '7';
break;
case 't':
case 'u':
case 'v': data[i] = '8';
break;
case 'w':
case 'x':
case 'y':
case 'z': data[i] = '9';
}
}
String l=df.format(String.valueOf(data));
txt2.setText(l);
}
public static void main(String args[])
{
phone M = new phone();
}
}
phones
是字符串数组 (String []
),而不是 String
。您正在尝试将它与字符串进行比较,这就是您收到 incompatible types
错误
其他几个notes/problems:
- 您的
phone
class 应命名为Phone
。 "Class names should be nouns, in mixed case with the first letter of each internal word capitalized" phones=="A"
。字符串比较不是这样工作的。请参阅 this Whosebug 问题,了解有关为什么这不起作用以及应该如何比较字符串的更多详细信息txt2.setText(x);
会给你另一个错误,因为x
是一个int
,并且该方法将String
作为输入this.setDefaultCloseOperation(3);
- 您应该使用WindowConstants
中的常量之一,而不是硬编码的3
。这将转化为this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);