Task: you need to enter purely from the keyboard and in it swap the last two digits.

100 <n <999 n enter from the keyboard after the change, we get x.

I believe that the solutions are a million. I would like to read the input through the scanner, limit the input from 101 to 998 and if the number is not in the range, then I would have to re-enter the number and as a result swap 2 and 3 digits: X is equal ... I figured out the scanner, but I can’t find the desired method to limit the input, in case of an error, return to the repeated input and in the input number, swap the digits. I don't need ready code, just push in the right direction.

import java.util.Scanner; // импорт сканера class Task { public static void main(String args[]){ System.out.print("Введите число: "); Scanner scan = new Scanner(System.in); int number = scan.nextInt(); System.out.println ("Вы ввели число " + number); } } 

java

Closed due to the fact that off-topic participants 0xdb , Roman C , freim , LFC , aleksandr barakin 8 Feb at 10:19 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • " Learning tasks are allowed as questions only on the condition that you tried to solve them yourself before asking a question . Please edit the question and indicate what caused you difficulties in solving the problem. For example, give the code you wrote, trying to solve a problem "- Roman C, LFC
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • one
    I can not find the method I need to limit the input x > 100 and x < 999 and in the input number, swap the digits y = 100 * (x \ 100) + 10 * (x % 10) + ((x % 100) \ 10) . - Akina
  • I'll try to offer my option. (1) - To begin with, it will be correct to check whether the entered string is generally a number. (2) - Further, if this is the case, we transform the string into a number and check that it belongs to the range (if (x> 100 && x <999) // do something). We check both conditions in a loop — if at least one is wrong, we interrupt the current iteration and start a new one (continue statement to help). If everything is correct, then we swap characters in the entered string (object of type String, but better than StringBuilder). This line and give as an answer. Here is a rough solution. - Bakuard
  • Create an input template and check it, if it doesn’t match, then execute a separate data entry request - Roman C

1 answer 1

You have already implemented keyboard input and output of the entered result. It remains to wrap your code in a loop and check it by condition - if the entered number is suitable - we exit the loop and move on, if not - please re-enter. How exactly to "turn" the numbers in the number - a lot of options. As an option - get a string from your number and swap characters in a string, then again lead to a number. It is possible through arithmetic operators to get three digits of your number - the remainder of dividing by 10, then the remainder of dividing by 10, the result of the first division by 10, etc. Then, having 3 digits - multiply them by the necessary digits and add.

  System.out.print("Введите число: "); Scanner scan = new Scanner(System.in); int number; while ((number = scan.nextInt()) < 100 || number > 999) { System.out.println("Введите повторно число от 100 до 999: "); } System.out.println ("Вы ввели число " + number); /// Строка byte[] data = String.valueOf(number).getBytes(); byte temp; temp = data[1]; data[1] = data[2]; data[2] = temp; int newNumber = Integer.parseInt(new String(data)); System.out.println("Перевернутое число: " + newNumber); /// Арифметика int[] numbers = new int[3]; int i = 0; while (number != 0) { numbers[i] = number % 10; number = number / 10; i++; } newNumber = numbers[2] * 100 + numbers[1] + numbers[0] * 10; System.out.println("Перевернутое число: " + newNumber); 
  • while ((number = scan.nextInt()) < 100 || number > 999) - cool done - Anton Sorokin
  • Brilliant! My rating is less than 15 and therefore the number of votes does not publicly change. Thank you so much! - Reserveb