初学者,不确定是代码问题还是软件安装错误。Java程序的类中多个方法有输入scanner,main运行异常。删去所有方法的in.close()后可以运行。图一是保留了in.close()的运行结果,图二为把所有in.close()注释掉的结果。软件是myeclipse2016
package exp1;
public class OperaMain {
public static void main(String[] args) {
//new DoffodilNum();
//new PrimeNum();
SwitchScore A=new SwitchScore();
A.setScore();
//A.turnLevel();
//A.getLevel();
RankArry B=new RankArry();
B.setArryLen();
B.setArry();
//B.getArry(1);
//B.setRank();
B.getOutWay();
}
}
package exp1;
import java.util.Random;
import java.util.Scanner;
//随机数
public class RandomNum {
int arry[];
int n;
//输入数组长度
public void setArryLen(){
System.out.println("输入一个整数n决定随机数的生成数量:");
Scanner in=new Scanner(System.in);
n=in.nextInt();
this.arry=new int[n];
in.close();
}
//生成随机数数组
public void setArry(){
int a;
Random r = new Random();
for(a=0;a<n;a++){
this.arry[a]=1+ r.nextInt(999);
}
}
//输出方式
public void getOutWay(){
//输入选项
int y;
do{
System.out.println("1-从小到大输出\t2-从大到小输出");
System.out.println("输入一个整数y决定排序方式:");
Scanner in=new Scanner(System.in);
y=in.nextInt();
in.close();
}while(y!=1&&y!=2);
this.getArry(y);
}
//输出随机数数组
public void getArry(int x){
int a;
System.out.print("当前数组:");
if(x==1){
for(a=0;a<n;a++){
System.out.print(this.arry[a]+" ");
}
}
else if(x==2){
for(a=n-1;a>-1;a--){
System.out.print(this.arry[a]+" ");
}
}
System.out.print("\n");
}
}
package exp1;
import java.util.Scanner;
//排序
public class RankArry extends RandomNum{
int time;
public void setRank(){
//输入选项
int m;
System.out.println("1-插入排序\t2-起泡排序\t3-选择排序\t4-快速排序");
System.out.println("输入一个整数m决定排序方式:");
Scanner in=new Scanner(System.in);
m=in.nextInt();
in.close();
//条件选择
switch(m){
case 1:this.sortInsertion();break;
case 2:this.sortBubble();break;
case 3:this.sortSelection();break;
case 4:this.sortQuick(0,n-1);break;
}
}
public void sortInsertion(){
int t;
int k;
for(int i=1;i<this.arry.length;i++){
t=this.arry[i];
k=i-1;
for(;k>-1;k--){
if(t<this.arry[k]){
this.arry[k+1]=this.arry[k];
}
else
break;
}
if(t!=this.arry[i])
this.arry[k+1]=t;
}
}
public void sortBubble(){
int t;
for(int i=1;i<this.arry.length;i++){
for(int j=0;j<this.arry.length-i;j++){
if(this.arry[j]>this.arry[j+1]){
t=this.arry[j];
this.arry[j]=this.arry[j+1];
this.arry[j+1]=t;
}
}
}
}
public void sortSelection(){
int t;
int k;
for(int i=0;i<this.arry.length;i++){
k=i;
for(int j=i+1;j<this.arry.length;j++){
if(this.arry[k]>this.arry[j]){
k=j;
}
}
if(k!=i){
t=this.arry[k];
this.arry[k]=this.arry[i];
this.arry[i]=t;
}
}
}
public void sortQuick(int left,int right){
int t;
if(left<right){
int temp=this.arry[left];
int x=left;
int y=right;
while(x<y){
while(x<y&&temp<=this.arry[y]){
y--;
}
while(x<y&&temp>=this.arry[x]){
x++;
}
if(x<y){
t=this.arry[x];
this.arry[x]=this.arry[y];
this.arry[y]=t;
}
}
this.arry[left]=arry[x];
this.arry[x]=temp;
this.sortQuick(left, x);
this.sortQuick(x+1, right);
}
}
public void timeStart(){
}
public void timeEnd(){
}
}
package exp1;
import java.util.Scanner;
public class SwitchScore {
int score;
char level;
public void setScore(){
int i;
do{
System.out.println("输入一个百分制整数:");
Scanner in=new Scanner(System.in);
i=in.nextInt();
in.close();
}while(i<0||i>100);
this.score=i;
}
public void turnLevel(){
switch(this.score/10)
{
case 10:
case 9:this.level='A';break;
case 8:this.level='B';break;
case 7:this.level='C';break;
case 6:this.level='D';break;
case 5:case 4:case 3:case 2:case 1:
case 0:this.level='E';break;
}
}
public void getLevel(){
System.out.println("对应等级为"+this.level);
}
}