如何使用ENTER退出循环 [英] How to exit loop with ENTER

查看:219
本文介绍了如何使用ENTER退出循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户按下ENTER键时,如何退出此循环。这是我完成的代码的一部分。我在用户按ENTER时如何编码时遇到问题。

How do I exit this loop when user presses ENTER. This is part of the codes I have done. I am having problems in how to code when user presses ENTER.

static String[] itemList = new String[10];

do {
    System.out.print("Enter item (press ENTER to exit) " + (count + 1) + ":  ");
    String item = input.next();
    itemList[count] = item;
    if (item == "")
        count = itemList.length;


推荐答案

您正在比较 String 使用 == 而不是 .equals()

You are comparing Strings using == not .equals().

这将比较指向 String 的指针,而不是 String 的内容。

This compares the pointer to the String, not the contents of the String.

这篇关于如何使用ENTER退出循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