Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在try-catch块中递归调用方法_Java_Exception_Recursion_Exception Handling - Fatal编程技术网

Java 如何在try-catch块中递归调用方法

Java 如何在try-catch块中递归调用方法,java,exception,recursion,exception-handling,Java,Exception,Recursion,Exception Handling,嗨,我有这个问题,我有一个方法,让用户插入一个代表产品“数量”的值,现在,如果用户想要的数量高于库存数量,它必须抛出一个异常,让用户再次输入我尝试过的数字,插入相同方法的递归调用,但即使成功,它也会进入无限循环,就像异常仍然“活动”一样 编辑我现在包括了所有的代码,但它有点难,所以sry的“复杂性”的代码 完整代码 public void addToCart(ArrayList<Utilizzabile> cart,ArrayList<Integer> quant) {

嗨,我有这个问题,我有一个方法,让用户插入一个代表产品“数量”的值,现在,如果用户想要的数量高于库存数量,它必须抛出一个异常,让用户再次输入我尝试过的数字,插入相同方法的递归调用,但即使成功,它也会进入无限循环,就像异常仍然“活动”一样

编辑我现在包括了所有的代码,但它有点难,所以sry的“复杂性”的代码

完整代码

public void addToCart(ArrayList<Utilizzabile> cart,ArrayList<Integer> quant) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
boolean lol=false;
Utilizzabile us=null;
String id = JOptionPane.showInputDialog(frame, "Inserisci un ID prodotto:");
if (id ==null) { return;}
while (!id.matches("[0-9]+")) { //user inserts a value and the while checks for an int value inserted
    JOptionPane.showMessageDialog(frame, "Valore inserito errato");
    id = JOptionPane.showInputDialog(frame, "Inserisci un ID prodotto:");
    if (id == null) { return;} }
int iden = Integer.parseInt(id);
for (Utilizzabile u: arr) { // this for loop checks if the ID inserted represents a product in the catalog
    if ((u.getId() == iden) && (u.eAcquistabile())) {
        lol =true;
        us = u; } } 
if (lol == true) { //now if the ID corresponds to an existent product it ask the user to input the quantity requested
    boolean lol2=false;
    String qua = JOptionPane.showInputDialog(frame, "Inserisci un quantità da aggiungere al carrello:");
    if (qua ==null) { return;}
    while (lol2==false) {
    while (!qua.matches("[0-9]+")) {
        JOptionPane.showMessageDialog(frame, "Valore inserito errato");
        qua = JOptionPane.showInputDialog(frame, "Inserisci un quantità da aggiungere al carrello:");
        if (qua == null) { return;} }
    if (qua.length()>0 && qua.length()<=8) {
    int quantit = Integer.parseInt(qua);
    for (int l=0;l<cart.size();l++) { //this for checks if in the cart were already that product and then changes the quantities only
        if ((cart.get(l).getId() == us.getId()) && (us.getRem()-quantit >0) ) {
            int num = quant.get(l)+quantit;
            quant.set(l,num);
            JOptionPane.showMessageDialog(frame, "Quantità del prodotto richiesto aggiornata");
            return;}                
    }
    if ( (us.getRem()-quantit) >0) { //checks if all went good and the quantity is avaiable
            JOptionPane.showMessageDialog(frame, "Prodotto della quantità richiesta aggiunto al carrello");
            lol2=true;
            cart.add(us);
            quant.add(quantit);} }
    try {
        if (lol2==false)
            throw new NegativeNumberException(); }
    catch (NegativeNumberException pto){
        JOptionPane.showMessageDialog(frame, "Quantità non disponibile");
        this.addToCart(cart,quant); }       
    } }
else {
    JOptionPane.showMessageDialog(frame, "Prodotto non trovato");
    this.addToCart(cart,quant); }       
public void addToCart(ArrayList cart,ArrayList quant){
JFrame=新JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
布尔lol=false;
实用性us=null;
String id=JOptionPane.showInputDialog(框架,“Inserisci-un-id-prodotto:”);
如果(id==null){return;}
while(!id.matches(“[0-9]+”){//用户插入一个值,while检查插入的int值
showMessageDialog(框架,“Valore inserito errato”);
id=JOptionPane.showInputDialog(框架,“Inserisci-un-id-prodotto:”);
如果(id==null){return;}}
int-iden=Integer.parseInt(id);
for(utilitizabile u:arr){//此for循环检查插入的ID是否表示目录中的产品
if((u.getId()==iden)和&(u.eAcquistabile()){
lol=真;
us=u;}}
如果(lol==true){//现在如果ID对应于现有产品,它会要求用户输入请求的数量
布尔lol2=假;
String qua=JOptionPane.showInputDialog(框架,“插入数据的数量”);
如果(qua==null){return;}
while(lol2==false){
而(!qua.matches(“[0-9]+”){
showMessageDialog(框架,“Valore inserito errato”);
qua=JOptionPane.showInputDialog(框架,“插入卡雷罗数据的数量”);
如果(qua==null){return;}}
if(qua.length()>0&&qua.length()0){//检查是否一切正常,数量是否可用
showMessageDialog(框架,“Prodotto della quantitárichiesta aggiunto al-carrello”);
lol2=真;
cart.add(美国);
quant.add(quantit);}
试一试{
if(lol2==false)
抛出新的NegativeEnumberException();}
捕获(异常pto){
showMessageDialog(框架,“Quantitánon disponibile”);
this.addToCart(cart,quant);}
} }
否则{
showMessageDialog(框架,“Prodotto non-trovato”);
this.addToCart(cart,quant);}
}


这段代码本质上是一个图形部分,用于让用户将产品添加到购物车,并检查是否一切正常,但我需要放置一个例外,以检查库存数量是否少于用户想要的数量(我毫无例外地做到了,没有任何问题,但这是为了考试,我刚刚注意到教授希望我必须通过使用异常来解决这个问题。永远不要使用异常来控制常规或几乎常规的控制流。这是一种糟糕的编程风格

使用一些do语句重复该对话框,直到获得令人满意的输入

由于缺少上下文,没有提供任何代码。(递归调用在哪里?)

以后 不过,也有处理异常的空间。您可以放弃模式匹配和长度检查,并捕获NumberFormatException

Integer quantity = null;
do {
    String id ... dialogue
    try {
        quantity = Integer.parseInt( id );
        if( quantity <= 0 ) throw new NumberFormatException( "only positive integers" );
    } catch(  NumberFormatException nfe ){
        ... error dialogue;
        quantity = null;
    }
} until( quantity != null );
整数数量=null;
做{
字符串id…对话
试一试{
数量=整数.parseInt(id);

if(quantity永远不要使用异常来控制常规或几乎常规的控制流。这是糟糕的编程风格

使用一些do语句重复该对话框,直到获得令人满意的输入

由于缺少上下文,没有提供任何代码。(递归调用在哪里?)

以后 不过,也有处理异常的空间。您可以放弃模式匹配和长度检查,并捕获NumberFormatException

Integer quantity = null;
do {
    String id ... dialogue
    try {
        quantity = Integer.parseInt( id );
        if( quantity <= 0 ) throw new NumberFormatException( "only positive integers" );
    } catch(  NumberFormatException nfe ){
        ... error dialogue;
        quantity = null;
    }
} until( quantity != null );
整数数量=null;
做{
字符串id…对话
试一试{
数量=整数.parseInt(id);

如果(数量将try catch插入do while循环

当用户插入正确的值时停止循环 例如

inta=10;
做{
试一试{

如果(a将try catch插入do while循环

当用户插入正确的值时停止循环 例如

inta=10;
做{
试一试{

如果(a使用递归是不好的,因为在“n”调用之后,您可以接收StackOverflowerError。我同意@laune。 因此,我建议使用循环。例如:

while (true){
    // lol2 here is TRUE if was entered correct value and false if not.
    if (lol2)
        break;
    else {
        JOptionPane.showMessageDialog(frame, "Quantità non disponibile");
        this.addToCart(cart,quant);
    }
}

使用递归并不好,因为在“n”调用之后,您可以接收StackOverflowerError。我同意@laune。 因此,我建议使用循环。例如:

while (true){
    // lol2 here is TRUE if was entered correct value and false if not.
    if (lol2)
        break;
    else {
        JOptionPane.showMessageDialog(frame, "Quantità non disponibile");
        this.addToCart(cart,quant);
    }
}

使用
if(!lol2)
代替
if(lol2==false)
lol2?这毫无意义,至少提供一个有意义的变量名,以便其他人可以假设在何种情况下该变量的值。这样做也是一个很好的做法,那么您的代码即使在几年后也有意义later@Christian这只是一个建议,很好的编程实践。请给出更多的c上文本,我的意思是,递归方法是什么?那
try catch
在哪里?亲爱的上帝,你真的需要改进你的代码风格。我甚至不知道从哪里开始x(但我想你可以从空格开始…使用
if(!lol2)
而不是
if(lol2==false)
lol2?这毫无意义,至少提供一个有意义的变量名,以便其他人可以假设在何种情况下该变量的值。这样做也是一个很好的做法,那么您的代码即使在几年后也有意义later@Christian这只是一个建议,很好的编程实践。请给出更多的c我的意思是,什么是递归方法?那
try-catch
loc在哪里