java - 区分 String/Int 用户输入

标签 java arrays string int

我在尝试键入代码以使我的程序区分用户输入的是字符串值还是 int 值时遇到问题。如果输入 int 值,它将存储到一个数组(命名数据)中,可以通过输入字符串值来打印和测试该数组(如下面方法 go() 中的代码所示。Am我错误地使用了 .hasNextInt?这是我的代码:

import java.util.Scanner;
import java.util.Random;
import java.text.DecimalFormat;

public class IntegerStatistics {

  java.util.Scanner scan;

  // declare storage for the integers
  int[] data;
  Random random;

  // create a constructor
  public IntegerStatistics() {
    scan = new Scanner(System.in);
    data = new int[10];
    random = new Random();

  }

  private void showMenu() {
    System.out.println("Menu:");
    System.out.println("   p - Print the list of values");
    System.out.println("   s - Print statistics for the values");
    System.out.println("   f - Fill the list with random values");
    System.out.println("   c - Clear the list of values");
    System.out.println("   h - Print out this menu");
    System.out.println("   x - Exit the program");
  }

  private void clearValues() {
    System.out.print("The values: [0");
    // empty (zero out) the array
    int i = 1;    
    while(i < data.length) {
      System.out.print(", 0");
      i++;
    } System.out.println("]");
  }

  private void fillList() {
    data[0] = (random.nextInt(26) - 10);
    System.out.print("The values: [" + data[0]);
    for(int i = 1; i < data.length; i++) {
      data[i] = (random.nextInt(26) - 10);
      System.out.print(", " + data[i]);
    } System.out.println("]");
  }

  private void printValues() {
    data[0] = 1;
    System.out.print("The values: [" + data[0]);
    // print the values
    for(int i = 1; i < data.length; i++) {
      data[i] = i + 1;
      System.out.print(", " + data[i]);
    } System.out.println("]");
  }

  private void printStats() {
    int sum = 0;
    int max = data[0];
    int min = data[0];
    // calculate the stat values of the array
    for(int i = 0; i < data.length; i++) {
      // calculate sum of values
      sum += data[i];
      // find maximum value in array
      if(data[i] > max) {
        max = data[i];
      // find minumum value in array
      } else if(data[i] < min) {
        min = data[i];
      } 
    } 
    // caculate average of values
    DecimalFormat df = new DecimalFormat("#.000");
    double avgValue = (sum / ((double)data.length));
    // print stat values of array
    System.out.println("Sum of the values: " + sum);
    System.out.println("Maximum value: " + max);
    System.out.println("Minimum value: " + min);
    System.out.printf("Average value: " + df.format(avgValue) + "\n");
  }

  public void go() {
    System.out.println("Welcome to Simple Statistics Program\n");
    String input;
    int inputNum = Integer.parseInt(s);
    showMenu();
    int index = 0;
    do {
      System.out.print("Enter a command or integer: ");
      input = scan.next();
      inputNum = scan.nextInt();
      if(inputNum.hasNextInt()) {
        data[index] = input.hasNexInt();
      } else if(input.equals("p")) {
        printValues();
      } else if(input.equals("s")) {
        printStats();
      } else if(input.equals("f")) {
        fillList();
      } else if(input.equals("c")) {
        clearValues();
      } else if(input.equals("h")) {
        showMenu();
      } else if(input.equals("x")) {
     // do nothing
      } else {
        System.out.println("Unrecognized command. Try again.");
        showMenu();
      }
    } while( ! input.equals("x"));
    System.out.println("\nThank you for using the Simple Statistics Program");
  }

  public static void main(String[] args) {
    new IntegerStatistics().go();
  }

}

通过输入int创建的数组可以部分填充,但长度不能超过10个整数。让我知道我需要澄清。

最佳答案

你可以定义一个字符串并尝试转换为整数,如果它抛出 NumberFormatException 是因为它不是数字。

关于java - 区分 String/Int 用户输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16513420/

相关文章:

java - 如何只创建一次实例变量?

java - 错误: JPA + MySQL Transaction is not active in the current thread

java - java 统计元素出现的频率

java - OutOfBoundsException 问题,Java

java - 选择排序返回不在我的数组中的零

php - mysql_fetch_array() 不显示所有结果

javascript - 从 Javascript 数组中删除项目并显示新数组

java - System.out.print() 不会向 Eclipse 控制台发送任何输出。为什么?

objective-c - 为什么在 Objective-C 中的字符串之前使用 '@'?

arrays - 如果我不关心特定编码,如何将 u8 切片打印为文本?