What's new

Closed Pahelp about loops java

Status
Not open for further replies.
Baka hindi mo nailagay si Scanner, nagbase lang kasi ako sa sinend mo eh
Pero eto boss sure nato triny ko na para sayo.
Code:
import java.util.*;
public class Main
{
    public static void main(String[] args) {
        Scanner input = new Scanner (System.in);
       
        char rep;
        char repnum;
        int loop;
       
        do{
           
        int  jeff = 0;
        int counter = 0;
        float ave;
        System.out.println("How many numbers that you want to add?");
        loop = input.nextInt();
            do{
                for (int x = 0;loop > x;x++){
                System.out.println("Input any number you want to add");
                int jeff2 = input.nextInt();
                jeff += jeff2;
                counter += 1;
                }
            System.out.println("Do you want to add another number?\nY - Yes\nN - No\n");
            repnum = input.next().charAt(0);
            }while(repnum == 'Y' || repnum == 'y');
       
        ave = jeff / counter;
        System.out.println("Sum is : " + jeff);
        System.out.println("Average is : " + ave);

        System.out.println("\n\nDo you want to get another average?\nY - Yes\nN - No\n");
        rep = input.next().charAt(0);
        }while(rep == 'Y' || rep == 'y');
    }
}
okay na paps.. na lagyan kona ;)
 
Try it with an online REPL: You do not have permission to view the full content of this post. Log in or register now.

Java:
import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;

// Reads and validates user input
class Parser {
  final static int INPUT_BREAK = 0;

  public static List<Integer> read() {
    Scanner scanner = new Scanner(System.in);
    List<Integer> elements = new ArrayList<>();
    
    while(true) {
      try {
        System.out.printf("Enter an integer value ('%d' to exit): ", INPUT_BREAK);
        int input = scanner.nextInt();
        if (input == INPUT_BREAK) { break; }
        elements.add(input);

      } catch(Exception ex) {
        System.out.printf("Invalid input! Please try again.\n");
        // To clear up any garbage input after an exception
        scanner.nextLine();
      }
    };
    scanner.close();

    return elements;
  }
}

// Does the math processing
class Arithmetic {
  public static void process(List<Integer> elements) {
    System.out.printf("Sum: %d \n", sum(elements));
    System.out.printf("Average: %f \n", average(elements));
  }

  private static int sum(List<Integer> elements) {
    return elements.stream().reduce(0, (a, b) -> a + b);
  }

  private static double average(List<Integer> elements) {
    return (double) sum(elements)/elements.size();
  }
}

class Main {
  public static void main(String[] args) {
    Arithmetic.process(Parser.read());
  }
}


Bash:
# Sample output

Enter an integer value ('0' to exit): 1
Enter an integer value ('0' to exit): 2
Enter an integer value ('0' to exit): 3
Enter an integer value ('0' to exit): a
Invalid input! Please try again.
Enter an integer value ('0' to exit): b
Invalid input! Please try again.
Enter an integer value ('0' to exit): 0
Sum: 6
Average: 2.000000
 
RikkuRikku Also I wanna point out two things on your question:

1. Fix the code formatting. You are asking people to look at your code snippet. You might as well do the formatting correctly.
2. Also use Java as the language and not JavaScript. They are not the same language.

Most of the issues I see in code are caused by unreadable and untidy code. If it's hard to read in the eyes of a senior developer then probably that code is bad.
 
Status
Not open for further replies.

Similar threads

Back
Top