Advertisement
Guest User

Untitled

a guest
Aug 29th, 2014
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.05 KB | None | 0 0
  1. package dasda;
  2.  
  3. import java.util.Scanner;
  4. import javax.swing.JOptionPane;
  5. import java.text.DecimalFormat;
  6.  
  7. public class Teste {
  8.  
  9.         public static void main(String[] args) {
  10.  
  11.                 Scanner soma = new Scanner(System.in);
  12.                        
  13.  
  14.                 String num1, num2, num3;
  15.                
  16.                 do{
  17.                     System.out.println("Digite o primeiro numero para somar:");
  18.                     num1 = soma.nextLine();
  19.                 }while(!isNumeric(num1));
  20.                 double c = Double.parseDouble(num1);
  21.                
  22.                 do{
  23.                     System.out.println("Digite o segundo numero para somar:");
  24.                     num2 = soma.nextLine();
  25.                 }while(!isNumeric(num2));
  26.                 double b = Double.parseDouble(num2);
  27.                
  28.                 do{
  29.                     System.out.println("Digite o terceiro numero para somar:");
  30.                     num3 = soma.nextLine();
  31.                 }while(!isNumeric(num3));
  32.                 double a = Double.parseDouble(num3);
  33.                                
  34.                 double somatorio = (a+b+c);
  35.                 double media = ( somatorio/3.0);
  36.                 DecimalFormat df = new DecimalFormat("###,##0.00");
  37.                                
  38.                //JOptionPane.showMessageDialog(null,"A media e:\n\n"+(df.format(media)));
  39.                 System.out.println("Media: " + media);
  40.  
  41.  
  42.  
  43.         }
  44.        
  45.         private static final boolean isNumeric(final String s) {
  46.               byte aux = 0;
  47.            
  48.               if (s == null || s.isEmpty()) return false;
  49.               for (int x = 0; x < s.length(); x++) {
  50.                     final char c = s.charAt(x);
  51.                     if (x == 0 && (c == '-')) continue;  // negative
  52.                     if ( (x > 0) && (aux == 0) && (c == '.') ){
  53.                         aux++;
  54.                         continue; // double                    
  55.                     }
  56.                     if ((c >= '0') && (c <= '9')) continue;  // 0 - 9
  57.                     return false; // invalid
  58.               }
  59.               return true; // valid
  60.         }
  61.  
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement