首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

关于Scanner的用法有关问题

2013-06-25 
关于Scanner的用法问题程序源代码如下,报错是在getStartNum()方法,有两个方法都用到Scanner获得参数,第一

关于Scanner的用法问题
程序源代码如下,报错是在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)
[解决办法]
用全局变量。。

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;
}
}

}


[解决办法]
用全局变量
Scanner sc = new Scanner(System.in);

这就是一个工具,举个例子  你用myeclipse 敲代码 需要创建一个类  打开一次myeclipse嘛

你是不是只要把myeclipse打开 调用它的功能new一个class文件就可以了,而不用去没创建一个类真的打开一次myeclipse吧!!!

而且这样的话特别占用内存!

就是这个意思!!关于Scanner的用法有关问题

[解决办法]

问题解决 , 谢谢了。不明白为什么必须要用全局变量呢?


sc.close();会将System.in这个输入流关闭,这个输入流是public final static InputStream in = nullInputStream();

前面的将这个in 设置为null,后面再操作,就没有输入流了。

热点排行
Bad Request.