Java简单问题

时间:2017-05-07 04:46:33

标签: java eclipse

我保持疯狂我只想在Java终端阅读一些年龄时使用循环

public static void main(String[] args){

    Scanner keyboard = new Scanner(System.in);

    int lenghtMax = 100;

    int[] age = new int[lenghtMax];
    int status = 1;
    String next; 
    int i = 0;

    while(status == 1){

        if (i >= 100){

            System.out.println("You cant register more ages!\n");

        } else {                

            System.out.println("What the age?");
            age[i] = keyboard.nextInt();
            i++;

            System.out.println("Do you want register other age? y/n");
            next = keyboard.next();

            if (next == "y"){

                status = 1;

            } else {

                status = 0;
                keyboard.close();

            }

        }

    }   

    System.out.println("Passing Here!");
}

这是我的代码,错误在哪里,因为它打印了该消息!

输出

What the age?
45
Do you want register other age? y/n
y
Passing Here!

为什么打印那条消息?,代码应该在while循环中恢复,我的同时是正常的!

1 个答案:

答案 0 :(得分:2)

我猜你是Java世界的新手。 Java中的字符串比较应该使用equals(Object o)equalsIgnoreCase(String another)方法,而不是==有多种更简单,更好的方法可以实现您的尝试去做。 注意:使用java.util.Scanner接受用户输入。

一个:如果您使用int数组来存储值 -

int lengthMax = 100;
int[] ages = new int[lengthMax];
Scanner sc = new Scanner(System.in);
        /*
         * Just iterate through the length of the array
         */
        for(int i = 0; i < lengthMax; i++) {
            System.out.println("What is the age?");
           /*
            * Read the input and add the same to array
            * at position 'i'.
            */
            ages[i] = Integer.parseInt(sc.nextLine());

           /*
            * Read the choice form the user
            */
            System.out.println("Do you want to register more age? y / n");
            String choice = sc.nextLine();
           /*
            * If choice is a YES - y
            * simply continue with the loop
            */
            if(choice.equals("y")){
                continue;
            } else {
            // If choice is anything other than 'y', break out
                break;
            }
        }
//close the Scanner
sc.close();

二:如果您使用Collection ArrayList来存储年龄 -

int lengthMax = 100;
List<Integer> agesList = new ArrayList<>();
Scanner sc = new Scanner(System.in);
/*
 * Create an infinite loop
 */
while(true) {
          /*
           * At each run, check if the size of the ArrayList is
           * equal to the max length of ages defined.
           *
           * If yes, simply print your message and break out.
           */
            if(agesList.size() == lengthMax) {
                System.out.println("You can't register more ages!!");
                break;
            }

           /*
            * Start accepting the ages, and add each to the list.
            */
            System.out.println("What is the age?");
            agesList.add(Integer.parseInt(sc.nextLine()));

            /*
             * Ask if user is still interested to register ages.
             */
            System.out.println("Do you want to register more age? y / n");
            String choice = sc.nextLine();

            // If yes, continue with the loop, else simply break out.
            if(choice.equals("y")){
                continue;
            } else {
                break;
            }
        }
sc.close();

我希望这会在你的学习过程中帮助你。

相关问题