HackerRank 扫描仪 Class Java
HackerRank Scanner Class Java
此代码用于打印链表并在 Eclipse 中正常工作:-
import java.util.Scanner;
public class HackerAss {
static Scanner scan;
static Node head;
static Node current;
public static void main(String[] args) {
int q=1,count=0;
while(q==1){
scan = new Scanner(System.in);
q = scan.nextInt();
if(q==0)
break;
else{
int element = scan.nextInt();
if(count == 0)
current = new Node(element);
else{
head = new Node(element);
head.node = current;
current = new Node(element);
}
}
count++;
}
while(head!=null){
System.out.println(head.data);
head=head.node;
}
}
static class Node{
int data;
Node node;
public Node() {
data=0;
node=null;
}
public Node(int data) {
this.data=data;
node=null;
}
}}
但是在 HackerRank 中它显示错误:-
Exception in thread "main" java.util.NoSuchElementException
错误的原因是什么,解决方法是什么。我有作业要提交。
这个错误来自 nextInt()。您必须在代码
中使用以下条件来避免这种情况
import java.util.Scanner;
public class HackerAss {
static Scanner scan;
static Node head;
static Node current;
public static void main(String[] args) {
int q=1,count=0;
scan = new Scanner(System.in);
while(q==1){
if(scan.hasNextInt() ) {
q = scan.nextInt();
}
if(q==0)
break;
else{
if(scan.hasNextInt() ) {
int element = scan.nextInt();
}
if(count == 0)
current = new Node(element);
else{
head = new Node(element);
head.node = current;
current = new Node(element);
}
}
count++;
}
while(head!=null){
System.out.println(head.data);
head=head.node;
}
}
static class Node{
int data;
Node node;
public Node() {
data=0;
node=null;
}
public Node(int data) {
this.data=data;
node=null;
}
}}
您的问题来自您的代码,而不是您的 IDE。
首先,您应该定义一次扫描仪:
while(q==1){
scan = new Scanner(System.in);
q = scan.nextInt();
...
}
这真是个坏主意,因为你甚至不知道流中是否有东西,你不得不像这样使用扫描器:
scan = new Scanner(System.in);
while(scan.hasNextInt()){
q = scan.nextInt();
...
}
之后,java.util.NoSuchElementException
来自您两次调用 nextInt() 的事实:
scan = new Scanner(System.in);
q = scan.nextInt();
...
int element = scan.nextInt();
在文档 nextInt()
中,当输入耗尽时抛出一个 java.util.NoSuchElementException
:https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextInt%28%29
此代码用于打印链表并在 Eclipse 中正常工作:-
import java.util.Scanner;
public class HackerAss {
static Scanner scan;
static Node head;
static Node current;
public static void main(String[] args) {
int q=1,count=0;
while(q==1){
scan = new Scanner(System.in);
q = scan.nextInt();
if(q==0)
break;
else{
int element = scan.nextInt();
if(count == 0)
current = new Node(element);
else{
head = new Node(element);
head.node = current;
current = new Node(element);
}
}
count++;
}
while(head!=null){
System.out.println(head.data);
head=head.node;
}
}
static class Node{
int data;
Node node;
public Node() {
data=0;
node=null;
}
public Node(int data) {
this.data=data;
node=null;
}
}}
但是在 HackerRank 中它显示错误:-
Exception in thread "main" java.util.NoSuchElementException
错误的原因是什么,解决方法是什么。我有作业要提交。
这个错误来自 nextInt()。您必须在代码
中使用以下条件来避免这种情况import java.util.Scanner;
public class HackerAss {
static Scanner scan;
static Node head;
static Node current;
public static void main(String[] args) {
int q=1,count=0;
scan = new Scanner(System.in);
while(q==1){
if(scan.hasNextInt() ) {
q = scan.nextInt();
}
if(q==0)
break;
else{
if(scan.hasNextInt() ) {
int element = scan.nextInt();
}
if(count == 0)
current = new Node(element);
else{
head = new Node(element);
head.node = current;
current = new Node(element);
}
}
count++;
}
while(head!=null){
System.out.println(head.data);
head=head.node;
}
}
static class Node{
int data;
Node node;
public Node() {
data=0;
node=null;
}
public Node(int data) {
this.data=data;
node=null;
}
}}
您的问题来自您的代码,而不是您的 IDE。
首先,您应该定义一次扫描仪:
while(q==1){
scan = new Scanner(System.in);
q = scan.nextInt();
...
}
这真是个坏主意,因为你甚至不知道流中是否有东西,你不得不像这样使用扫描器:
scan = new Scanner(System.in);
while(scan.hasNextInt()){
q = scan.nextInt();
...
}
之后,java.util.NoSuchElementException
来自您两次调用 nextInt() 的事实:
scan = new Scanner(System.in);
q = scan.nextInt();
...
int element = scan.nextInt();
在文档 nextInt()
中,当输入耗尽时抛出一个 java.util.NoSuchElementException
:https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextInt%28%29