【问题标题】:How do I make it so that it will only accept a String from the first textField?如何使它只接受来自第一个文本字段的字符串?
【发布时间】:2012-05-28 16:49:41
【问题描述】:

我的代码用于扩展二项式的小程序。我还没有完成它,但它只接受 1 到 10 之间的指数,包括 1 到 10。我到了 7 点,但就像我说的,我还没有完成。我的问题是:在第一个文本字段中,它将接受一个数字和一个变量(如 4a 或其他东西)。而且我写的代码不能考虑这个数字(它应该只接受一个变量)。所以......我如何得到它,以便第一个 textField 不能允许有一个 int?谢谢!

import java.applet.Applet; 
import java.awt.*;
import java.awt.event.*; 
import java.applet.*; 
import javax.swing.*; 
import java.text.DecimalFormat; 
import java.util.ArrayList; 
import javax.swing.Action;

public class BinomialExpander extends JApplet implements ActionListener
{
  JLabel welcome;
  JLabel directions;
  JLabel example;

  JLabel instructions;
  JLabel startOfBinomial;
  JLabel plusSign;
  JLabel forExponent;
  JLabel endOfBinomial;

  JTextField firstVar;
  JTextField txtSecond;
  JTextField exp;


  JLabel lblExpanded; 
  JLabel outputExpanded;  
  FlowLayout layout;

  JButton compute;

  private int[] pascal1 = {1,1};
  private int[] pascal2 = {1,2,1};
  private int[] pascal3 = {1,3,3,1};
  private int[] pascal4 = {1,4,6,4,1};
  private int[] pascal5 = {1,5,10,10,5,1};
  private int[] pascal6 = {1,6,15,20,15,6,1};
  private int[] pascal7 = {1,7,21,35,35,21,7,1};
  private int[] pascal8 = {1,8,28,56,70,56,28,8,1};
  private int[] pascal9 = {1,9,36,84,126,84,36,9,1};
  private int[] pascal10 = {1,10,45,120,210,120,45,10,1};

  private String a;
  private int y; 
  private int expo;
  private String x;

  public void init() 
  {
      Container c = getContentPane(); 
      c.setBackground(Color.pink);

      layout = new FlowLayout();
      layout.setAlignment(FlowLayout.LEFT);
      c.setLayout(layout);
      setSize(500,175);

      welcome = new JLabel("Welcome to the Binomial Expander Applet!");
      welcome.setFont(new Font("Copperplate Gothic Bold", Font.PLAIN, 16));
      directions = new JLabel("Enter binomial ONLY in the form: (VARIABLE + NUMBER)^EXPONENT");
      directions.setFont(new Font("Times New Roman", Font.PLAIN, 14));
      example = new JLabel("EXAMPLE: (x + 2)^2.");
      instructions = new JLabel("Enter the variable of your binomial:");
      startOfBinomial = new JLabel(" (");
      firstVar = new JTextField(4);
      plusSign = new JLabel(" + ");
      txtSecond = new JTextField(4);
      endOfBinomial = new JLabel(")");
      forExponent = new JLabel("^");
      forExponent.setFont(new Font("Times New Roman", Font.PLAIN, 10));
      exp = new JTextField(2);
      compute = new JButton("Compute!");
      compute.addActionListener(this);
      lblExpanded = new JLabel("Your expanded binomial is: ");
      outputExpanded = new JLabel("");
      c.add(welcome);
      c.add(directions);
      c.add(example);
      c.add(instructions);
      c.add(startOfBinomial);
      c.add(firstVar);
      c.add(plusSign);
      c.add(txtSecond);
      c.add(endOfBinomial);
      c.add(forExponent);
      c.add(exp);
      c.add(compute);
      c.add(lblExpanded);
      c.add(outputExpanded);
  }
  public void actionPerformed(ActionEvent event) 
  {
      a = firstVar.getText();
      y = Integer.parseInt(txtSecond.getText());
      expo = Integer.parseInt(exp.getText());

      outputExpanded.setText(expand()); 
  }
  public String expand()
  {  
     if (expo < 1 || expo > 10)
     {
        x = "ERROR: Exponent value must be between 1 and 10, inclusive.";
        return x;
     }
     else if (expo == 1)
     {
        x = a + "+" + y;
        return x;  
     }
     else if (expo == 2)
     {
        x = a + "^" + expo + " + " + (pascal2[1] * y) + a + " + " + (pascal2[2] * Math.pow(y, expo));
        return x;
     }
     else if (expo == 3)
     {
        x = a + "^" + expo + " + " + (pascal3[1] * y) + a + "^" + (expo-1) + (pascal3[2] * Math.pow(y, expo-1)) + a + " + " + (Math.pow(y, expo));
        return x;
     }
     else if (expo == 4)
     {
        x = a + "^" + expo + " + " + (pascal4[1] * y) + a + "^" + (expo-1) + " + " + (pascal4[2] * Math.pow(y, expo-2)) + a + " ^ " + (expo-2) + " + " + (pascal4[3] * 
        Math.pow(y, expo-1)) + a + "+" + (Math.pow(y,expo));
        return x;
     }
     else if (expo == 5)
     {
        x = a + "^" + expo + " + " + (pascal5[1] * y) + a + "^" + (expo-1) + " + " + (pascal5[2] * Math.pow(y,expo-3)) + a + "^" + (expo-2) + " + " + (pascal5[3] * 
        Math.pow(y, expo-2)) + a + "^" + (expo-3) + " + " + (pascal5[4] * Math.pow(y, expo-1)) + a + " + " + (Math.pow(y, expo));
        return x;
     }
     else if (expo == 6)
     {
        x = a + "^" + expo + " + " + (pascal6[1] * y) + a + "^" + (expo-1) + " + " + (pascal6[2] * Math.pow(y, expo-4)) + a + "^" + (expo-2) + " + " + (pascal6[3] *
        Math.pow(y, expo-3)) + a + "^" + (expo-3) + " + " + (pascal6[4] * Math.pow(y, expo-2)) + a + "^" + (expo-4) + " + " + (pascal6[5] * Math.pow(y, expo-1)) +
        a + " + " + (Math.pow(y,expo));
        return x;
     }
     else if (expo == 7)
     {
        x = a + "^" + expo + " + " + (pascal7[1] * y) + a + "^" + (expo-1) + " + " + (pascal7[2] * Math.pow(y, expo-5)) + a + "^" + (expo-2) + " + " + (pascal7[3]
        * Math.pow(y, expo-4)) + a + "^" + (expo-3) + " + " + (pascal7[4] * Math.pow(y, expo-3)) + a + "^" + (expo-4) + " + " + (pascal7[5] * Math.pow(y, expo-2))
        + a + "^" + (expo-5) + (pascal7[6] * Math.pow(y, expo-1)) + a + " + " + (Math.pow(y, expo));
        return x;
     }
     return x; 
  }
  }

