如何在另一个类中使用在循环内定义的String?

时间:2017-05-10 16:23:00

标签: java string class for-loop jframe

所以我已经成功编写了一个密码生成器,但我现在想要创建一个打印出创建的密码的JFrame。 这是我的代码: class passwort

    public class passwort
{
    public static void main(String[] args){
        Passwortgenerator fritz = new Passwortgenerator();       
        fritz.passwortausgeben();
    }
}

类Passwortgenerator

public class Passwortgenerator
{
   public int x;

    public Passwortgenerator() {
        this(1);
    }
    public Passwortgenerator(int x) {
        this.x = x;
    }   
    public void passwortausgeben(){

            int d=Zufallsgenerator.zufallszahl();
            int k=0;


                String[] alpha = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","1"};

                for(int i=0; i<alpha.length; i++){
                    for(int j=0; j<alpha.length; j++){
                        for(int l=0; l<alpha.length; l++){
                            for(int m=0; m<alpha.length; m++){


                                if( k<d ){
                                    k++;


                                }else{
                                    System.out.print(k + " " + alpha[i] + alpha[j] + alpha[l] + alpha[m]);

                                    String y=k + " " + alpha[i] + alpha[j] + alpha[l] + alpha[m];


                                    System.out.print(y);
                                    System.exit(0);
                                }
                            }
                        }
                    }
                }        
            }
        }

类Zufallsgenerator:

import java.util.Random;

public final class Zufallsgenerator
{
    public void main(String[] args){
        zufallszahl();
    }
    public static int zufallszahl(){
        Random Hanz = new Random();
        int d = Hanz.nextInt(100000);
        return d;


    }

}

类Fenster:

import javax.swing.JFrame;
import javax.swing.JLabel;

public final class Fenster
{
     public static void main(String[] args) 
    { 
        JFrame meinFrame = new JFrame("3");        
        meinFrame.setSize(200,200);
        meinFrame.add(new JLabel(y));
        meinFrame.setVisible(true);
    }
}

一切正常,但我找不到调用JFrame类的方法,以及如何使用第二类创建的String。我已经发现你不能在循环中使用String,但我无法想出任何替代方案。 谢谢你的帮助。

1 个答案:

答案 0 :(得分:1)

您设计的相关部分是:

public void passwortausgeben(){
    // ...
                                System.out.print(y);
                                System.exit(0);
 }

您的方法不仅生成密码,还负责将其显示给用户以及控制程序的生命周期(即立即退出)。

你真的想把这些问题分开。更改方法,使其仅生成密码。它不需要对其他任何事情负责。

public String passwortausgeben(){
    // ...
                                return y;
}

然后您可以单独处理其他问题。对于控制台使用:

public class passwort
{
    public static void main(String[] args){
        Passwortgenerator fritz = new Passwortgenerator();       
        final String password = fritz.passwortausgeben();
        System.out.print(password);
        // Note: no System.exit() is actually needed here, since the method
        // will end, and all threads have terminated, so the program
        // will exit naturally.
    }
}

然后您可以在Swing GUI中显示它。例如:

import javax.swing.JFrame;
import javax.swing.JLabel;

public final class Fenster
{
     public static void main(String[] args) 
    { 
        Passwortgenerator fritz = new Passwortgenerator();       
        final String password = fritz.passwortausgeben();

        JFrame meinFrame = new JFrame("3");        
        meinFrame.setSize(200,200);
        meinFrame.add(new JLabel(password));
        meinFrame.setVisible(true);
    }
}

请注意,由于您的Fenster课程有&#34; main()&#34;方法,你直接作为你的程序运行它。您的passwort课程将被闲置。如果您在命令行上运行程序,并且位于正确的目录中,则java Fenster。如果您使用的是IDE,则需要配置IDE的运行以告知Java运行时使用哪个类作为&#34; main&#34;类。

改变passwortausgeben()的一种方法的完整示例。正如您的编译器告诉您的那样,该方法确实需要返回一个值。这意味着它需要在所有情况下返回值 。由于您的成功生成是在条件声明中,它可能会也可能不会发生。 (d可能太大,在这种情况下,您永远不会生成密码)

public String passwortausgeben(){
    int d=Zufallsgenerator.zufallszahl();
    int k=0;

    String[] alpha = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","1"};

    for(int i=0; i<alpha.length; i++){
        for(int j=0; j<alpha.length; j++){
            for(int l=0; l<alpha.length; l++){
                for(int m=0; m<alpha.length; m++){
                    if( k<d ){
                        k++;
                    }else{
                        String y=k + " " + alpha[i] + alpha[j] + alpha[l] + alpha[m];
                        return y;
                    }
                }
            }
        }
    }

    throw new IllegalStateException("the loops all ended before k>=d, no password generated");
    }

设计方法的另一种方法(这需要Java 8,现在可以使用几年了):

public Optional<String> passwortausgeben(){
    int d=Zufallsgenerator.zufallszahl();
    int k=0;

    String[] alpha = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","1"};

    for(int i=0; i<alpha.length; i++){
        for(int j=0; j<alpha.length; j++){
            for(int l=0; l<alpha.length; l++){
                for(int m=0; m<alpha.length; m++){
                    if( k<d ){
                        k++;
                    }else{
                        String y=k + " " + alpha[i] + alpha[j] + alpha[l] + alpha[m];
                        return Optional.of(y);
                    }
                }
            }
        }
    }

    return Optional.empty();
    }

或者避免在方法中间返回。由于深度嵌套的循环以及一旦知道答案就停止做无用工作的愿望,这个有点麻烦。您的四个循环中的每一个都需要结束,但break只会结束包含break语句的循环。 Java确实有可以与break一起使用的标签,但这些标签几乎从不使用也不会被记住。

public Optional<String> passwortausgeben(){
    int d=Zufallsgenerator.zufallszahl();
    int k=0;

    String[] alpha = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","1"};
    String result = null;

    for(int i=0; i<alpha.length && result==null; i++){
        for(int j=0; j<alpha.length && result==null; j++){
            for(int l=0; l<alpha.length && result==null; l++){
                for(int m=0; m<alpha.length && result==null; m++){
                    if( k<d ){
                        k++;
                    }else{
                        result = k + " " + alpha[i] + alpha[j] + alpha[l] + alpha[m];
                    }
                }
            }
        }
    }

    return Optional.ofNullable(result);
    }

与您的实际问题无关,可以删除循环并替换为某个分区。

public String passwortausgeben(){
    int d=Zufallsgenerator.zufallszahl();
    final int k = d;

    String[] alpha = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","1"};

    int i = d / (alpha.length*alpha.length*alpha.length);
    d -= (i*alpha.length*alpha.length*alpha.length);

    int j = d / (alpha.length*alpha.length);
    d -= (j*alpha.length*alpha.length);

    int l = d / alpha.length;

    // the remainder
    int m = d % alpha.length;

    if (i < alpha.length)
        { return k + " " + alpha[i] + alpha[j] + alpha[l] + alpha[m]; }
    else
        { throw new IllegalStateException("Number is too large. No password generated"); }
    }