在java中添加字符值 import java.awt.*//容器、网格布局、*等。。。 导入javax.swing.*//JFrame、JLabel、*等。。。 导入java.awt.event.*; 公共类NumerologyEC扩展JFrame { 专用静态最终整数宽度=400; 专用静态最终内部高度=200; 私人JLabel wordJL,sumJL; 私有JTextField wordTF,sumTF; 私有JButton计算ejb,exitJB; 私有CalculateButtonHandler cbHandler; 私有ExitButtonHandler ebHandler; 公共数字学() { setTitle(“命理学额外学分”); wordJL=newjlabel(“输入一个单词:”,SwingConstants.RIGHT); sumJL=新的JLabel(“字母总数:”,SwingConstants.RIGHT); wordTF=新的JTextField(10); sumTF=新的JTextField(10); CalculateEJB=新的JButton(“计算”); cbHandler=新的CalculateButtonHandler(); CalculateEJB.addActionListener(cbHandler); exitJB=新的JButton(“退出”); ebHandler=新的ExitButtonHandler(); exitJB.addActionListener(ebHandler); 容器窗格=getContentPane(); pane.setLayout(新的GridLayout(3,2)); 窗格。添加(wordJL); 添加(wordTF); 添加(计算EJB); 添加(exitJB); 窗格。添加(sumJL); 窗格。添加(sumTF); 设置尺寸(宽度、高度); setVisible(真); setDefaultCloseOperation(关闭时退出); } 私有类CalculateButtonHandler实现ActionListener { 已执行的公共无效操作(操作事件e) { 双字母和; letterSum=Sumchar值; } } 私有int-sumCharValues(字符串输入){ 字符串总数=input.toLowerCase(); int结果=0; 对于(int i=0,n=total.length();i
如何在“私有类CalculateButtonHandler implements ActionListener”中实现sumcharvalues函数?我正在尝试添加字符的值。我只是个新手,伙计们。提前感谢您的帮助您可以在执行算术之前通过强制转换为int对字符求和。在java中添加字符值 import java.awt.*//容器、网格布局、*等。。。 导入javax.swing.*//JFrame、JLabel、*等。。。 导入java.awt.event.*; 公共类NumerologyEC扩展JFrame { 专用静态最终整数宽度=400; 专用静态最终内部高度=200; 私人JLabel wordJL,sumJL; 私有JTextField wordTF,sumTF; 私有JButton计算ejb,exitJB; 私有CalculateButtonHandler cbHandler; 私有ExitButtonHandler ebHandler; 公共数字学() { setTitle(“命理学额外学分”); wordJL=newjlabel(“输入一个单词:”,SwingConstants.RIGHT); sumJL=新的JLabel(“字母总数:”,SwingConstants.RIGHT); wordTF=新的JTextField(10); sumTF=新的JTextField(10); CalculateEJB=新的JButton(“计算”); cbHandler=新的CalculateButtonHandler(); CalculateEJB.addActionListener(cbHandler); exitJB=新的JButton(“退出”); ebHandler=新的ExitButtonHandler(); exitJB.addActionListener(ebHandler); 容器窗格=getContentPane(); pane.setLayout(新的GridLayout(3,2)); 窗格。添加(wordJL); 添加(wordTF); 添加(计算EJB); 添加(exitJB); 窗格。添加(sumJL); 窗格。添加(sumTF); 设置尺寸(宽度、高度); setVisible(真); setDefaultCloseOperation(关闭时退出); } 私有类CalculateButtonHandler实现ActionListener { 已执行的公共无效操作(操作事件e) { 双字母和; letterSum=Sumchar值; } } 私有int-sumCharValues(字符串输入){ 字符串总数=input.toLowerCase(); int结果=0; 对于(int i=0,n=total.length();i,java,character,Java,Character,如何在“私有类CalculateButtonHandler implements ActionListener”中实现sumcharvalues函数?我正在尝试添加字符的值。我只是个新手,伙计们。提前感谢您的帮助您可以在执行算术之前通过强制转换为int对字符求和。 e、 g 这可能有点过分自信,但它应该会起作用。试试这个 result += ((int) c) - ((int) 'a') + 1 包文件操作; 导入java.awt.*//容器、网格布局、*等。。。 导入javax.swing.
e、 g 这可能有点过分自信,但它应该会起作用。试试这个
result += ((int) c) - ((int) 'a') + 1
包文件操作;
导入java.awt.*//容器、网格布局、*等。。。
导入javax.swing.*//JFrame、JLabel、*等。。。
导入java.awt.event.*;
公共类NumerologyEC扩展JFrame
{
专用静态最终整数宽度=400;
专用静态最终内部高度=200;
私人JLabel wordJL,sumJL;
私有JTextField wordTF,sumTF;
私有JButton计算ejb,exitJB;
私有CalculateButtonHandler cbHandler;
私有ExitButtonHandler ebHandler;
公共数字学()
{
setTitle(“命理学额外学分”);
wordJL=newjlabel(“输入一个单词:”,SwingConstants.RIGHT);
sumJL=新的JLabel(“字母总数:”,SwingConstants.RIGHT);
wordTF=新的JTextField(10);
sumTF=新的JTextField(10);
CalculateEJB=新的JButton(“计算”);
cbHandler=新的CalculateButtonHandler();
CalculateEJB.addActionListener(cbHandler);
exitJB=新的JButton(“退出”);
ebHandler=新的ExitButtonHandler();
exitJB.addActionListener(ebHandler);
容器窗格=getContentPane();
pane.setLayout(新的GridLayout(3,2));
窗格。添加(wordJL);
添加(wordTF);
添加(计算EJB);
添加(exitJB);
窗格。添加(sumJL);
窗格。添加(sumTF);
设置尺寸(宽度、高度);
setVisible(真);
setDefaultCloseOperation(关闭时退出);
}
私有类CalculateButtonHandler实现ActionListener
{
已执行的公共无效操作(操作事件e)
{
if(例如getSource().equals(calculateEJB))
{
双字母和;
lettsum=sumCharValues(wordTF.getText());
sumTF.setText(letterSum+“”);
}
}
}
私有int-sumCharValues(字符串输入){
字符串总数=input.toLowerCase();
int结果=0;
对于(int i=0,n=total.length();i
它工作正常吗?试着更好地解释这个问题。另外,不要忘记对函数的调用类似于sumCharValues()
而不是sumCharValues
。
result += ((int) c) - ((int) 'a') + 1
package fileoperations;
import java.awt.*; //Container, GridLayout, *, or etc...
import javax.swing.*; //JFrame, JLabel, *, or etc...
import java.awt.event.*;
public class NumerologyEC extends JFrame
{
private static final int Width = 400;
private static final int Height = 200;
private JLabel wordJL, sumJL;
private JTextField wordTF, sumTF;
private JButton calculateJB, exitJB;
private CalculateButtonHandler cbHandler;
private ExitButtonHandler ebHandler;
public NumerologyEC()
{
setTitle ("Numerology Extra Credit");
wordJL = new JLabel ("Enter a word: ", SwingConstants.RIGHT);
sumJL = new JLabel ("Sum of letters: ", SwingConstants.RIGHT);
wordTF = new JTextField(10);
sumTF = new JTextField(10);
calculateJB = new JButton ("Calculate");
cbHandler = new CalculateButtonHandler();
calculateJB.addActionListener (cbHandler);
exitJB = new JButton ("Exit");
ebHandler = new ExitButtonHandler();
exitJB.addActionListener (ebHandler);
Container pane = getContentPane();
pane.setLayout (new GridLayout (3, 2));
pane.add(wordJL);
pane.add(wordTF);
pane.add(calculateJB);
pane.add(exitJB);
pane.add(sumJL);
pane.add(sumTF);
setSize(Width, Height);
setVisible (true);
setDefaultCloseOperation (EXIT_ON_CLOSE);
}
private class CalculateButtonHandler implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
if(e.getSource().equals(calculateJB))
{
double letterSum;
letterSum = sumCharValues(wordTF.getText());
sumTF.setText(letterSum+"");
}
}
}
private int sumCharValues (String input){
String total = input.toLowerCase();
int result = 0;
for (int i = 0, n = total.length(); i < n; i++) {
char c = total.charAt(i);
result += (c - 'a' + 1);
}
return result;
}
private class ExitButtonHandler implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
System.exit (0);
}
}
public static void main (String[] args)
{
NumerologyEC rectObject = new NumerologyEC();
}
}