Eingabe in einer Gleichung.

stefan.v

Mitglied
Guten frühen Abend.
Ich habe 3 Gleichungen und der Benuzer soll die Koeffizienten eingeben.
1:_x+_y+_z=_
2:_x+_y+_z=_
3:_x+_y+_z=_
Bei jeden Underline(_) soll der Benuzer eine zahl eingeben.
Java:
float a11;
                    float a12;
                    float a13;
                    float b1;
                    float a21;
                    float a22;
                    float a23;
                    float b2;
                    float a31;
                    float a32;
                    float a33;
                    float b3;
                    float D;
                    float Dp;
                    float Dn;
                    float Dx;
                    float Dxp;
                    float Dxn;
                    float Dy;
                    float Dyp;
                    float Dyn;
                    float Dz;
                    float Dzp;
                    float Dzn;
                    float x;
                    float y;
                    float z;                   
                   
                    System.out.println("");
                    System.out.print("a11(x): ");
                    a11 = myScan.nextFloat();
                    System.out.print("a12(y): ");
                    a12 = myScan.nextFloat();
                    System.out.print("a13(z): ");
                    a13 = myScan.nextFloat();
                    System.out.print("b1: ");
                    b1 = myScan.nextFloat();
                    System.out.print("a21(x): ");
                    a21 = myScan.nextFloat();
                    System.out.print("a22(y): ");
                    a22 = myScan.nextFloat();
                    System.out.print("a23(z): ");
                    a23 = myScan.nextFloat();
                    System.out.print("b2: ");
                    b2 = myScan.nextFloat();
                    System.out.print("a31(x): ");
                    a31 = myScan.nextFloat();
                    System.out.print("a32(y): ");
                    a32 = myScan.nextFloat();
                    System.out.print("a33(z): ");
                    a33 = myScan.nextFloat();
                    System.out.print("b3: ");
                    b3 = myScan.nextFloat();
                    System.out.println();
                    System.out.println("");
                    System.out.println(a11 + "x + " + a12 + "y + " + a13 + "z = " + b1);
                    System.out.println(a21 + "x + " + a22 + "y + " + a23 + "z = " + b2);
                    System.out.println(a31 + "x + " + a32 + "y + " + a33 + "z = " + b3);        
                    System.out.println(""); 
                    System.out.println("");

                    D = a11*a22*a33+a12*a23*a31+a13*a21*a32-(a13*a22*a31+a11*a23*a32+a12*a21*a33);
                    Dp = a11*a22*a33+a12*a23*a31+a13*a21*a32;
                    Dn = (a13*a22*a31+a11*a23*a32+a12*a21*a33);
Ich habe es bereits so gemacht aber ich möchte gerne das der Benutzer die Zahl direkt in der Gleichung eingeben kann.
Ich habe schon nach viel möglichkeiten gesucht aber leider keine gefunden. Ich möchte es auch nicht mit einer grafischen Oberfläche lösen.

Wenn jemand eine Ide hat schreiben Sie sie bitte.

Vielen Dank schon in vorhinein.

Liebe Grüße Stefan V.
 
eine idee wäre es den cursor immer an die entsprechende stell springen zu lassen und nach erfolgter eingabe die gleichung neu auszugeben in einer neuen zeile
 
Danke für die Idee. Nur wie lasse ich den curoser von einer auf die andere Zeile Springen? Und wenn ich etwas eingebe springt es automatisch in die nächst Zeile. Wie kann ich das verhindern?
 
Hab mal eine möglichkeit geschrieben, wie man es lösen könnte, ist vlt nicht die schönste.
aber so könntest du es lösen, zumindest das mit dem focus.

Java:
import java.awt.*;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.*;

/**
 *
 * @author Christian
 */
public class UI implements FocusListener{
    
    double[] gleichung1 =new double[ 3];
    double gleichung1Erg =0;
    double[] gleichung2 =new double[ 3];
    double gleichung2Erg =0;
    double[] gleichung3 =new double[ 3];
    double gleichung3Erg =0;
    
    JLabel xG1 =new JLabel( "x+ "); JLabel yG1 =new JLabel( "y+ "); JLabel zG1 =new JLabel( "z =");
    JLabel xG2 =new JLabel( "x+ "); JLabel yG2 =new JLabel( "y+ "); JLabel zG2 =new JLabel( "z =");
    JLabel xG3 =new JLabel( "x+ "); JLabel yG3 =new JLabel( "y+ "); JLabel zG3 =new JLabel( "z =");
    
