java项目中外接扫描仪无法使用,扫描仪在Blue J中不起作用(java)

博主在使用Java Scanner从用户获取矩阵尺寸时遇到问题,程序提示输入但无法接收。通过创建简单测试程序发现输入问题。可能原因包括方法结构或可能是代码bug。尝试解答中提到不必关闭Scanner,因为nextInt()后会自动保存输入。
摘要由CSDN通过智能技术生成

So I have to create matrix of a certain size specified from the user. To do this I was using a Scanner and was going to take two int values, and use those to create the matrix. Long story short, when I ran it, it opened up the terminal window and said "Enter number: " just like I programmed it to, but it wouldn't let me enter a number. I investigated by making a simple program that should take in an integer and output that number, and the results were the same. Any suggestions?

import java.util.*;

public class scanner

{

public void scanner()

{

Scanner a = new Scanner(System.in);

System.out.println("Enter number: ");

int number = a.nextInt();

a.close();

System.out.print(number);

}

}

When I run this, it opens up the terminal window, says "Enter number: " but does not have the blinking cursor indicating that I can type, even when I click on it. I inserted this code into JGrasp and it worked just fine. Any help at all?

解决方案

the problem might be from having your code inside a method instead of a main method or it could just be a bug

side note though: once you type the integer for a.nextInt() and hit enter, it should set int number to that value so I don't believe that the a.close() is necessary because it will move on to printing the integer regardless

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值