जब भी मैं इसे चलाता हूं, chooseCave()
फ़ंक्शन in.nextInt()
के साथ ठीक काम करता है। जब मैं गुफा चुनते हैं, संदेशों 2-सेकंड के अंतराल पर पॉप अप, और फिर जैसे ही यह वह हिस्सा अतीत हो जाता है, यह मेरे त्रुटि देता है:धागे "मुख्य" java.util में अपवाद। NoSuchElementException
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown Source)
at Dragon.main(Dragon.java:81)
मैं hasNextLine()
और hasNextInt()
की कोशिश की है, और जब मैं main
विधि में while hasNextLine()
का उपयोग करें, मुझे एक और अधिक त्रुटियां मिलती हैं। जब मैं chooseCave()
विधि में while hasNextInt()
का उपयोग करता हूं, तो यह मेरे इनपुट को स्वीकार नहीं करता है।
जब मैं chooseCave()
विधि में if hasNextInt()
उपयोग करें, यह playAgain
स्ट्रिंग के लिए अपने इनपुट hasNextInt()
बूलियन रिटर्न false
स्वीकार नहीं करता है, और सीधे एक और खेल में चला जाता है, लेकिन फिर और यह स्पैम करता "किस गुफा ..." असीम ।
मैं त्रुटियों की रिपोर्ट और जावा-डॉक्स और स्टैक ओवरफ़्लो के माध्यम से इसी तरह की समस्याओं के साथ चला गया हूं। कृपया मदद करे।
import java.util.Scanner;
public class Dragon {
public static void displayIntro() {
System.out.println("You are in a land full of dragons. In front of you, ");
System.out.println("You see two caves. In one cave, the dragon is friendly");
System.out.println("and will share his treasure with you. The other dragon");
System.out.println("is greedy and hungry, and will eat you on sight");
System.out.println(' ');
}
public static int chooseCave() {
Scanner in = new Scanner(System.in);
int cave = 0;
while (cave != 1 && cave != 2) {
System.out.println("Which cave will you go into? (1 or 2)");
cave = in.nextInt();
}
in.close();
return cave;
}
public static void checkCave(int chosenCave) {
System.out.println("You approach the cave...");
try
{
// Sleep at least n milliseconds.
// 1 millisecond = 1/1000 of a second.
Thread.sleep(2000);
}
catch (InterruptedException e)
{
System.out.println("awakened prematurely");
}
System.out.println("It is dark and spooky...");
try
{
// Sleep at least n milliseconds.
// 1 millisecond = 1/1000 of a second.
Thread.sleep(2000);
}
catch (InterruptedException e)
{
System.out.println("awakened prematurely");
}
System.out.println("A large dragon jumps out in front of you! He opens his jaws and...");
try
{
// Sleep at least n milliseconds.
// 1 millisecond = 1/1000 of a second.
Thread.sleep(2000);
}
catch (InterruptedException e)
{
System.out.println("awakened prematurely");
}
double friendlyCave = Math.ceil(Math.random() * 2);
if (chosenCave == friendlyCave) {
System.out.println("Gives you his treasure!");
}
else {
System.out.println("Gobbles you down in one bite!");
}
}
public static void main(String[] args) {
Scanner inner = new Scanner(System.in);
String playAgain = "yes";
boolean play = true;
while (play) {
displayIntro();
int caveNumber = chooseCave();
checkCave(caveNumber);
System.out.println("Do you want to play again? (yes or no)");
playAgain = inner.nextLine();
if (playAgain == "yes") {
play = true;
}
else {
play = false;
}
}
inner.close();
}
}
+1, मैं एक इनपुट समस्या के साथ एकाधिक स्कैनर का उल्लेख करना भूल गया। – syb0rg
आपके लिए धन्यवाद और syb0rg। मुझे बस स्कैनर के साथ विधि को हटाना पड़ा और समकक्ष को मुख्य विधि में रखना पड़ा। यह काम करता हैं! एक नई प्रोग्रामिंग भाषा में पहली सफलता ... वास्तव में अच्छा लगता है। :) –