import java.util.Scanner; public class Ex1_1 { public static void main(String[] args) { @SuppressWarnings("all") Scanner sc = new Scanner(System.in); System.out.println("Введите числа для вычисления \nсреднего арифметического!!!"); int a = 0; int summ = 0; while (true){ System.out.print("число " + (a + 1) + ": "); int ch = sc.nextInt(a + 1); if (ch == 0000){ break; } ch = +ch; summ = summ + ch; ch++; } int i = summ / a; int j = summ % a; if (summ % a == 0) System.out.println("Ответ : " + i); else System.out.println("Ответ : " + i + " с остатком : " + j); } } 

This is the program code. When compiling, the following occurs: The compiler executes the loop once, and the second time it produces the following error:

 Введите числа для вычисления среднего арифметического!!! число 1: 123123 

Exception in thread "main" java.util.InputMismatchException: radix 1 less than Character.MIN_RADIX at java.util.Scanner.nextInt (Unknown Source) at Ex1_1.main (Ex1_1.java:13)


if I replace int ch = sc.nextInt (a + 1) with int ch = sc.nextInt () , then there will be an infinite loop

Enter the numbers to calculate the arithmetic average !!!
the number 1: 1
the number 1: 2
the number 1: 4
the number 1: 3
the number 1: 5
number 1: 7
number 1: 8
the number 1: 5
the number 1: 4
number 1:

  • You either need to come up with some adequate condition for getting out of the cycle, or first request the number of input numbers and stop the cycle upon receipt of this number. Now the program simply reads the data in an infinite loop, and then, provided that the program somehow all the same leaves the loop, divides the summ into a, which, by the way, is 0 and ends. Rewrite the code based on what you were told in the answer, which, by the way, solved the initial problem of the question, so you can put a check on it. - Aim X

2 answers 2

As for me, here is the incorrect use of the nextInt() method

sc.nextInt() - a method for getting an in-line representation of a character set obtained from the represented stream

sc.nextInt(int radix) - a method for getting an intv representation of a character set obtained from the represented stream, in the base SS, transmitted as the parameter of the nextInt(int radix) method nextInt(int radix)

To solve the problem, simply replace int ch = sc.nextInt(a + 1) with int ch = sc.nextInt()

And the cycle didn’t finish the first iteration, because after the first call of nextInt() the InputMismatchException exception is InputMismatchException and the method is forced to stop and throw an exception.

     public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Введите числа для вычисления \nсреднего арифметического!!!"); int a = 0; int summ = 0; while (true) { System.out.print("число " + (a + 1) + ": "); int ch = sc.nextInt(); if (ch == 0) { break; } summ += ch; a++; } int i = summ / a; int j = summ % a; if (summ % a == 0) System.out.println("Ответ : " + i); else System.out.println("Ответ : " + i + " с остатком : " + j); sc.close(); } 

    That's how it works. The error was in sc.nextInt (a + 1) and in the logic of the program