编写一个主要方法,要求用户输入一个整数
@curred_user = User.find(params[:username]) @users = User.find_by_sql(["SELECT * FROM table WHERE username <> ?",@current_user.username ])
,然后分配该大小的整数数组,并在用户输入时存储许多整数。输入完毕后,按照输入方式的相反顺序打印。
到目前为止,我已经提出了这个问题(非常感谢任何帮助):
n
答案 0 :(得分:2)
将捕获更改为:
catch (InputMismatchException e) {
if(stop.equalsIgnoreCase("stop")){
continue;
}
stop = num.nextLine();
e.equals(stop);
}
以相反的顺序打印它们:
for (int i= (number.size()-1); i >= 0; i--) {
System.out.println(number.get(i));
}
答案 1 :(得分:2)
int n = 0;
Scanner num = new Scanner(System.in);
System.out.println("How many numbers do you want to display: ");
n = num.nextInt();
int[] number = new int[n];
System.out.println("Enter the numbers: ");
for(int i = 0; i<n; i++){
number[i] = num.nextInt();
}
System.out.println("Here they are in reverse! ");
for(int i = n-1; i >= 0; --i){
System.out.println(number[i]);
}
答案 2 :(得分:1)
问题陈述您不需要arraylist。只是使用数组的基本代码逻辑。
void print reverse(int n){
int[] arr = new int[n];
Scanner sc = new Scanner(System.in);
for (int i = 0; i < n; ++i){
arr[i] = sc.nextInt();
}
for (int i = n-1; i >= 0; --i){
System.out.println(arr[i]);
}
}
答案 3 :(得分:1)
以相反的顺序打印它们
for (int i = number.size() - 1; i >= 0; i--) {
System.out.println(number.get(i));
}
答案 4 :(得分:1)
你的问题中的任务是要求你做这样的事情(甚至不需要处理用户写stop
或其他不正确数据的情况,但你可以根据需要处理它们。
//ask user for array size
int n = getInt();
//create array for n integers
int[] array = new int[n];
//read n integers from user
for (i in 0..n-1)
tmp = readInt()
//and store them in array
put tmp in array at position i
//print content of array backwards
for (i in n-1..0)
print(i-th element from array)