Exit loop For when typing note 1

Asked

Viewed 125 times

-1

This code I’m writing, where the option 1 the user enters notes that he wishes to insert into an ATM where it was to stop when the note 1 was inserted.

In the option 2 is to enter the value of the serve and this serve will only use the notes that were inserted.

In the option 0 the program ends.

Any way I can inject this condition of footnote 1 close loop?

public static void main(String[] args) {
        // TODO Auto-generated method stub
        String menu = "* MENU OPÇÕES * " + "\n" + "1 - Notas Existentes" + "\n" + "2 - Saque" + "\n" + "0 - Sair" + "\n";
        int [] possiveis = new int [7];
        int work = 1;
        int div1=0,div2=0,div3 = 0,div4 = 0,div5 = 0,div6 = 0,div7 = 0;
        int resto1=0,resto2=0,resto3 = 0,resto4 = 0,resto5 = 0,resto6 = 0,resto7 = 0;
        while (work!=0) {
            System.out.println(menu);
            Scanner entrada = new Scanner (System.in);
            System.out.println("Digite a opçao desejada: ");
            int opcao = entrada.nextInt();
            if (opcao==1) {
                for (int i=0;i<possiveis.length;i++) {
                    System.out.println("Digite as notas disponiveis");
                    possiveis[i] = entrada.nextInt();
                }
                for (int i =0; i<possiveis.length;i++) {
                    if(possiveis[i]!=1) {
                        System.out.println(possiveis[i]);
                    }else if (possiveis[i]==1 ){
                        System.out.println(possiveis[i]);
                    }
                    work=1;
                }
            }else if (opcao==2) {
                System.out.println("Digite o valor do saque");
                int saque = entrada.nextInt();
                if ((possiveis[0]>0)|| (possiveis[1] >0) || (possiveis[2] >0) || (possiveis[3] >0) ||(possiveis[4]>0) || (possiveis[5]>0) || (possiveis[6]>0)){
                div1 = saque / possiveis[0];
                resto1 = saque % possiveis[0];
                if (resto1>0){

                div2 = resto1 / possiveis[1];
                resto2 = resto1 % possiveis[1];}
                if (resto2>0){
                div3 = resto2 / possiveis[2];
                resto3 = resto2 % possiveis[2];}
                if (resto3>0){
                div4 = resto3 / possiveis[3];
                resto4 = resto3 % possiveis[3];}
                if (resto4>0){
                div5 = resto4 / possiveis[4];
                resto5 = resto4 % possiveis[4];}
                if ( resto5>0){
                div6 = resto5 / possiveis [5];
                resto6 = resto5 % possiveis [5];}
                if (resto6>0){
                div7 = resto6 / possiveis [6];
                resto7 = resto6 % possiveis [6];}
                }

                if (div1!=0) {
                    System.out.println("Total de notas de  " + possiveis[0] + " = " + div1);

                }
                if (div2!=0) {
                    System.out.println("Total de notas de  " + possiveis[1] + " = " + div2);

                }
                if (div3!=0) {
                    System.out.println("Total de notas de  " + possiveis[2] + " = " + div3);
                }
                if (div4!=0) {
                    System.out.println("Total de notas de  " + possiveis[3] + " = " + div4);
                }
                if (div5!=0) {
                    System.out.println("Total de notas de  " + possiveis[4] + " = " + div5);
                }
                if (div6!=0) {
                    System.out.println("Total denotas de  " + possiveis[5] + " = " + div6);
                }
                if (div7!=0) {
                    System.out.println("Total de notas de  " + possiveis[6] + " = " + div7);
                }
                work=1;
            }else if (opcao==0) {
                work=0;
            }else {
                System.out.println("Opcão invalida");
            }

        }
    }
  • Using a break?

  • But in case it was to stop typing the notes after typing 1,using break will only solve the issue of printing notes

  • You want to interrupt the first for (what you have possiveis[i] = entrada.nextInt())? If that’s it, you can put the break within the for, that only the for is interrupted and the while keeps rolling. Another thing, in the second for vc faz: if the value is different from 1, prints, and if it is equal to 1, also prints, what is the purpose of that?

  • Really that second for was logical mistake my haha, thank you for the touch

2 answers

2


From the various comments, hints that you want to interrupt this loop when typed 1:

for (int i=0;i<possiveis.length;i++) {
    System.out.println("Digite as notas disponiveis");
    possiveis[i] = entrada.nextInt();
}