    JLabel eG1 =new JLabel( " __");
    JLabel eG2 =new JLabel( " __");
    JLabel eG3 =new JLabel( " __");
    
    JTextField xTFG1 =new JTextField( "__"); JTextField yTFG1 =new JTextField( "__");
    JTextField xTFG2 =new JTextField( "__"); JTextField yTFG2 =new JTextField( "__");
    JTextField xTFG3 =new JTextField( "__"); JTextField yTFG3 =new JTextField( "__");
    
    JTextField zTFG1 =new JTextField( "__");
    JTextField zTFG2 =new JTextField( "__");
    JTextField zTFG3 =new JTextField( "__");
    
    JTextField[][] xyz =new JTextField[ 3][ 3];
    
    private void collectTF(){
        xyz[ 0][ 0] = xTFG1;
        xyz[ 0][ 1] = yTFG1;
        xyz[ 0][ 2] = zTFG1;
        xyz[ 1][ 0] = xTFG2;
        xyz[ 1][ 1] = yTFG2;
        xyz[ 1][ 2] = zTFG2;
        xyz[ 2][ 0] = xTFG3;
        xyz[ 2][ 1] = yTFG3;
        xyz[ 2][ 2] = zTFG3;
        for( JTextField[] xyz1 :xyz){
            for( JTextField xyz11 :xyz1){
                xyz11.addFocusListener(this);
            }
        }
    }
    
    
    GridBagConstraints gbc =new GridBagConstraints();
    
    JFrame frame =new JFrame( "Gleichung eingeben");
    JPanel panel =new JPanel( new GridBagLayout());
    
    
    public void initFrame(){
        collectTF();
        gbc.insets =new Insets(5, 5, 2, 5);
        //            x, y, w, h,  wx,   wy, fill                       gbc, panel, Component
        addComponent( 0, 0, 1, 1,    0,    0, GridBagConstraints.EAST,  gbc, panel, xTFG1);
        addComponent( 1, 0, 1, 1,    0,    0, GridBagConstraints.EAST,  gbc, panel, xG1);
        addComponent( 2, 0, 1, 1,    0,    0, GridBagConstraints.EAST,  gbc, panel, yTFG1);
        addComponent( 3, 0, 1, 1,    0,    0, GridBagConstraints.EAST,  gbc, panel, yG1);
        addComponent( 4, 0, 1, 1,    0,    0, GridBagConstraints.EAST,  gbc, panel, zTFG1);
        addComponent( 5, 0, 1, 1,    0,    0, GridBagConstraints.EAST,  gbc, panel, zG1);
        addComponent( 6, 0, 1, 1,    0,    0, GridBagConstraints.EAST,  gbc, panel, eG1);
        
        addComponent( 0, 1, 1, 1,    0,    0, GridBagConstraints.EAST,  gbc, panel, xTFG2);
        addComponent( 1, 1, 1, 1,    0,    0, GridBagConstraints.EAST,  gbc, panel, xG2);
        addComponent( 2, 1, 1, 1,    0,    0, GridBagConstraints.EAST,  gbc, panel, yTFG2);
        addComponent( 3, 1, 1, 1,    0,    0, GridBagConstraints.EAST,  gbc, panel, yG2);
        addComponent( 4, 1, 1, 1,    0,    0, GridBagConstraints.EAST,  gbc, panel, zTFG2);
        addComponent( 5, 1, 1, 1,    0,    0, GridBagConstraints.EAST,  gbc, panel, zG2);
        addComponent( 6, 1, 1, 1,    0,    0, GridBagConstraints.EAST,  gbc, panel, eG2);
        
        addComponent( 0, 2, 1, 1,    0,    0, GridBagConstraints.EAST,  gbc, panel, xTFG3);
        addComponent( 1, 2, 1, 1,    0,    0, GridBagConstraints.EAST,  gbc, panel, xG3);
        addComponent( 2, 2, 1, 1,    0,    0, GridBagConstraints.EAST,  gbc, panel, yTFG3);
        addComponent( 3, 2, 1, 1,    0,    0, GridBagConstraints.EAST,  gbc, panel, yG3);
        addComponent( 4, 2, 1, 1,    0,    0, GridBagConstraints.EAST,  gbc, panel, zTFG3);
        addComponent( 5, 2, 1, 1,    0,    0, GridBagConstraints.EAST,  gbc, panel, zG3);
        addComponent( 6, 2, 1, 1,    0,    0, GridBagConstraints.EAST,  gbc, panel, eG3);
        
        frame.add( panel);
        frame.setVisible( true);
        frame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo( null);
    }
    
    
    /**
     * Interne Methode zum hinzufügen der einzeln Componenten auf das Panel
     * 
     * @param gridx Colum
     * @param gridy Row
     * @param width count of Colums
     * @param height count of Rows
     * @param weightx percent of the colum for the Component
     * @param weighty percent of the row for the Component
     * @param fill fill orientation
     * @param gbc GridBagConstraints
     * @param pnl 
     * @param c Component to add
     */
    private void addComponent( int gridx, int gridy, int width, int height, double weightx, double weighty, int fill,
                               GridBagConstraints gbc, JPanel pnl, Component c){
        gbc.gridx      =gridx;    gbc.gridy     =gridy;
        gbc.weightx    =weightx;  gbc.weighty   =weighty;
        gbc.gridheight =height;   gbc.gridwidth =width;
        gbc.fill =fill;
        pnl.add( c, gbc);
    }
    
