объяснение))))
- да я знаю что код не идеальный, в основном потому, что я пока не разобрался как правильно работать с консолью в В IntelliJ IDEA (в контексте получения с нее данных);
- цикл while при получении данных с консоли использован потому что мы не можем знать какой набор (количество) цифр будет введен
- прерывание по вводу стоп слова потому, что просто не знаю как по другому сказать программе что ввод закончен
- два разных сканера с наборами данных потому что хотел создать массив именно с необходимой длинной (в зависимости от количества данных)
p.s. по совету попробую включить строчку кода что то типа ... int N = console.nextInt(); для проверки
update не помогло
public class Solution {
public static void main(String[] args) {
//напишите тут ваш код
Scanner console = new Scanner(System.in);
String stopline = "", inputdata = "";
int arraylength = 0, digit;
boolean firstdigit = false;
while (true){
if (console.hasNextInt()){
digit = console.nextInt();
if (!firstdigit){
arraylength = digit;
if (arraylength <= 0)
return;
firstdigit = true;
} else
inputdata = inputdata + " " + digit;
} else {
stopline = console.nextLine();
if (stopline.equals("stop"))
break;
}
}
Scanner sc = new Scanner(inputdata);
int[] array = new int[arraylength];
for (int i = 0; i < arraylength; i++)
array[i] = sc.nextInt();
if (arraylength % 2 == 0)
for (int i = (arraylength - 1); i >= 0; i--)
System.out.println(array[i]);
else
for (int i = 0; i < arraylength; i++)
System.out.println(array[i]);
console.close();
sc.close();
}
}package com.javarush.task.pro.task05.task0505;
import java.util.Scanner;
/*
Reverse
*/
public class Solution {
public static void main(String[] args) {
//напишите тут ваш код
Scanner console = new Scanner(System.in);
String stopline = "", inputdata = "";
int N = 0, digit;
boolean firstdigit = false;
while (true){
if (console.hasNextInt()){
digit = console.nextInt();
if (!firstdigit){
N = digit;
if (N <= 0)
return;
firstdigit = true;
} else
inputdata = inputdata + " " + digit;
} else {
stopline = console.nextLine();
if (stopline.equals("stop"))
break;
}
}
Scanner sc = new Scanner(inputdata);
int[] array = new int[N];
for (int i = 0; i < N; i++)
array[i] = sc.nextInt();
if (N % 2 == 0)
for (int i = (N - 1); i >= 0; i--)
System.out.println(array[i]);
else
for (int i = 0; i < N; i++)
System.out.println(array[i]);
console.close();
sc.close();
}
}