首页 > 解决方案 > 回到开始而不杀死Java中的程序

问题描述

代码:

import java.util.Scanner;
import javax.swing.JOptionPane;

public class Cajero {

    public static void main(String[] args) {
        Scanner caja = new Scanner(System.in);

        Cuenta lidy = new Cuenta(600.0);
        Cuenta emma = new Cuenta(390.0);
        Cuenta zared = new Cuenta(200.0);

        String i="lidice";
        String k="emmanuel";
        String y="zared";
        String h;

        h=JOptionPane.showInputDialog(null,"BIENVENIDO al banco <<GC>> \n ¿Cuál es su nombre?");

       if(i.equals(h)){

      String z;
        double g=0;
        while (g!= 4) {

        z = JOptionPane.showInputDialog("BIENVENIDO LIDY \n Escoge una opción  : \n \n CONSULTAR SALDO-----1 \n ABONAR-----2 \n RETIRAR----3 \n NINGÚN TRAMITE----4");
        g = Double.parseDouble(z);

        if (g == 1) {

            JOptionPane.showMessageDialog(null, "Escogiste Consultar Saldo es correcto?");

            JOptionPane.showMessageDialog(null, "El saldo de lidy es  :" + lidy.getSaldo());
        }

        if (g == 2) {

            String x;

            JOptionPane.showMessageDialog(null, "Escogiste  ABONAR es correcto?");

            x = JOptionPane.showInputDialog("Cuánto deseas abonar?:   ");
            double c;
            c = Double.parseDouble(x);
            lidy.setAbonar(c);

            JOptionPane.showMessageDialog(null, "Su daldo es:" + lidy.getSaldo());

        }


        if (g == 3) {

                String s;
                 JOptionPane.showMessageDialog(null, "Escogiste  RETIRAR es correcto?");

                s = JOptionPane.showInputDialog("Cuánto deseas Retirar?");
                double w;
                w = Double.parseDouble(s);

                if(w>lidy.getSaldo()){
                    JOptionPane.showMessageDialog(null, "No saldo");
                }
                if(w<lidy.getSaldo()){

                     lidy.setretirar(w);

                    JOptionPane.showMessageDialog(null, "Su saldo es de:" + lidy.getSaldo());
                }

                }

                }

                  }

我的问题是这样的:

当程序说 NINGÚN TRAMITE----4 并且如果我在那里写 4 时,我想确保程序再次询问我的名字,就像它在开始时所做的那样。

不知道要不要放

if (g == 4)
{
 return ...
}

或类似的东西。

在 python 中,循环程序很容易。请按照上面提到的代码在 JAVA 中提供相同的帮助。

标签: java

解决方案


您可以使用 do-while 进行循环。

public class Cajero {

public static void main(String[] args) {
    Scanner caja = new Scanner(System.in);

    Cuenta lidy = new Cuenta(600.0);
    Cuenta emma = new Cuenta(390.0);
    Cuenta zared = new Cuenta(200.0);



    String i="lidice";
    String k="emmanuel";
    String y="zared";
    String h;

    int dialogResult;
    do{

        h=JOptionPane.showInputDialog(null,"BIENVENIDO al banco <<GC>> \n ¿Cuál es su nombre?");

        if(i.equals(h)){

            String z;
            double g=0;
            while (g!= 4) {



                z = JOptionPane.showInputDialog("BIENVENIDO LIDY \n Escoge una opción  : \n \n CONSULTAR SALDO-----1 \n ABONAR-----2 \n RETIRAR----3 \n NINGÚN TRAMITE----4");
                g = Double.parseDouble(z);



                if (g == 1) {

                    JOptionPane.showMessageDialog(null, "Escogiste Consultar Saldo es correcto?");

                    JOptionPane.showMessageDialog(null, "El saldo de lidy es  :" + lidy.getSaldo());
                }

                if (g == 2) {

                    String x;

                    JOptionPane.showMessageDialog(null, "Escogiste  ABONAR es correcto?");

                    x = JOptionPane.showInputDialog("Cuánto deseas abonar?:   ");
                    double c;
                    c = Double.parseDouble(x);
                    lidy.setAbonar(c);

                    JOptionPane.showMessageDialog(null, "Su daldo es:" + lidy.getSaldo());

                }


                if (g == 3) {

                    String s;
                    JOptionPane.showMessageDialog(null, "Escogiste  RETIRAR es correcto?");

                    s = JOptionPane.showInputDialog("Cuánto deseas Retirar?");
                    double w;
                    w = Double.parseDouble(s);

                    if(w>lidy.getSaldo()){
                        JOptionPane.showMessageDialog(null, "No saldo");
                    }
                    if(w<lidy.getSaldo()){

                        lidy.setretirar(w);

                        JOptionPane.showMessageDialog(null, "Su saldo es de:" + lidy.getSaldo());
                    }

                }


            }

        }
        dialogResult = JOptionPane.showConfirmDialog(null, "Would you like to exit?", "Title", JOptionPane.YES_NO_OPTION);
    }while(dialogResult==JOptionPane.NO_OPTION);
}

}

推荐阅读