Well, just use the break:

for (int i = 0; i < possiveis.length; i++) {
    System.out.println("Digite as notas disponiveis");
    int valor = entrada.nextInt();
    if (valor == 1) {
        break;
    }
    possiveis[i] = valor;
}

That one break only interrupts the for, and the while continues running normally (since that was his concern, according to this comment).

The problem is, by interrupting the for, the uninitialized elements will have zero value. And depending on the notes and the value of the serve, you may end up dividing by zero, which causes a ArithmeticException.


Well, I’ll take this opportunity to suggest some changes to your code.

You are creating a Scanner within the while, which means that each iteration creates a new instance. But it doesn’t need that, you can create only one at the beginning (before the while) and use it throughout the program.

Your second for it’s pretty weird:

for (int i = 0; i < possiveis.length; i++) {
    if (possiveis[i] != 1) {
        System.out.println(possiveis[i]);
    } else if (possiveis[i] == 1) {
        System.out.println(possiveis[i]);
    }
    work = 1;
}

If the value is different from 1, print, and if it is equal to 1, also print. I don’t know what you tried to do here, if it was some typo or whatever, but the way it is doesn’t make sense. The attribution of work = 1 is also done several times, which is redundant. From what I’ve seen, this line may be outside the for, right after he.

Another point is that you don’t need the variable work, just do while (true) (one loop infinite) and interrupt it with break when the option is zero:

while (true) {
    System.out.println(menu);
    System.out.println("Digite a opçao desejada: ");
    int opcao = entrada.nextInt();
    if (opcao == 1) {
        ...
    } else if (opcao == 2) {
        ...
    } else if (opcao == 0) {
        break; // <---- AQUI
    } else {
        System.out.println("Opcão invalida");
    }
}

That one break above interrupts the while, and he doesn’t mix with the break that we put inside the first for. There are those who criticize the use of break, but honestly, create this variable work I think worse.

I also suggest breaking each feature into a method of its own. For example:

public void lerNotas(Scanner entrada, int[] possiveis) {
    for (int i = 0; i < possiveis.length; i++) {
        System.out.println("Digite as notas disponiveis");
        int valor = entrada.nextInt();
        if (valor == 1) {
            break;
        }
        possiveis[i] = valor;
    }
    for (int i = 0; i < possiveis.length; i++) {
        System.out.println(possiveis[i]);
    }
}

public void saque(Scanner entrada, int[] possiveis) {
    System.out.println("Digite o valor do saque");
    int saque = entrada.nextInt();
    int divs[] = new int[possiveis.length];
    int resto = saque;
    for (int i = 0; i < divs.length; i++) {
        if (possiveis[i] != 0) {
            divs[i] = resto / possiveis[i];
            resto = resto % possiveis[i];
            if (resto == 0)
                break;
        }
    }
    for (int i = 0; i < divs.length; i++) {
        if (divs[i] != 0) {
            System.out.println("Total de notas de  " + possiveis[i] + " = " + divs[i]);
        }
    }
}

Notice I changed your cashout algorithm to use loops. I haven’t checked if it works for every possible case, I just took your logic and turned it into a loop. Anyway, if you search the site, you will probably find several algorithms for this problem (remember that your algorithm considers that there is an unlimited amount of notes - if you want to consider the amount of each note, there is another algorithm and already escapes the scope of the question).

And in the second for, removed the if/else that made no sense. Now just change the while to use the above methods:

Scanner entrada = new Scanner(System.in);
String menu = "* MENU OPÇÕES * " + "\n" + "1 - Notas Existentes" + "\n" + "2 - Saque" + "\n" + "0 - Sair" + "\n";
int[] possiveis = new int[7];
while (true) {
    System.out.println(menu);
    System.out.println("Digite a opçao desejada: ");
    int opcao = entrada.nextInt();
    if (opcao == 1) {
        lerNotas(entrada, possiveis);
    } else if (opcao == 2) {
        saque(entrada, possiveis);
    } else if (opcao == 0) {
        break;
    } else {
        System.out.println("Opcão invalida");
    }
}

1

To force the output of a while just use the reserved word break. Example:

while(condicao){
  if (opcao == 1) {
  // seu codigo aqui
  break;
  }
}
  • But in case I can’t leave while,it’s being used as my program menu,I wanted to stop typing inside the for when insert note 1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.