我想让用户输入一些竞赛结果(至少2人,最多7人),我觉得我可以使用更短的代码,(我是Java的初学者,只有2个月,所以任何建议都是非常有用的!)谢谢,守则如下:
int entrants = 0;
String[][] runners = new String[2][2];
boolean valid = false; //this is for the initial number of runners question - it will stay false and keep us in the loop until a number between 2-7 is entered
System.out.println("Please enter the number of runners for this race (2 to 7 entrants permitted)");
entrants = input.nextInt();
input.nextLine();
while(valid == false){
if(entrants < 2) {
System.out.println("You need at least 2 runners for a race, please re-enter the number of runners");
entrants = input.nextInt();
input.nextLine();
} else if (entrants > 7) {
System.out.println("You have entered too many runners for this race (max runners is 7). Please re-enter the number of runners");
entrants = input.nextInt();
input.nextLine();
} else {
System.out.printf("You have entered %d runners\n", entrants);
runners = new String[entrants][2];
valid = true;
}
}发布于 2019-12-11 14:53:13
让我们完全重构代码。
我对代码做了一个单一的功能更改:我反复问最初的问题,而不是要求重新输入。这样就更简单了。
// --- only *declare* entrants, don't assign it an initial value that you don't want
int entrants;
// --- get rid of that pesky warning you get if you don't close `Scanner` instances
try (Scanner input = new Scanner(System.in)) {
// --- we'll just use a break statement to make the while loop a lot easier
while(true) {
// --- this is the question that gets (re-) asked
System.out.println("Please enter the number of runners for this race (2 to 7 entrants permitted)");
// --- only now we assign a possible value
// --- note that you normally first test for presence using hasNextInt!
entrants = input.nextInt();
input.nextLine();
// --- nothing wrong with the original if statement
if (entrants < 2) {
System.out.println("You need at least 2 runners for a race.");
} else if (entrants > 7) {
System.out.println("You have entered too many runners for this race (max runners is 7).");
} else {
// --- use a clear comment *why* you break, this is an exit point for the loop after all
// we've found a correct number of entrants
break;
}
}
}
System.out.printf("You have entered %d runners\n", entrants);
// --- there was absolutely no reason to define or initialize the array before...
String[][] runners = new String[entrants][2];当然,要使代码实际上更小,请删除以---开头的注释。
发布于 2019-12-11 14:43:00
你有很多行可以做同样的事情,只要稍作调整就可以合并:
int entrants = 0;
String[][] runners = null; //no need to create an array that you'll be throwing away
boolean valid = false; //this is for the initial number of runners question - it will stay false and keep us in the loop until a number between 2-7 is entered
System.out.println("Please enter the number of runners for this race (2 to 7 entrants permitted)");
do {
//read the input
entrants = input.nextInt();
input.nextLine();
//validate (we'll keep your messages)
if(entrants < 2) {
System.out.println("You need at least 2 runners for a race, please re-enter the number of runners");
} else if (entrants > 7) {
System.out.println("You have entered too many runners for this race (max runners is 7). Please re-enter the number of runners");
} else {
System.out.printf("You have entered %d runners\n", entrants);
//create the array and set the exit condition
runners = new String[entrants][2];
valid = true;
}
} while (!valid);注意do-while的使用,它类似于while-循环,但在迭代后评估条件,即循环体至少运行一次。
进一步的改进可以包括将约束放入常量(即min =2和max = 7),而不是将它们放在所有代码中。
https://codereview.stackexchange.com/questions/234024
复制相似问题