JAVA如何用txt文件中的数组替换scnr.next(),以避免用户键入数千次?
我注意到用户必须输入很多变量才能完成这项任务。用户需要根据此代码键入90次。我听说我可以使用File,PrintWriter来允许扫描仪读取数组形式的txt内容。我查阅了很多教程,但很难达到我的目标。您介意我帮助我将我的scnr.next()替换为txt文件中的数组吗JAVA如何用txt文件中的数组替换scnr.next(),以避免用户键入数千次?,java,arrays,methods,java.util.scanner,Java,Arrays,Methods,Java.util.scanner,我注意到用户必须输入很多变量才能完成这项任务。用户需要根据此代码键入90次。我听说我可以使用File,PrintWriter来允许扫描仪读取数组形式的txt内容。我查阅了很多教程,但很难达到我的目标。您介意我帮助我将我的scnr.next()替换为txt文件中的数组吗 import java.util.Scanner; public class midtermBc3Choe{ public static void main (String [] args){ int wor
import java.util.Scanner;
public class midtermBc3Choe{
public static void main (String [] args){
int workerNum = 30;
int jobNum = 5;
int monthNum = 6;
System.out.println("There are 2 criteria to consider per job.");
System.out.println("There are" + jobNum + "jobs for a person per month.");
System.out.println("Tell a worker's quality of his job by useing word 'good' or 'bad.'");
double correctEachTask[][][] = new double [workerNum][monthNum][jobNum];
double correctMonthly[][] = new double [workerNum][monthNum];
double correct6Month[] = new double [workerNum];
Scanner scnr = new Scanner(System.in);
for (int i = 0 ; i < workerNum ; i++){//workers
for(int j=0; j< monthNum; j++){//month
for(int k=0; k< jobNum; k++){//jobs
boolean criteria1 = false;
boolean criteria2 = false;
String string1 = scnr.next();
String string2 = scnr.next();
if( string1.equals("good") && string2.equals("good") ){
criteria1= true;
criteria2 = true;
correctEachTask[i][j][k] = 1.0;
}
else if( string1.equals("good") && string2.equals("bad") ){
criteria1 = true;
criteria2 = false;
correctEachTask[i][j][k] = 0.5;
}
else if( string1.equals("bad") && string2.equals("good") ){
criteria1 = false;
criteria2 = true;
correctEachTask[i][j][k] = 0.5;
}
else{
correctEachTask[i][j][k] = 0.0;
}
}//end - task -each
}// end j - month -each
}//end k - workers - each
for(int i= 0; i< workerNum; ++i){
for(int j = 0; j < monthNum; ++j){
double accum =0;
for(int k =0; k < jobNum; ++k){
accum += correctEachTask[i][j][k];
}//for k - monthly avg correct
correctMonthly[i][j]=accum/jobNum;
}//for j- month - monthly avg correct
}// for i - workerNum -monthly avg correct
for(int i =0; i < workerNum; ++i){
double accumMonthlyAvg =0;
for(int j= 0; j < monthNum ; ++j){
accumMonthlyAvg += correctMonthly[i][j];
}// for j - month - 6 month avg correct
correct6Month[i] = accumMonthlyAvg/monthNum;
}// for i - workerNum - 6 month avg correct
System.out.print("workers' monthly average: ");
for (int i =0; i < workerNum; ++i){
for(int j = 0; j < monthNum; ++j){
System.out.print(correctMonthly[i][j]);
}
}//print out montly average
System.out.println(" ");
System.out.print("worker 6 month average: ");
for (int i =0; i < workerNum; ++i){
System.out.print(correct6Month[i]);
}//print out 6 month average
}//main
}//class
这是我得到的
- 用
替换BufferedReader
,读取文件扫描仪
- 循环文件中的所有行,并在循环时将它们附加到
变量StringBuilder
- 完成后,使用
方法获取包含所有单词的数组split
- 现在,您可以循环已获得的数组。数组中的第0个元素将是
,第一个元素将是string1
,在第一个三重嵌套for循环中,依此类推string2
- 请注意,字数为120。您的三重嵌套for循环将运行(30*6*5)=900次。这将导致出现
ArrayIndexOutOfBoundsException
- 此外,您的
和criteria1
未被使用criteria2
public static void main(String[] args) {
int workerNum = 30;
int jobNum = 5;
int monthNum = 6;
System.out.println("There are 2 criteria to consider per job.");
System.out.println("There are" + jobNum + "jobs for a person per month.");
System.out.println("Tell a worker's quality of his job by useing word 'good' or 'bad.'");
double correctEachTask[][][] = new double[workerNum][monthNum][jobNum];
double correctMonthly[][] = new double[workerNum][monthNum];
double correct6Month[] = new double[workerNum];
BufferedReader br = null;//Use BufferedReader instead Scanner.
String rawData = "";//This will store all the text read from the file.
try {
br = new BufferedReader(new FileReader("input.txt"));
//Read the file until the end, then store the text read in rawData.
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line);
sb.append(System.lineSeparator());
line = br.readLine();
}
rawData = sb.toString();//here, convert the StringBuilder to normal String
br.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(Example.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Example.class.getName()).log(Level.SEVERE, null, ex);
}
//Now, we will split the long text whenever there is a space ie " ". We will obtain an array
//that will have all the "words" that you are looking for.
String[] cleanData = rawData.split(" ");
//check everything is read correctly.
for (int i = 0; i < cleanData.length - 1; i++) {
System.out.println(cleanData[i]);
}
//Here, we will create a counter to loop through the array we obtained earlier.
int counter = 0;
for (int i = 0; i < workerNum; i++) {//workers
for (int j = 0; j < monthNum; j++) {//month
for (int k = 0; k < jobNum; k++) {//jobs
boolean criteria1 = false;
boolean criteria2 = false;
//if reached the end of file
//without this if statement, your program will crash.
if (counter >= cleanData.length - 1) {
return;
}
//read the zeroth element
String string1 = cleanData[counter];
counter++;//increase the counter to read the first element
String string2 = cleanData[counter];
counter++;//increase the counter to read the second element once it loops back.
if (string1.equals("good") && string2.equals("good")) {
criteria1 = true;
criteria2 = true;
correctEachTask[i][j][k] = 1.0;
} else if (string1.equals("good") && string2.equals("bad")) {
criteria1 = true;
criteria2 = false;
correctEachTask[i][j][k] = 0.5;
} else if (string1.equals("bad") && string2.equals("good")) {
criteria1 = false;
criteria2 = true;
correctEachTask[i][j][k] = 0.5;
} else {
correctEachTask[i][j][k] = 0.0;
}
}//end - task -each
}// end j - month -each
}//end k - workers - each
for (int i = 0; i < workerNum; ++i) {
for (int j = 0; j < monthNum; ++j) {
double accum = 0;
for (int k = 0; k < jobNum; ++k) {
accum += correctEachTask[i][j][k];
}//for k - monthly avg correct
correctMonthly[i][j] = accum / jobNum;
}//for j- month - monthly avg correct
}// for i - workerNum -monthly avg correct
for (int i = 0; i < workerNum; ++i) {
double accumMonthlyAvg = 0;
for (int j = 0; j < monthNum; ++j) {
accumMonthlyAvg += correctMonthly[i][j];
}// for j - month - 6 month avg correct
correct6Month[i] = accumMonthlyAvg / monthNum;
}// for i - workerNum - 6 month avg correct
System.out.print("workers' monthly average: ");
for (int i = 0; i < workerNum; ++i) {
for (int j = 0; j < monthNum; ++j) {
System.out.print(correctMonthly[i][j]);
}
}//print out montly average
System.out.println(" ");
System.out.print("worker 6 month average: ");
for (int i = 0; i < workerNum; ++i) {
System.out.print(correct6Month[i]);
}//print out 6 month average
}//main
publicstaticvoidmain(字符串[]args){
int workerNum=30;
int-jobNum=5;
int-monthNum=6;
So.Ex.PrtLn(“每个工作有2个标准考虑”);
System.out.println(“每个人每月有“+jobNum+”个工作。”);
System.out.println(“通过使用“好”或“坏”两个词来告诉工人他的工作质量”;
double CorrectiveAchTask[][]=新的double[workerNum][monthNum][jobNum];
双倍每月[][]=新的双倍[工作日][月日];
双精度6个月[]=新双精度[工作日];
BufferedReader br=null;//使用BufferedReader代替扫描仪。
String rawData=“;//这将存储从文件读取的所有文本。
试一试{
br=新的BufferedReader(新的文件读取器(“input.txt”);
//读取文件直到结束,然后将读取的文本存储在rawData中。
StringBuilder sb=新的StringBuilder();
String line=br.readLine();
while(行!=null){
某人附加(行);
sb.append(System.lineSeparator());
line=br.readLine();
}
rawData=sb.toString();//这里,将StringBuilder转换为普通字符串
br.close();
}捕获(FileNotFoundException ex){
Logger.getLogger(例如.class.getName()).log(Level.SEVERE,null,ex);
}捕获(IOEX异常){
Logger.getLogger(例如.class.getName()).log(Level.SEVERE,null,ex);
}
//现在,只要有空格,我们就会分割长文本。我们将获得一个数组
//这将有所有的“话”,你正在寻找。
字符串[]cleanData=rawData.split(“”);
//检查所有内容是否正确阅读。
对于(int i=0;i=cleanData.length-1){
返回;
}
//读取第0个元素
字符串string1=cleanData[计数器];
计数器+++;//增加计数器以读取第一个元素
字符串string2=cleanData[计数器];
计数器+++;//增加计数器以在第二个元素循环后读取它。
if(string1.equals(“好”)和string2.equals(“好”)){
标准1=正确;
准则2=正确;
修正任务[i][j][k]=1.0;
}else if(string1.equals(“好”)&string2.equals(“坏”)){
标准1=正确;
标准2=错误;
修正任务[i][j][k]=0.5;
}else if(string1.equals(“坏”)&string2.equals(“好”)){
标准1=错误;
准则2=正确;
修正任务[i][j][k]=0.5;
}否则{
修正任务[i][j][k]=0.0;
}
}//结束-任务-每个
}//j月底-每个月
}//k端-工人-每个
对于(int i=0;i
So wrap
public static void main(String[] args) {
int workerNum = 30;
int jobNum = 5;
int monthNum = 6;
System.out.println("There are 2 criteria to consider per job.");
System.out.println("There are" + jobNum + "jobs for a person per month.");
System.out.println("Tell a worker's quality of his job by useing word 'good' or 'bad.'");
double correctEachTask[][][] = new double[workerNum][monthNum][jobNum];
double correctMonthly[][] = new double[workerNum][monthNum];
double correct6Month[] = new double[workerNum];
BufferedReader br = null;//Use BufferedReader instead Scanner.
String rawData = "";//This will store all the text read from the file.
try {
br = new BufferedReader(new FileReader("input.txt"));
//Read the file until the end, then store the text read in rawData.
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line);
sb.append(System.lineSeparator());
line = br.readLine();
}
rawData = sb.toString();//here, convert the StringBuilder to normal String
br.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(Example.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Example.class.getName()).log(Level.SEVERE, null, ex);
}
//Now, we will split the long text whenever there is a space ie " ". We will obtain an array
//that will have all the "words" that you are looking for.
String[] cleanData = rawData.split(" ");
//check everything is read correctly.
for (int i = 0; i < cleanData.length - 1; i++) {
System.out.println(cleanData[i]);
}
//Here, we will create a counter to loop through the array we obtained earlier.
int counter = 0;
for (int i = 0; i < workerNum; i++) {//workers
for (int j = 0; j < monthNum; j++) {//month
for (int k = 0; k < jobNum; k++) {//jobs
boolean criteria1 = false;
boolean criteria2 = false;
//if reached the end of file
//without this if statement, your program will crash.
if (counter >= cleanData.length - 1) {
return;
}
//read the zeroth element
String string1 = cleanData[counter];
counter++;//increase the counter to read the first element
String string2 = cleanData[counter];
counter++;//increase the counter to read the second element once it loops back.
if (string1.equals("good") && string2.equals("good")) {
criteria1 = true;
criteria2 = true;
correctEachTask[i][j][k] = 1.0;
} else if (string1.equals("good") && string2.equals("bad")) {
criteria1 = true;
criteria2 = false;
correctEachTask[i][j][k] = 0.5;
} else if (string1.equals("bad") && string2.equals("good")) {
criteria1 = false;
criteria2 = true;
correctEachTask[i][j][k] = 0.5;
} else {
correctEachTask[i][j][k] = 0.0;
}
}//end - task -each
}// end j - month -each
}//end k - workers - each
for (int i = 0; i < workerNum; ++i) {
for (int j = 0; j < monthNum; ++j) {
double accum = 0;
for (int k = 0; k < jobNum; ++k) {
accum += correctEachTask[i][j][k];
}//for k - monthly avg correct
correctMonthly[i][j] = accum / jobNum;
}//for j- month - monthly avg correct
}// for i - workerNum -monthly avg correct
for (int i = 0; i < workerNum; ++i) {
double accumMonthlyAvg = 0;
for (int j = 0; j < monthNum; ++j) {
accumMonthlyAvg += correctMonthly[i][j];
}// for j - month - 6 month avg correct
correct6Month[i] = accumMonthlyAvg / monthNum;
}// for i - workerNum - 6 month avg correct
System.out.print("workers' monthly average: ");
for (int i = 0; i < workerNum; ++i) {
for (int j = 0; j < monthNum; ++j) {
System.out.print(correctMonthly[i][j]);
}
}//print out montly average
System.out.println(" ");
System.out.print("worker 6 month average: ");
for (int i = 0; i < workerNum; ++i) {
System.out.print(correct6Month[i]);
}//print out 6 month average
}//main