【问题标题】:How do I enter a char as input to break out of a loop that is looking for an integer?如何输入 char 作为输入以跳出正在寻找整数的循环?
【发布时间】:2018-11-17 07:06:16
【问题描述】:

我有这个程序从输入文件中读取数字,然后要求用户输入一个数字。然后程序读取文件中的所有数字,如果输入的数字在文件中,程序打印回,“数字在文件中”,如果输入的数字不在文件中,程序打印回来,“数字不是在档案中。”如果用户输入'q',我还需要让它能够退出程序,但我不确定我该怎么做,看到'q'char 并且程序正在寻找输入int。我确实创建了一个变量char quit = 'q',但我不确定在哪里使用它,如果这甚至是正确的开始方式。

package classwork7_2;
import java.util.*;
import java.io.*;

public class ClassWork7_2 {

public static void main(String[] args)throws IOException {

    Scanner s = new Scanner(System.in);

    int[] numbers = fileToArray();
    Arrays.sort(numbers);
    char quit = 'q';


    while (true) {
    System.out.print("Enter a number in the file: ");
    int numb = s.nextInt();
    int i = Arrays.binarySearch(numbers, numb);
    if (i < 0) {
        System.out.print("Number is not in file\n");
    } else {
        System.out.print("Number is in file\n");

    }
}


}

public static int[] fileToArray() throws IOException{

    Scanner s = new Scanner(System.in);
    int[] array = new int[7];

    System.out.print("Enter name of file: ");
    String filename = s.nextLine();

    File f = new File(filename);
    Scanner inputFile = new Scanner(f);
    int i = 0;

    while(inputFile.hasNext()){

       array[i] = inputFile.nextInt();
       i++;
    }
        inputFile.close();
        return array;
}
}

【问题讨论】:

  • 不要使用 nextInt(),也许使用:int numb; String str = s.nextLine(); if (str,charAt(0) == 'q') { break; } if (!str.matches("\\d+") { System.out.println("Invalid Entry!"); continue; } numb = Integer.parseInt(str); // the reset of your code
  • 好的方法是用户 Scanner.hasNextXXX 方法,如我的回答中所述。

标签: java arrays loops


【解决方案1】:

你可以像下面这样改变while循环:

这里,不是读取nextInt,而是读取string,如果不是q,则转换为int

while (true) {

        System.out.print("Enter a number in the file: ");
        String ln = s.nextLine();
        if("q".equals(ln)) {
            break;//exiting as user entered "q"
        }
        int numb  = Integer.parseInt(ln);
        int i = Arrays.binarySearch(numbers, numb);
        if (i < 0) {
            System.out.print("Number is not in file\n");
        } else {
            System.out.print("Number is in file\n");

        }

    }

【讨论】:

    【解决方案2】:
    Scanner s = new Scanner(System.in);    
    String userInput = s.next();
    int result = Integer.parseInt(userInput);
    

    您可以将原始数字输入作为字符串,然后将它们解析为整数,并在使用二进制搜索时使用该结果。

    【讨论】:

      【解决方案3】:

      检查扫描仪是否有 int 或字母 'q':

          while (true) {
              System.out.print("Enter a number in the file: ");
              if (s.hasNextInt()) {
                  int numb = s.nextInt();
                  int i = Arrays.binarySearch(numbers, numb);
                  if (i < 0) {
                      System.out.print("Number is not in file\n");
                  } else {
                      System.out.print("Number is in file\n");
                  }
              }
      
              if (s.hasNext("q")) {
                  return;
              }
          }
      

      【讨论】:

        猜你喜欢
        • 2021-07-25
        • 2020-08-15
        • 2019-02-27
        • 1970-01-01
        • 2012-07-13
        • 2016-11-19
        • 2014-02-19
        • 1970-01-01
        相关资源
        最近更新 更多