如何区分子程序中的数据类型?

时间:2013-11-27 18:06:24

标签: java

我的作业如下:

  

编写子程序,使用重载函数查找最小的两个值。

int smallest (int a, int b)
char smallest (char a, char b)
float smallest (float a, float b)
double smallest (double a, double b)

我必须使用Scanner类进行输入,并在所有子例程中使用相同的代码。我的问题列在代码之后。任何帮助是极大的赞赏。

package subassignment;

import java.util.Scanner;

public class Subassignment {

    public static void main(String[] args) {

        Scanner console = new Scanner (System.in);

        String a, b;
        int v1, v2, min;

        System.out.println("Enter a value: ");
        a = console.next();
        System.out.println("Enter another value: ");
        b = console.next();

        v1 = Integer.parseInt(a);
        v2 = Integer.parseInt(b);         
        min = larger(v1, v2);

        System.out.println(min);

    }
    public static int larger(int x, int y){
        if (x > y){
           return y;
        }
        else{
           return x;   
        }
    }
public static char larger(char x, char y){
    if (x > y){
      return y;
    }
    else{
      return x;   
    }
 }
public static float larger(float x, float y){
    if (x > y){
       return y;
    }
    else{
      return x;   
    }
}
public static double larger(double x, double y){
    if (x > y){
      return y;
    }
    else{
     return x;   
    }  
 }
}

程序仅在我输入整数值时有效。我认为无论我输入什么数据类型Integer.parseInt(a)都会将其转换为整数值。我应该在哪里输入floatdoublechar的值。 假设我的输入为g,然后是f,我在编译器中收到此消息:

Exception in thread "main" java.lang.NumberFormatException: For input string: "g"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:492)
    at java.lang.Integer.parseInt(Integer.java:527)
    at subassignment.Subassignment.main(Subassignment.java:20)
Java Result: 1

*答案后的修订版#2

package subassignment;

import java.util.Scanner;

public class Subassignment {

public static void main(String[] args) {
    Scanner console = new Scanner (System.in);

        String a, b;
        int i1, i2, i3, type;
        char c1, c2, c3;
        double d1, d2, d3;
        float f1, f2, f3;

        System.out.println("What is your data type: ");
        System.out.println("1 for int: 2 for char: 3 for double: 4 for float ");
        type = console.nextInt();
        System.out.println("Enter a value: ");
        a = console.next();
        System.out.println("Enter another value: ");
        b = console.next();

        switch (type){
        case 1:{
        i1 = Integer.parseInt(a);
        i2 = Integer.parseInt(b);         
        i3 = larger(i1, i2);
        System.out.println(i3);
        break;
        }
        case 2:{
        c1 = a.charAt(0);
        c2 = b.charAt(0);
        c3 = larger(c1, c2);
        System.out.println(c3);
        break;
        }        
        case 3:{
        d1 = Double.parseDouble(a);
        d2 = Double.parseDouble(b);
        d3 = larger(d1, d2);
        System.out.println(d3);
        break;
        }
        case 4:{
        f1 = Float.parseFloat(a);
        f2 = Float.parseFloat(b);
        f3 = larger(f1, f2);
        System.out.println(f3);
        break;
        }
      }
    }
    public static int larger(int x, int y){
    if (x > y){
    return y;
        }
    else{
     return x;   
        }
    }
public static char larger(char x, char y){
if (x > y){
    return y;
   }
    else{
     return x;   
        }
    }
public static float larger(float x, float y){
if (x > y){
    return y;
   }
    else{
     return x;   
        }
    }
public static double larger(double x, double y){
if (x > y){
    return y;
   }
    else{
     return x;   
        }  
    }
}

2 个答案:

答案 0 :(得分:0)

Integer.parseInt(String a)仅适用于String到int的转换,因此无法用于float,double或char输入!!!

答案 1 :(得分:0)

    You can scan one more input value and get the code working as below:-
    ......
.........
            System.out.println("Enter a Input Type Integer->1 :: Char->2 :: Float->3::Double->4 ");
    int inputType = console.next();
    System.out.println("Enter a value: ");
    a = console.next();
    System.out.println("Enter another value: ");
    b = console.next();
    int v1,v2,v3;
    char vc1,vc2,vc3;
    float vf1,vf2,vf3;
    double vd1,vd2,vd3;
    switch(inputType)
    {
    case 1:
    {
        v1 = Integer.parseInt(a);
        v2 = Integer.parseInt(b);
        v3=larger(v1,v2);
    }
    case 2:
    {
        vc1 = a.charAt(0);
        vc2 = b.charAt(0);
        vc3=larger(vc1,vc2);
    }
    case 3:
    {
        vf1 = Float.parseFloat(a);
        vf2= Float.parseFloat(b);
        vf3=larger(vf1,vf2);
    }
    case 4:
    {
        vd1 = Double.parseDouble(a);
        vd2 = Double.parseDouble(a);
        vd3=larger(vd1,vd2);
    }
    }
......
.......