【问题讨论】:

    标签: java string applet int textfield


    【解决方案1】:

    我建议使用String'smatch() 函数来查看a 中的值是否是有效答案(我不知道什么对你有效)。然后如果它无效,您可以使用JDialog 来显示错误消息,而不是计算二项式。

    它看起来像这样:

      public void actionPerformed(ActionEvent event) 
      {
          a = firstVar.getText();
          y = Integer.parseInt(txtSecond.getText());
          expo = Integer.parseInt(exp.getText());
    
          if(a.matches(/*Some REGEX*/)) {
              outputExpanded.setText(expand());
          }
          else {
              //Use the JDialog
          } 
      }
    

    这里是页面:

    String

    JDialog

    【讨论】:

    • 他的意思是你应该运行检查字符串是否符合某些要求,如果没有打开一个对话框。
    • 哦!谢谢你uuuuuuuu!这解决了它!
    【解决方案2】:

    假设 a 是您想要从中删除数字的字符串,试试这个

    a = a.replaceAll("[0-9]*$", "");
    

    这将取出所有数字。

    【讨论】:

      【解决方案3】:

      以上答案足以回答您的问题。让我有点多管闲事,并指出您的代码可以通过使用循环而不是硬编码指令得到很大改进(和缩短)。

      我很快准备了以下代码:

      公共静态浮点系数(int n,int k){ int 支持 = 1; int inf = 1; 对于 (int i = (n-k)+1; i

      public static String Binomials(int limit, int y, String variable) {
          StringBuilder result = new StringBuilder("");
          for (int i = 0; i <= limit; i++) {
              if (i>0) {
                  result.append("+"+variable );
                  if (i>1) {
                      result.append("^" + i );
                  }
                  result.append("*");
              }
              result.append(  Math.pow(y, limit-i) * coefficient(limit, i));
          }
          return (result.toString());
      }
      

      它没有经过彻底的测试,也没有提高效率,但它似乎工作得很好。因此,我相信它可以作为您创建方法的通用版本的基础。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-10-21
        • 1970-01-01
        • 1970-01-01
        • 2013-02-19
        • 1970-01-01
        • 1970-01-01
        • 2012-05-08
        • 2020-06-30
        相关资源
        最近更新 更多