首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何解决使用jGRASP时线程“main”中的异常?

如何解决使用jGRASP时线程“main”中的异常?
EN

Stack Overflow用户
提问于 2018-09-18 04:40:41
回答 1查看 0关注 0票数 0

这是我的代码:

代码语言:javascript
复制
import java.util.Scanner;

public class scanner_practice {
public static void main(String[] args) {
// Housekeeping
Scanner kb = new Scanner(System.in);
String word, story;
int intVar;
double dubVar;

// Introduction
System.out.println("Hi there! I have a great story to tell, but I don't remember some of the words.");
System.out.println("Will you help me? Hooray! Thank you! Here we go...");

// Instructions
System.out.println("\nWhen I ask you for a word or number, just type it. I'll handle the rest.");

// Collect values and bulid the story
System.out.print("Verb: ");
word = kb.next();

story = "I am Dr. Pink and I am destined to " + word;

System.out.print("Adjective: ");
word = kb.next();
kb.nextLine(); // Stops the program skipping
story += "the world! Unfortunately the \n" + "world is full of " + word;

System.out.print("Plural noun:  ");
word = kb.nextLine();

story += " " + word;


System.out.print("Adjective: ");
word = kb.next();

story += "that do not agree. So this is \n" + "my " + word;

System.out.print("Adjective: ");
intVar = kb.nextInt();

story += " plan: To start with, I'll need to build my " + intVar;

System.out.print("Adjetive ");
word = kb.next();

story += " hideout in an /n" + "abandoned " + word; 

System.out.print("Company Name: ");
word = kb.next();

story += " complex in the middle of the" + word; 

System.out.print("Type of Place: ");
word = kb.next();
story += ". All \n" + "the " + word; 

System.out.print("Adjective: ");
word = kb.next();
story += "will address me as 'The Great and " + word; 

System.out.print("Occupation (plural): ");
word = kb.next();
kb.nextLine(); // Stops the program skipping
story += " Dr. Pink.' I will \n" + "hire some  " + word; 

System.out.print("Plural event): ");
word = kb.nextLine();
story += "to design a machine to trigger \n" + "a huge  " + word; 
System.out.print("Plural noun: ");
word = kb.next();
story += " whenever I want it to. And if they don't want to build it, I \n" + "will capture their " + word; 

System.out.print("Something you hate doing: ");
word = kb.next();
story += " and threaten to make them " + word + "for hours on end. \n" + "Muwahahahahaha!"; 


// Print out the story
System.out.println(story);
}

}
EN

回答 1

Stack Overflow用户

发布于 2018-09-18 14:07:40

学习如何排除错误几乎与学习如何编码一样重要:-)

A.首先查看InputMismatchException的文档,了解发生的时间和原因:

由扫描程序抛出,表示检索到的令牌与预期类型的模式不匹配,或者令牌超出预期类型的​​范围。

B.查看完整的堆栈跟踪。跟踪提供有关错误的上下文,例如它出现的行以及调用的方法:

...在java.util.Scanner中。nextInt(Scanner.java:2076) 在scanner_practice.main(scanner_practice.java:41

C.查看错误行附近的java代码:

代码语言:javascript
复制
    line 40: System.out.print("Adjective: "); 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100006133

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档