Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package dasda;
- import java.util.Scanner;
- import javax.swing.JOptionPane;
- import java.text.DecimalFormat;
- public class Teste {
- public static void main(String[] args) {
- Scanner soma = new Scanner(System.in);
- String num1, num2, num3;
- do{
- System.out.println("Digite o primeiro numero para somar:");
- num1 = soma.nextLine();
- }while(!isNumeric(num1));
- double c = Double.parseDouble(num1);
- do{
- System.out.println("Digite o segundo numero para somar:");
- num2 = soma.nextLine();
- }while(!isNumeric(num2));
- double b = Double.parseDouble(num2);
- do{
- System.out.println("Digite o terceiro numero para somar:");
- num3 = soma.nextLine();
- }while(!isNumeric(num3));
- double a = Double.parseDouble(num3);
- double somatorio = (a+b+c);
- double media = ( somatorio/3.0);
- DecimalFormat df = new DecimalFormat("###,##0.00");
- //JOptionPane.showMessageDialog(null,"A media e:\n\n"+(df.format(media)));
- System.out.println("Media: " + media);
- }
- private static final boolean isNumeric(final String s) {
- byte aux = 0;
- if (s == null || s.isEmpty()) return false;
- for (int x = 0; x < s.length(); x++) {
- final char c = s.charAt(x);
- if (x == 0 && (c == '-')) continue; // negative
- if ( (x > 0) && (aux == 0) && (c == '.') ){
- aux++;
- continue; // double
- }
- if ((c >= '0') && (c <= '9')) continue; // 0 - 9
- return false; // invalid
- }
- return true; // valid
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement