2013-05-13 121 views
0

我有问题试图键入代码,这将使我的程序区分用户输入的字符串值还是int值。如果输入一个int值,它将被存储到一个数组中(命名为data),这个数组可以通过键入字符串值来打印和测试(如方法go()中的代码所示。我是否错误地使用了.hasNextInt?这是我的代码:区分字符串/ Int用户输入

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的输入创建的阵列可部分填充,但不得超过十个整数的长度让我知道需要我澄清

+0

编译此代码时会发生什么?你有任何错误? – 2013-05-13 00:36:56

+0

这些是我得到的错误:找到2个错误: File:C:\ Users \ Desktop \ IntegerStatistics.java [line:94] 错误:方法nextInt()未定义为类型java.lang.String 文件:C:\ Users \ Desktop \ IntegerStatistics.java [line:95] 错误:方法hasNexInt()未定义为类型java.lang.String – Saffioti 2013-05-13 00:42:01

+0

'input = scan.next();'然后调用'inputNum = scan.nextInt();'不是你想要做的。这里有一些语法错误;毕竟,这可能不适合在Code Review上使用。 :/ – Makoto 2013-05-13 00:44:24

回答

1

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

0

您可以创建此类方法来识别该值是否为整数 -
public boolean isNumber(String s) { try { Integer.parseInt(s); return true; } catch(NumberFormateException nfe){ return false; } }