首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何允许用户使用java在Array中输入结果?

如何允许用户使用java在Array中输入结果?
EN

Code Review用户
提问于 2019-12-11 14:29:46
回答 2查看 27关注 0票数 1

我想让用户输入一些竞赛结果(至少2人,最多7人),我觉得我可以使用更短的代码,(我是Java的初学者,只有2个月,所以任何建议都是非常有用的!)谢谢,守则如下:

代码语言:javascript
复制
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;
    }
}
EN

回答 2

Code Review用户

回答已采纳

发布于 2019-12-11 14:53:13

让我们完全重构代码。

我对代码做了一个单一的功能更改:我反复问最初的问题,而不是要求重新输入。这样就更简单了。

代码语言:javascript
复制
// --- 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];

当然,要使代码实际上更小,请删除以---开头的注释。

票数 1
EN

Code Review用户

发布于 2019-12-11 14:43:00

你有很多行可以做同样的事情,只要稍作调整就可以合并:

代码语言:javascript
复制
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),而不是将它们放在所有代码中。

票数 1
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/234024

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档