繁体   English   中英

Java while循环从字符串字符创建文本字段

[英]Java while-loop create text fields from string characters

我目前正在处理文本字段,而我正在使用带有Java的NetBeans 作业要求用户输入句子。 程序读取句子,并显示一个框架,其中包含用户输入的句子,但每个字符都在其自己的文本字段中。 下面是我的代码以及一些注释,指出我在哪里遇到错误。

package lab.pkg2.pkg2;

import java.awt.FlowLayout;
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JTextField;

public class Lab22 {

    public static void main(String[] args) {

        // initialize string 
        String str1;

        Scanner sc = new Scanner(System.in); // scanner reads string
        System.out.print("Enter a sentence");
        str1 = sc.next();
        System.out.println(str1);

        // Create frame 
        JFrame frame = new JFrame("Characters in Text field :");
        frame.setLayout(new FlowLayout());

        // Create a text field with a single character
        While(str1 == length) { // error
            JTextField tf = new JTextField(4);
            System.out.print(str1.length());

            String ch = str1;
            tf.setText(String.valueOf(ch));

            str1++; // error
        }

        String ch = str1;
        frame.add(tf); // error

        // make another text field with a single character
        // Set up the frame and displays it
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

    }

}

While(str1 == length)说明:

线条While(str1 == length)导致错误的原因有多种。

  1. While不是正确的语法, while使用小写w是正确的语法。

  2. str1是一个StringString是一个Object ,当通过operator ==检查Object是否相等时,会检查引用(内存中的位置),而不是对象的内容。 要检查对象之间的相等性,请使用.equals()方法。
    注意:在对象之间使用==运算符不会导致编译错误。 但是,如果用于检查对象相等性,那么它将导致逻辑​​错误,这通常比编译错误更糟糕。

  3. 你从来没有声明一个变量length ,我假设你打算把循环的条件写成类似于while(str1.length() != 0) 如果你的意思是写while(str1 == length) ,假设length是一个int ,那么它就不会因为将一个对象( String )与一个原语( int )进行比较而不允许它。

str1++说明:

原因str1++; 结果错误类似于上面的第二点。 str1是一个String ,它是一个Object 大多数原语( intfloatdouble ,...)都有+, -, *, /, ++, --, ** ...运算符,但大多数对象都没有,除了像Iterator这样的一些s,并且做的对象是石头。 像C ++这样的语言允许你重载运算符,但Java不允许。 因此, str1++不起作用,您必须使用String类提供的方法来更改str1的内容。

frame.add(tf)说明:

frame.add(tf)导致错误的原因是因为tf不再在范围内。 while循环中声明了tf ,在花括号( { } )内声明的任何东西都不能在花括号之外引用。 如果你需要在花括号内修改变量然后在花括号之外使用它,那么在花括号之前声明它。

未来的逻辑错误:

作业要求用户输入句子。

如果句子中的单词用空格分隔,那么你就会想知道为什么只处理句子中的第一个单词。 原因在于Scanner类的nextnextLine方法之间的区别。 next读取直到遇到指定的分隔符(默认为空格), nextLine读取直到遇到操作系统的换行符。 因此,您将要使用:

str1 = sc.nextLine();

这是一个很好的StackOverflow问题和答案,以防你在使用Scanner类的next方法时遇到任何麻烦: 在使用next(),nextInt()或其他nextFoo()方法后跳过nextLine()

边注:

您可以使用while循环来获得所需的结果,但在这种情况下, for循环更容易实现。 如果使用for循环,则不必在每次迭代时截断str1 一个for循环的例子:

// create text fields with a single character
for(int i = 0; i < str1.length(); i++) {
    JTextField tf = new JTextField(4);
    char ch = str1.charAt(i);

    // set the newly created text fields text to ch
    tf.setText(ch + "");

    // add the text field to frame while it's still in scope
    frame.add(tf);
}

但是,如果你必须使用while循环,那么类似于以下内容将起作用:

// create text fields with a single character
while(str1.length() != 0) {
    JTextField tf = new JTextField(4);
    char ch = str1.charAt(0);

    // chop off first (zeroeth) character from str1
    // unless it's the last character
    str1 = (str1.length() > 1) ? str1.substring(1) : "";

    // set the newly created text fields text to ch
    tf.setText(ch + "");

    // add the text field to frame while it's still in scope
    frame.add(tf);
}


工作main方法代码:

public static void main(String[] args) {
    // initialize string 
    String str1;

    Scanner sc = new Scanner(System.in); // scanner reads string
    System.out.print("Enter a sentence");
    str1 = sc.next();

    System.out.println(str1);

    // create and set up frame 
    JFrame frame = new JFrame("Characters in Text field :");
    frame.setLayout(new FlowLayout());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // create text fields with a single character
    for(int i = 0; i < str1.length(); i++) {
        JTextField tf = new JTextField(4);
        char ch = str1.charAt(i);

        // output the character for debugging?
        System.out.println(ch);

        // set the newly created text fields text to ch
        tf.setText(ch + "");

        // add the text field to frame while it's still in scope
        frame.add(tf);
    }

    // let frame's layout manager do it's thing
    frame.pack();
    // show the frame
    frame.setVisible(true);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM