英文:
Outputs correctly but I am getting compiler error
问题
以下是您提供的代码的翻译:
一切都按照应该的方式输出,但我得到了编译器错误。我在 ZyBooks 上做这个。您能告诉我我做错了什么吗?
import java.util.Scanner;
public class Inputkey {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
String inputString = "";
int inputInt = 0;
inputString = scnr.next();
inputInt = scnr.nextInt();
while ( !inputString.equals("Stop")){
if ( inputInt <= 35) {
System.out.println( inputString + ": 需要重新排序");
}
inputString = scnr.next();
inputInt = scnr.nextInt();
}
}
}
我不会回答翻译请求之外的问题。
英文:
Everything is outputting the way it should but I am getting compiler error. I am doing this on ZyBooks. Can you let me know what I am doing wrong?
import java.util.Scanner;
public class Inputkey {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
String inputString = "";
int inputInt = 0;
inputString = scnr.next();
inputInt = scnr.nextInt();
while ( !inputString.equals("Stop")){
if ( inputInt <= 35) {
System.out.println( inputString + ": reorder soon");
}
inputString = scnr.next();
inputInt = scnr.nextInt();
}
}
}
The error I am getting:
Exception in thread "main"
java.util.NoSuchElementException
at java.base/java.util.Scanner.throwFor(Scanner.java:937)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
at Inputkey.main(Inputkey.java:18)
答案1
得分: 1
替代 inputString = scnr.next();
使用 inputString = scnr.nextLine();
Java Scanner 的 next() 和 nextLine() 方法的区别在于,nextLine() 返回文本行中直到回车符的每个字符,而 next() 将该行分割成单个单词,逐个返回单个文本字符串。
import java.util.Scanner;
public class Inputkey {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
String inputString = "";
int inputInt = 0;
inputString = scnr.nextLine();
inputInt = Integer.parseInt(scnr.nextLine());
while (!inputString.equals("Stop")) {
if (inputInt <= 35) {
System.out.println(inputString + ": reorder soon");
}
inputString = scnr.nextLine();
inputInt = Integer.parseInt(scnr.nextLine());
}
}
}
示例输入:
This is stackoverflow
35
stop
999
Stop
scanner.next() 将返回 This
,而 scanner.nextLine() 将返回 This is stackoverflow
。
英文:
instead of inputString = scnr.next();
use inputString = scnr.nextLine();
Java Scanner’s next() and nextLine() methods is that nextLine() returns every character in a line of text right up until the carriage return, while next() splits the line up into individual words, returning individual text Strings one at a time.
import java.util.Scanner;
public class Inputkey {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
String inputString = "";
int inputInt = 0;
inputString = scnr.nextLine();
inputInt = Integer.parseInt(scnr.nextLine());
while ( !inputString.equals("Stop")){
if ( inputInt <= 35) {
System.out.println( inputString + ": reorder soon");
}
inputString = scnr.nextLine();
inputInt = Integer.parseInt(scnr.nextLine());
}
}
}
Sample Input:
This is stackoverflow
35
stop
999
Stop
scanner.next() will returns This
while scanner.nextLine() will returns This is stackoverflow
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论