这是我的代码:
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);
}
}
发布于 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代码:
line 40: System.out.print("Adjective: ");
https://stackoverflow.com/questions/-100006133
复制相似问题