código de aula 11,12,13,14,15 fluxo condicional
Autor: fabioberlimtrabalho | Publicado: 2012-03-17T08:38:00-07:00

package Logica;&nbsp;/**&nbsp;*&nbsp;* @fabio berlim&nbsp;*/
import java.util.ArrayList;import java.util.Random;import java.util.Scanner;
public class Fluxo {
 /**  * @param args  */ /**  * @param args  */ public static void main(String[] args) {  //aula de if e else&nbsp;  int forca = 1;  if(forca &gt; 10){  System.out.println("você moveu a pedra");   }else{System.out.println("você não tem força suficiente!");}  //jogar um numero do dado se for pra você acerta o ataque  Random r = new Random();  int dado = r.nextInt(6) + 1;//num pode 0  //achar numero par e impar  if((dado % 2) == 0){System.out.println("par você acertou o ataque");}  else{System.out.println("impar você errou!");}  //fazer perguntas  System.out.println("Qual caminho você escolhe?");  System.out.println("1- montanha 2-floresta 3-cidade" );  Scanner s = new Scanner(System.in);  int dinheiro = 100;  int x = s.nextInt();  //varias condições  if(x == 1){System.out.println("voce segue pela montanha e encontra um ogro");}  else if(x == 2){System.out.println("voce segue pela floresta e é roubado");}  else if(x == 3){System.out.println("Na cidade você vai fazer compras");  //você pode colocar condições dentro de outras condições  if(dinheiro &gt;50){System.out.println("você pode comprar uma master sword");}}  else{System.out.println("você vai ficar parado!");}  int dinheiro2 = 100;  System.out.println("Quem matou a vidente?");  System.out.println("1- neta 2-criada arabe 3-servo indiano" );  int x2 = s.nextInt();//colocar numero da resposta  switch(x2){  case 1:   System.out.println("Ela demonstro um grande intere-se na morte da velha");   break;  case 2:   System.out.println("Ela era unica que estranhamente ja tinha uma arma");   break;  case 3:   System.out.println("Paraceu realmente se precupar com a vidente quando quis arrombar a porta");   break;  default:   System.out.println("ambos estavão fora do quarto pode ser nenhum dos 3");   //agora varios casos com a mesma resposta&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /*usarei netbeans pois esse comando e uma novidade do java 7 e&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; o eclipse não aceita switch com string */   System.out.println("escolha um desses pokemons");&nbsp;   System.out.println("pikachu zápidos voltorbe bulbasauro oddishi chikorita charmander Vulpix magmar");   String pokemons = s.nextLine();   switch(pokemons){  case "pikachu":  case "zápidos":  case "voltorbe":   System.out.println("voce escolhe um pokemon do tipo elétrico");   break;  case "bulbasauro":  case "oddishi":  case "chikorita":   System.out.println("voce escolhe um pokemon do tipo folha");   break;  case "charmander":  case "Vulpix":  case "magmar":  System.out.println("voce escolhe um pokemon do tipo fogo");  break;  default:&nbsp;   System.out.println("desconhecido");   }  }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //dentro do for pode usar numeros e variaveis vo usar variaveis pra ficar interativo&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int i = 0;i&lt;3;i++ ){//imprimir uma sequencia de numeros&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(i);}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("digite um numero de vezes que você quer que repita a mensagem");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int repeticao = s.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int i = 0;i&lt;=repeticao;i++){//imprimir uma mensagem muitas vezes&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("eu bart simpson não farei os professores chorar");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //essa variavel so pode ser usada dentro do bloco for&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String texto = "";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("digite um numero pra ver uma sequência crescente e decresente");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int numero = s.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int a = 0;a&lt;numero;a++){//cescente de 0 a numero&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; texto += a + ",";}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(texto);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String texto2 = "";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int b = numero;b&gt;0;b--){//decresentes de numero a 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; texto2 += b + ",";}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(texto2);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;String texto3 = "";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println("escolha um numero para saber quantos numeros imapares e pares tem");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int numero2 = s.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int c = 0;c&lt;=numero2;c++){//quantos numeros pares á de 0 a numero&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(c % 2 == 0){texto3 += c + ",";}}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(texto3);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;String texto4 = "";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int d = 0;d&lt;=numero2;d++){//quantos numeros impares á de 0 a numero&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(x % 2 != 0){texto4 += d + ",";}}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(texto4);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("qual tamanho do quadrado?");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int tamanho = s.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int e = 0;e&lt;=tamanho;e++){//for dentro de for montar um quadrado&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int i = 0;i&lt;=tamanho;i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print("* ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println();//quebra de linha}&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //for comum percorrendo um array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int[] pares = {2,4,6,8};&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int i = 0; i&lt;pares.length;i ++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int par = pares[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(par + ",");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println();//quebra de linha so pra separar os exemplos&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //foreach ou for aprimorado percorrendo um array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int par : pares){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(par + ",");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }//é mais simples porem não da pra editar a variavel isso limita muito&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ArrayList&lt;Integer&gt; list = new ArrayList&lt;Integer&gt;();&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; for(int i = 0;i &lt;= 10;i++){//adicionar a lista numeros&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list.add(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println();//quebra de linha&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(Integer numero3:list){//apresentar os numeros que você adicionou na lista&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(numero3 + ",");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Fluxo de repetição while,do while&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int i = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while(i &lt; 10){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println();//quebra de linha&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int f =0;f &lt; 10;f++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(f);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int x3 = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; do{//execultar o código pelo menos uma vez&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x3++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }while(x3 &gt; 10);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ArrayList&lt;String&gt; produtos = new ArrayList&lt;String&gt;();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("liste seus produtos &nbsp;: para sair digite fim");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String produto;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while(!"fim".equals(produto = s.nextLine())){//método equals de String serve para comparar&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; produtos.add(produto);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(produtos.toString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //aula de break,continue e rótulos&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while(true){//isso seria um loop infinito&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Entrou");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int g = 0;g &lt;=10;g++){//para a repetição quando i for igual a 5&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(g == 5){ break; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(g);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println();//quebra de linha pra mostrar os dois exemplos&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int h = 0;h &lt;=10;h++){//pula uma parte da repetição quando i for igual a 5&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(h == 5){ continue; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(h);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }//usar rótulos para saber qual você quer break e continue&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; um://isso é um rótulo&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int j = 0;j &lt; 2;j++){//vai repitir duas vezes aquele for que vão ser interrompidos no 5&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dois:&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int k = 0;k &lt;=10;k++){//para a repetição quando i for igual a 5&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(k == 5){ break dois; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(k + " ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println();//quebrar linha denovo o certo era apagar código anterior mais bom deixar gravado&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;boolean[][] matrix = {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {false,true,false,false},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {false,false,false,false} &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;busca:&nbsp; for(int a = 0;a&lt;matrix.length;a++){//os dois elementos do Array vão imprimir A&nbsp; &nbsp; &nbsp; System.out.print("A ");&nbsp; &nbsp; &nbsp; for(int b = 0;b&lt;matrix[a].length;b++){//os elementos dentro do conjunto são os b&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(matrix[a][b]){System.out.print("True &nbsp;");}//procurar algo e parar a procura quando achar&nbsp; &nbsp; &nbsp; System.out.print("B ");}&nbsp;&nbsp; &nbsp; &nbsp; break busca;&nbsp; }}}