关于Scanner的用法问题

leonrd 2013-05-20 03:44:02
程序源代码如下,报错是在getStartNum()方法,有两个方法都用到Scanner获得参数,第一个方法能够获得,到第二个就不行了。错误信息放在下面。
package zeng.javatest;

import java.util.Scanner;

public class Josephus {

private int getCrimerNum() {
int crimerNum = 0;
System.out.println("输入犯人的数量:");
Scanner sc = new Scanner(System.in);
crimerNum = sc.nextInt();
sc.close();
return crimerNum;
}

private int getStartNum() {
int startNum = 0;
System.out.println("输入从几号开始计数:");
Scanner sc = new Scanner(System.in);
startNum = sc.nextInt();
sc.close();
return startNum;
}

private int getCount() {
int counter = 0;
System.out.println("输入一个count:");
Scanner sc = new Scanner(System.in);
counter = sc.nextInt();
sc.close();
return counter;
}

private int[] init(int n) {
int a[] = new int[n];
for (int i = 0; i < a.length; i++) {
a[i] = i + 1;
}
return a;
}

private boolean countCrimerNum(int a[]) {
int count = 0;
for (int i = 0; i < a.length; i++) {
if(a[i] == 0){
count ++;
}
}
if (count < (a.length - 1)) {
return true;
}
else return false;
}

private int[] killCrimer(int a[], int s, int d) {
int index = 0;
int n = a.length;
index = (s+d)%n;
a[index] = 0;
System.out.println("第"+(index + 1)+"个犯人被处刑");
return a;
}

public static void main(String[] args) {
int n = 0, s = 0, d = 0;
Josephus jj = new Josephus();
n = jj.getCrimerNum();
s = jj.getStartNum();
d = jj.getCount();
int crimer[] = new int[n];
crimer = jj.init(n);
while(jj.countCrimerNum(crimer)){
crimer = jj.killCrimer(crimer, s, d);
s = (s + d)%n;
}
}

}


eclipse下的报错信息:

输入犯人的数量:
5
输入从几号开始计数:
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at zeng.javatest.Josephus.getStartNum(Josephus.java:20)
at zeng.javatest.Josephus.main(Josephus.java:71)
...全文
132 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
ChoiTony 2013-11-04
  • 打赏
  • 举报
回复
那全局变量怎么定义和使用吗?
oh_Maxy 2013-05-20
  • 打赏
  • 举报
回复
引用 2 楼 leonrd 的回复:
问题解决 , 谢谢了。不明白为什么必须要用全局变量呢?
sc.close();会将System.in这个输入流关闭,这个输入流是public final static InputStream in = nullInputStream(); 前面的将这个in 设置为null,后面再操作,就没有输入流了。
wandefajin 2013-05-20
  • 打赏
  • 举报
回复
用全局变量 Scanner sc = new Scanner(System.in); 这就是一个工具,举个例子 你用myeclipse 敲代码 需要创建一个类 打开一次myeclipse嘛 你是不是只要把myeclipse打开 调用它的功能new一个class文件就可以了,而不用去没创建一个类真的打开一次myeclipse吧!!! 而且这样的话特别占用内存! 就是这个意思!!
leonrd 2013-05-20
  • 打赏
  • 举报
回复
问题解决 , 谢谢了。不明白为什么必须要用全局变量呢?
菖蒲老先生 2013-05-20
  • 打赏
  • 举报
回复
用全局变量。。

import java.util.Scanner;

public class Josephus {
	
	static Scanner sc = null;

	private int getCrimerNum() {
		int crimerNum = 0;
		System.out.println("输入犯人的数量:");
		//Scanner sc = new Scanner(System.in);
		crimerNum = sc.nextInt();
		//sc.close();
		return crimerNum;
	}

	private int getStartNum() {
		int startNum = 0;
		System.out.println("输入从几号开始计数:");
		//Scanner sc = new Scanner(System.in);
		startNum = sc.nextInt();
		//sc.close();
		return startNum;
	}

	private int getCount() {
		int counter = 0;
		System.out.println("输入一个count:");
		//Scanner sc = new Scanner(System.in);
		counter = sc.nextInt();
		//sc.close();
		return counter;
	}

	private int[] init(int n) {
		int a[] = new int[n];
		for (int i = 0; i < a.length; i++) {
			a[i] = i + 1;
		}
		return a;
	}

	private boolean countCrimerNum(int a[]) {
		int count = 0;
		for (int i = 0; i < a.length; i++) {
			if (a[i] == 0) {
				count++;
			}
		}
		if (count < (a.length - 1)) {
			return true;
		} else
			return false;
	}

	private int[] killCrimer(int a[], int s, int d) {
		int index = 0;
		int n = a.length;
		index = (s + d) % n;
		a[index] = 0;
		System.out.println("第" + (index + 1) + "个犯人被处刑");
		return a;
	}

	public static void main(String[] args) {
		sc = new Scanner(System.in);
		int n = 0, s = 0, d = 0;
		Josephus jj = new Josephus();
		n = jj.getCrimerNum();
		s = jj.getStartNum();
		d = jj.getCount();
		int crimer[] = new int[n];
		crimer = jj.init(n);
		while (jj.countCrimerNum(crimer)) {
			crimer = jj.killCrimer(crimer, s, d);
			s = (s + d) % n;
		}
	}

}

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