如何阻止我的do while循环接受除“stop”之外的每一个字符串

时间:2022-08-23 01:11:07

Write a main method that asks the user for an integer n which then allocates an array of ints of that size and stores that many integers as the user enters them. Once they are all entered, print them in the reverse order of how they were entered.

编写一个主要方法,询问用户一个整数n,然后整数n分配该大小的整数数组,并在用户输入时存储许多整数。输入完毕后,按照输入方式的相反顺序打印。

This is what I have come up, thus far (any help will be greatly appreciated):

这就是我到目前为止(任何帮助将不胜感激):

import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Array2D {

    public static void main(String[] args){
        Scanner num = new Scanner(System.in);
        ArrayList<Integer> number = new ArrayList<Integer>();
        int ber = 0;
        String stop = "";

        System.out.println("Type as many number as you like, to get in reverse order. When done typing number, type STOP: ");
        do{
            try{
                ber = num.nextInt();
                number.add(ber);
            }
            catch(InputMismatchException e){
                stop = num.nextLine();
                e.equals(stop);
            }




        }while(!(stop.equalsIgnoreCase("stop")));

5 个解决方案

#1


1  

Task from your question is asking you to do something like this (there is no even need to handle case where user writes stop or other incorrect data, but you can handle them if you want).

您的问题中的任务是要求您执行此类操作(甚至不需要处理用户写入停止或其他不正确数据的情况,但您可以根据需要处理它们)。

//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)

#2


2  

Change the catch to :

将捕获更改为:

catch (InputMismatchException e) {
    if(stop.equalsIgnoreCase("stop")){
       continue;
    }
    stop = num.nextLine();
    e.equals(stop);
}

And for printing them in reverse order:

并以相反的顺序打印它们:

for (int i= (number.size()-1); i >= 0; i--) {
    System.out.println(number.get(i));
}

#3


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]);
}

#4


1  

You do not need arraylist for the problem statement. Just the basic code logic using arrays.

问题陈述不需要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]);
    }
}

#5


1  

print them in the reverse order

以相反的顺序打印它们

for (int i = number.size() - 1; i >= 0; i--) {
    System.out.println(number.get(i));
}

#1


1  

Task from your question is asking you to do something like this (there is no even need to handle case where user writes stop or other incorrect data, but you can handle them if you want).

您的问题中的任务是要求您执行此类操作(甚至不需要处理用户写入停止或其他不正确数据的情况,但您可以根据需要处理它们)。

//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)

#2


2  

Change the catch to :

将捕获更改为:

catch (InputMismatchException e) {
    if(stop.equalsIgnoreCase("stop")){
       continue;
    }
    stop = num.nextLine();
    e.equals(stop);
}

And for printing them in reverse order:

并以相反的顺序打印它们:

for (int i= (number.size()-1); i >= 0; i--) {
    System.out.println(number.get(i));
}

#3


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]);
}

#4


1  

You do not need arraylist for the problem statement. Just the basic code logic using arrays.

问题陈述不需要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]);
    }
}

#5


1  

print them in the reverse order

以相反的顺序打印它们

for (int i = number.size() - 1; i >= 0; i--) {
    System.out.println(number.get(i));
}