Въведение в програмирането, Наков, задача 9 от Методи /java/
package execises; import java.util.Arrays; import java.util.Scanner; public class ThreeInOne { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("The tasks:"); System.out.println("1. Reverse the digits in number"); System.out.println("2. Average in line"); System.out.println("3. Result from a * x + b = 0"); System.out.print("Please, choice the task(number of task): "); int number = scanner.nextInt(); System.out.println(); if (number == 1){ System.out.print("Please, enter the number > 0: "); int num = scanner.nextInt(); reversedNumber(num); } else if (number == 2){ System.out.print("Please, enter the sequence, separated by \",\": "); String line = scanner.next();//2,5,10,5,8 averageOfLine(line); }else { System.out.print("Please, enter the double variables \"a\" and \"b\" (a != 0): "); double a = scanner.nextDouble(); double b = scanner.nextDouble(); printSolution(a, b); } } private static void printSolution(double a, double b) { //a * x + b = 0 double x = -b / a; System.out.printf("x = %.2f", x); } private static void averageOfLine(String line) { int[] result = Arrays.stream(line.split(",")).mapToInt(Integer::parseInt).toArray(); int sum = 0; for (int i = 0; i < result.length ; i++) { sum += result[i]; } double average = sum * 1.0 / result.length; System.out.printf("%.2f%n", average); } private static void reversedNumber(int num) { while (num > 0) { int lastDigit = num % 10; System.out.print(lastDigit); num /= 10; } System.out.println(); } }
Благодаря, ще я барна още малко, :))