是什么导致没有此类元素异常 [英] What Causes No Such Element Exception

查看:95
本文介绍了是什么导致没有此类元素异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的代码中,输入单词时,没有此类元素异常.它确实可以正确输出单词和and子手,但这样做后也会崩溃.是什么原因造成的,我该如何解决? 这是错误的开始:

In my code I am getting a no such element exception when I enter a word. It does output the word correctly and the hangman, but it also crashses after doing so. What is causing this and how could I fix it? Here is the start of the error:

Exception in thread "AWT-EventQueue-1" java.util.NoSuchElementException: No line found
    at java.util.Scanner.nextLine(Unknown Source)
    at Hangman.paint(Hangman.java:50)
    at javax.swing.RepaintManager$3.run(Unknown Source)
    at javax.swing.RepaintManager$3.run(Unknown Source) 

是否也很难修改我的代码,以便每次计算机猜测它都绘制了子手的一部分时,而不是在执行程序时全部出现在子手中?

Also would it be hard to modify my code so that each time the computer guesses it draws one part of the hangman instead of it all appearing upon executing the program?

import java.util.Scanner;
import javax.swing.JApplet;
import java.awt.*;

public class Hangman extends JApplet
{
    public void paint (Graphics Page)
    {
                //gallows
                Page.drawLine(0,300,20,300);
                Page.drawLine(10,40,10,300);
                Page.drawLine(10,40,80,40);
                Page.drawLine(80,40,80,55);

                //torso
                Page.drawOval(50,55,50,55);
                Page.drawOval(50,100,50,100);
                //left arm and hand
                Page.drawLine(50,150,40,110);
                Page.drawLine(40,110, 45,100);
                Page.drawLine(40,110, 25,100);
                Page.drawLine(40,110, 25,115);


                //right arm and hand
                Page.drawLine(100,150,120,110);
                Page.drawLine(120,110, 115,95);
                Page.drawLine(120,110, 125,95);
                Page.drawLine(120,110, 135,115);

                //left  leg and foot
                Page.drawLine(80,200,100,250);
                Page.drawLine(100,250, 115,260);


                //right leg and foot
                Page.drawLine(75,200,60,250);
                Page.drawLine(60,250,45,260);




     Scanner in = new Scanner(System.in);
     System.out.println("Enter a 4 or 5 letter word and the computer will play hangman against you!");
     String word = in.nextLine();


     char[] letter = word.toCharArray();


     for (int i = 0; i < letter.length; i++) {
             letter[i] = 'a';
     }


     for (int i = 0; i < word.length(); i++){
         for (int j = 48; j < 122; j++) {

                     if (letter[i] == word.charAt(i)) { 
                             break; 
                     } else {
                             letter[i] = (char)((int) j + 1);
                     }
             }
     }
     System.out.println("Your word is: ");

     for (char letters : letter) {
             System.out.print(letters);
     }
     in.close();
}

}

推荐答案

不要在您的paint方法内关闭in.它关闭基础流,并且下一次尝试从中读取将产生错误.

Do not close in inside your paint method. It closes the underlying stream and the next attempt to read from it produces the error.

关闭与System.in关联的Scanner对象几乎不是一个好主意.

It's hardly ever a good idea to close Scanner object associated with System.in.

文档:何时扫描器已关闭,如果源实现了Closeable接口,它将关闭其输入源."

From the docs: "When a Scanner is closed, it will close its input source if the source implements the Closeable interface."

这篇关于是什么导致没有此类元素异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