- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的扫描仪有问题。我希望程序读取字符串和整数。我有两种不同的情况。例如,我的程序要求用户输入出生年份、全名和城市。
Scanner input = new Scanner(System.in);
int year;
String name;
String city;
String state;
System.out.print("Please enter the year of birth to add: ");
year = input.nextInt();
System.out.print("Please enter full name to add: ");
name = input.nextLine();
System.out.print("Please enter a city to add: ");
city = input.nextLine();
System.out.println("Please enter the state to add: );
state = input.next();
输出:
Please enter the year of birth to add: 22
Please enter full name to add: Please enter a city to add: Denver
第一种情况是程序跳过该问题并询问下一个问题。没有nextLine
,它将收到异常。
对于第二种情况,假设如果城市有两个或多个空间(例如旧金山、纽约市...), nextLine
会帮助还是不帮助?看起来和第一种情况很相似。我知道nextLine
将读取带有空格的字符串,但否则仍然会导致问题。我无法解决这个问题。
最佳答案
尝试:
input.nextLine(); //this is totally useless and just eats up the rest of the line
在 nextInt() 之后和 nextLine() 之前。它有效。
问题:在您的代码中,它完全按照您的指示执行。它接收整数,然后继续执行 nextLine()。这是同一行。 nextInt() 不会自动进入下一行。因此,您可以使用 nextLine() 来吃掉最后一行。
解决方案:sc.nextInt() 吃掉整数,但不吃掉新行。如果您选择下一行(与 nextInt() 相同),它将继续查看下一行的真实信息,并且不会跳过问题。
输出:
Please enter the year of birth to add: 199
Please enter full name to add: 1234
Please enter a city to add: 1235123
Please enter the state to add: 1234123
希望这有帮助。
关于Java Scanner 通过读取带有空格的字符串来忽略接下来的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29000420/
我有一个模型,我想为其检索下一条记录和上一条记录。我想通过模型上的 named_scope 来执行此操作,并且还将要返回的下一个/上一个记录的 X 数作为参数传入。 例如,假设我有 5 条记录: 记录
我有以下数据框: print(df) day month year quantity 6 04 2018 10 8
我在这个网站上看到过这样的问题;但他们只是提供生日数据而不提供计数例如:mysql query to get birthdays for next 10 days SQL Select Upcomin
我有以下数据框: print(dd) dt_op quantity product_code 20/01/18 1 613 21/01/18 8
Julia 中的迭代可以通过实现具有 3 个函数的迭代接口(interface)来为新定义的类型实现:start , next , done 我在这些函数的末尾没有看到感叹号,所以根据我对 julia
我正在阅读 clojure 中关于 rest 与 next 的解释。据我了解,它分解为 next 评估/实现序列的尾部,知道它是否应该返回nil,而rest总是返回序列,所以它更懒。 但是,我不明白的
早上好 我有以下数据框: a = [1,2,3,4,5,6] b = pd.DataFrame({'a': a}) 我想创建一个列,对“a”列的下“n”行求和,包括 a 的现值;我试过: n =
编写一个可以迭代任何可迭代对象的通用函数,返回现在,下一对。 def now_nxt(iterable): iterator = iter(iterable) nxt = iterat
我正在尝试显示接下来 5 分钟的剩余时间(捕捉到当前时间的完整 5 分钟,例如 15:05、15:10..) 我能够在下一个小时(而不是分钟)的剩余时间内实现相同的目标: function sec
我想捕获用户在接下来的 2 小时内执行的步数。 这就是我要说的。 Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.H
我有一个如下所示的 DataFrame: ds y 0 2017-02-07 0.154941 1 2017-02-08 0.110595 2
我想通过集合分析计算过去 2 个值的总和 + 当前值 + 接下来的 2 个值。 无论是否使用集合分析,它都按预期工作。无论有无集分析 ( "{}" ),它都按预期工作。但是当缩放时,图形的值会相应地发
我有一个大数据集,低于训练和测试数据集 train_data是从2016-01-29到2017-12-31 head(train_data) date Date_t
另一个“如何删除上一个、下一个、完成按钮”-您可能会想的问题。实际上不是。我对此进行了相当深入的研究,并尝试了不同的方法,但似乎没有任何方法或解决方案能真正做到这一点。下面提到和显示的所有解决方法(就
我是一名优秀的程序员,十分优秀!