我的程序不会超过我的前几行代码

时间:2016-02-09 02:47:48

标签: java arrays eclipse

所以我对这个网站很陌生,但你们是我最后的希望。这里的目标是用户将以华氏度输入10个温度(输入-999以在进入时停止)然后程序将这些温度改变为摄氏度。用户还需要输入他们的位置。我可以输入该位置,但是在我点击“输入”后#39;我明白了:

  

线程中的异常" main" java.util.InputMismatchException
      在java.util.Scanner.throwFor(未知来源)
      在java.util.Scanner.next(未知来源)
      在java.util.Scanner.nextInt(未知来源)
      在java.util.Scanner.nextInt(未知来源)
      在Prog1.main(Prog1.java:20)`

这是我的代码:

import java.util.Scanner;
public class Prog1 
{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);

        //asks the user for the location
        System.out.print("Enter Location: ");
        int place = in.nextInt();
        System.out.print(place);

        double[] temp = new double[10];
        int num = 0;
        //prime the loop
        System.out.print("Enter temperature: ");
        int input = in.nextInt();
        //get up to 10 temperatures and store in array "temp"
        while (input >= -998 && num < 10) {
            temp[num] = input;
            num++;

            System.out.print("Enter temperature: ");
            input = in.nextInt();
        }
        //print report
        System.out.println();
        System.out.printf("%5s    %5\n", "Fahrenheit", "Celcius");
        for (int x = 0; x < num; x++) {
            System.out.printf("%5d    %5s\n", temp[x], celsius( temp[x]));
        }

        System.out.printf("\nHigh: %6.2f", max( temp, num));
        System.out.printf("\nLow: %6.2f", min( temp, num));
        System.out.printf("\nAverage: %6.2f", average(temp, num));      
    }

    /**
     * Method to convert farenheit to celsius
     * @param double farenheit temperature
     * @return double celsius temperature 
     */
    public static double celsius(double input) {
        double celcius = 5.0 / 9.0 * ( input - 32);
        return celcius;
    }

    /**
     * Method to calculate average, min and max temperatures
     * 
     * @param double farenheit temperature
     * @return average, min and max temperatures
     */
    public static double average(double[] temp, int num) {
        double sum = 0;
        for (int x = 0; x < num; x++) {
            sum += temp [x];
        }
        return (double) sum / num;
    }

    public static double max( double[] temp , int num){
        double max = temp[0];
        for (double x : temp) {
            if (max > num) {
                max = num;
            }
            return num; 
        }
        return  max;
    }

    public static double min(double[] temp, int num) {
        double min = temp[0];
        for (double x : temp) {
            if (min < num) {
                min = num;
            }
            return min;
        }
        return num;
    }
}

4 个答案:

答案 0 :(得分:0)

您正在获取InputMismatchException,这意味着无论用户提交给您的是什么,都是您编程的不同类型。

您要求用户输入其位置,然后拨打in.nextInt()。如果标准输入中的下一个标记不是整数,则nextInt()将抛出异常。您必须为抛出异常的位置输入除整数之外的其他内容。如果要输入基于文本的位置(如字符串),请尝试in.nextLine()

答案 1 :(得分:0)

上面的解决方案非常简单。您必须替换:

 System.out.print("Enter Location: ");
 int place = in.nextInt();
 System.out.print(place);

 System.out.print("Enter Location: ");
 String place = in.nextLine();
 System.out.print(place);

因为位置是一个字符串,当你给出像印度&#39; in.nextInt()会期待一个数字而不是字符串。所以你得到了那个错误。只在输入位置sysout下面放置它。

也替换格式。试试这个:

import java.util.Scanner;
public class Prog1 
{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);

        //asks the user for the location
        System.out.print("Enter Location: ");
        String place = in.nextLine();
        System.out.print(place);

        double[] temp = new double[10];
        int num = 0;
        //prime the loop
        System.out.print("Enter temperature: ");
        int input = in.nextInt();
        //get up to 10 temperatures and store in array "temp"
        while (input >= -998 && num < 10) {
            temp[num] = input;
            num++;

            System.out.print("Enter temperature: ");
            input = in.nextInt();
        }
        System.out.println();
        System.out.printf("%s    %s\n", "Fahrenheit", "Celcius");
        for (int x = 0; x < num; x++) {
            System.out.println(temp[x] + "\t" +celsius(temp[x]) );
        }

        System.out.printf("\nHigh: %6.2f", max( temp, num));
        System.out.printf("\nLow: %6.2f", min( temp, num));
        System.out.printf("\nAverage: %6.2f", average(temp, num));      
    }

    /**
     * Method to convert farenheit to celsius
     * @param double farenheit temperature
     * @return double celsius temperature 
     */
    public static double celsius(double input) {
        double celcius = 5.0 / 9.0 * ( input - 32);
        return celcius;
    }

    /**
     * Method to calculate average, min and max temperatures
     * 
     * @param double farenheit temperature
     * @return average, min and max temperatures
     */
    public static double average(double[] temp, int num) {
        double sum = 0;
        for (int x = 0; x < num; x++) {
            sum += temp [x];
        }
        return (double) sum / num;
    }

    public static double max( double[] temp , int num){
        double max = temp[0];
        for (double x : temp) {
            if (max > num) {
                max = num;
            }
            return num; 
        }
        return  max;
    }

    public static double min(double[] temp, int num) {
        double min = temp[0];
        for (double x : temp) {
            if (min < num) {
                min = num;
            }
            return min;
        }
        return num;
    }
}

答案 2 :(得分:0)

InputMismatch将数据类型放在某个预期为不同数据类型的位置时,抛出异常。由于您正在使用度,我假设您可能正在输入浮点值,因此in.nextDouble()可能是比in.nextInt()更好的选项。如果您希望将其作为输出,则可以稍后将其转换为整数。

答案 3 :(得分:0)

我只是为了看看发生了什么来运行你的代码。您没有正确格式化printf。它将替换这部分。

//print report
        System.out.println();
        System.out.printf("%s    %s\n", "Fahrenheit", "Celcius");
        for (int x = 0; x < num; x++) {
            System.out.println(temp[x] + "\t" +celsius(temp[x]) );
        }

我发现此快速指南很有用:here