    @Override
    public void focusGained( FocusEvent e){
//      Auslesen welches objekt der auslößer war
        Object obj =e.getSource();
        if( e.getSource() instanceof JTextField){
//          Den kompletten inhalt des Textfeldes markieren
            (( JTextField) obj).selectAll();
        } else{}
    }
    @Override
    public void focusLost( FocusEvent e){
        boolean run =true;
        for( JTextField[] xyz1 :xyz){
            if( run){
                for( JTextField xyz11 :xyz1){
                    if( e.getSource().hashCode() ==xyz11.hashCode()){
                        xyz11.transferFocus(); //den focus transervieren
                        run =false;
                        break;
                    }
                }
            }
        }
    }
    
   
    public static void main( String[] args){
        UI ui =new UI();
        ui.initFrame();
    }
}
 
Vielen Dank für Ihren Tipp!

Ich habe es sogleich ausprobiert und es hat prima funktioniert.
Jetzt habe ich nur das Problem, dass ich die eingabe nicht speichern kann, bzw es wird nicht als Zahl gespeichert.
Wenn Sie eine Lösung auf das Problem haben, bzw einen Lösungsvorschlag haben schreiben Sie ihn mir bitte.
Das würde mich sehr freuen.

Mit freundlichen Grußen Stefan V.
 
etwas eigeninitiative:
aber was fehlt sind die Elemente/Stichworte JTextField.getText(); Float.parseFloat( String);

nur ne kurze frage, ist das für matrizenoperationen?
 
Zuletzt bearbeitet:
funktioniert es, oder gibt es probleme?
Java:
private void calc(){
        for( int row =0; row <a.length; row++){
            for( int col =0; col <a[ row].length; col++){
                if( xyz[ row][ col].getText().equals("__")){
                    a[ row][ col] =0;
                } else{
                    a[ row][ col] =Double.parseDouble( xyz[ row][ col].getText());
                }
            }
        }
        double d, dp, dn;
        d  = a[0][0]*a[1][1]*a[2][2]+a[0][1]*a[1][2]*a[2][0]+a[0][2]*a[1][0]*a[2][1]
           -(a[0][2]*a[1][1]*a[2][0]+a[0][0]*a[1][2]*a[2][1]+a[0][1]*a[1][0]*a[2][2]);
        dp = a[0][0]*a[1][1]*a[2][2]+a[0][1]*a[1][2]*a[2][0]+a[0][2]*a[1][0]*a[2][1];
        dn =(a[0][2]*a[1][1]*a[2][0]+a[0][0]*a[1][2]*a[2][1]+a[0][1]*a[1][0]*a[2][2]);
        eLG1.setText( Double.toString( d));
        eLG2.setText( Double.toString( dp));
        eLG3.setText( Double.toString( dn));
    }

wäre ein ansatz

ich glaube jeder hat das nicht vor der graischen oberfläche überlesen^^ sry
 
Zuletzt bearbeitet:
hab mal was probiert mittels cmd
Java:
package javaforum.Gleichungen;

