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

时间:2016-01-17 01:49:11

标签: java while-loop

我目前正在处理文本字段,而我正在使用带有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);

    }

}

1 个答案:

答案 0 :(得分:3)

While(str1 == length)说明:

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

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

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

  3. 您从未声明变量length,我假设您打算将循环条件写为类似while(str1.length() != 0)。如果您的意思是写while(str1 == length),假设lengthint,那么将对象(String)与基元({{1})进行比较就无法做到})并且不允许。

  4. int说明:

    str1++导致错误的原因类似于上面的第二点。 str1++;str1String。大多数原语(Objectintfloat,...)都有double个运算符,但大多数对象都没有,只有少数像{{1} s,和做的对象是一成不变的。像C ++这样的语言允许你重载运算符,但Java不允许。因此,+, -, *, /, ++, --, ** ...不起作用,您必须使用Iterator类提供的方法来更改str1++的内容。

    String说明:

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

    未来的逻辑错误:

      

    作业要求用户输入句子。

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

    next

    以下是一个很好的StackOverflow问题及答案,以防您在使用nextLinestr1 = sc.nextLine(); 方法时遇到任何问题:Skipping nextLine() after using next(), nextInt() or other nextFoo() methods

    旁注:

    您可以使用Scanner循环来获得所需的结果,但在这种情况下更容易实现next循环。如果使用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);
    }