import java.util.Scanner;

/**
 *
 * @author Christian
 */
public class Cmd{
    
    float[][] a =new float[ 3][ 3];
    float[] b =new float[ 3];
    String[][] inStreams ={{ "x1", "y1", "z1", "b1"},
                           { "x2", "y2", "z2", "b2"},
                           { "x3", "y3", "z3", "b3"}};
    String[] formatet = new String[3];
    
    boolean continueThis =true;
    
    
    public Cmd(){
        input();
    }
    
    private void input(){
        Scanner s =new Scanner( System.in);
        for( int row =0; row <inStreams.length; row++){
            for( int col =0; col <inStreams[ row].length; col++){
                System.out.println( "eingabe für " +inStreams[ row][ col] +": ");
                if( col <=2){
                    a[ row][ col] =s.nextFloat();
                    System.out.println( a[ row][ col]);
                } else if( col ==3){
                    b[ row] =s.nextFloat();
                    System.out.println( b[ row]);
                } 
            }
        }
    }
    
    private void showInput(){
        formatet[ 0] =String.format(" %10.4fx +%10.4fy +%10.4fz =%10.4f", a[ 0][ 0], a[ 0][ 1], a[ 0][ 2], b[ 0]);
        formatet[ 1] =String.format(" %10.4fx +%10.4fy +%10.4fz =%10.4f", a[ 1][ 0], a[ 1][ 1], a[ 1][ 2], b[ 1]);
        formatet[ 2] =String.format(" %10.4fx +%10.4fy +%10.4fz =%10.4f", a[ 2][ 0], a[ 2][ 1], a[ 2][ 2], b[ 2]);
        
        for( String inputs :formatet){
            System.out.println( inputs);
        }
    }
    
    private void calc(){
        double d, dp, dn;
        d  = a[0][0]*a[1][1]*a[2][2]+a[0][1]*a[1][2]*a[2][0]+a[0][2]*a[1][0]*a[2][1]
           -(a[0][2]*a[1][1]*a[2][0]+a[0][0]*a[1][2]*a[2][1]+a[0][1]*a[1][0]*a[2][2]);
        dp = a[0][0]*a[1][1]*a[2][2]+a[0][1]*a[1][2]*a[2][0]+a[0][2]*a[1][0]*a[2][1];
        dn =(a[0][2]*a[1][1]*a[2][0]+a[0][0]*a[1][2]*a[2][1]+a[0][1]*a[1][0]*a[2][2]);
        System.out.println("d: " +d);
        System.out.println("dp: " +dp);
        System.out.println("dn: " +dn);
    }
    
    private void eingabeOK(){
        System.out.println("Eingabe ok? y / n");
        Scanner s =new Scanner( System.in);
        continueThis=s.next().equals( "y");
    }
    
    public static void main( String[] args){
        Cmd cmd =new Cmd();
        cmd.showInput();
        cmd.eingabeOK();
        while( cmd.continueThis ==false){
            cmd.input();
            cmd.showInput();
            cmd.eingabeOK();
        }
        cmd.calc();
    }
    
}
 
Ich habe mein Problem dank Ihnen Gelöst. Vielen dank dafür.

Den Java Code(cmd) der ist doch so ähnlich wie ich es(siehe oben) gemacht habe nur das es anders geschrieben wurde oder?

LG Stefan V.
 
da Ihr code nicht vollständig ist, kann ich nur sagen, dass meiner wesentlich kompackter ist -> weniger schreibaufwand hat. Es geht noch schneller
Langsam fühle ich mich mit Sie und Ihnen echt alt.
 
Es tut mir leid wenn Sie(du) sich(dich) alt mit Sie und Ihnen fühlen(fühlst), nur da ich erst 14 Jahre alt bin nehme ich an das die meisten älter als ich bin und dadurch finde ich es, daneben das mann immer höflich sein soll, angemessen höflich zu sein.

Ps. Ich möchte mich nochmal bei Ihnen(dir) bedanken führ Ihre(deine) hilfsbereitschafft.

Mit freundlichen Grüßen Stefan V.
 
In diesem Forum ist es gang und gäbe sich zu Duzen. Mach dir also keinen Kopf darüber wie alt du bist. Hauptsache du bleibst sachlich und höflich.
 

Neue Themen


Zurück
Oben