Java中温度转换的操作数错误?

时间:2015-09-16 19:27:23

标签: java temperature operand

我正在进行第一次Java任务,我在这里遇到错误。

 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ctof;

/**
 *
 * @author Braydon
 */
    import java.util.Scanner;
public class CtoF {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        System.out.println("Enter temperature in Celsius:");
        Scanner temp = new Scanner(System.in);
        String T = scan.nextLine();
            T = (T - 32) * 5/9;
            System.out.println("Temperature in Fahrenheit =" + T);
             }
}

它给我的错误如下。

run:
Enter temperature in Celsius:
Exception in thread "main" java.lang.UnsupportedOperationException: Not supported yet.
    at ctof.scan.nextLine(scan.java:19)
    at ctof.CtoF.main(CtoF.java:21)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

错误在我执行数学运算的行中,但我已经尝试了所有内容,我似乎无法修复它。请帮忙!

2 个答案:

答案 0 :(得分:1)

你正在调用错误的功能。

您需要拨打temp.nextLine()而不是scan.nextLine()才能阅读下一行。 (在您发布的代码中甚至没有定义扫描)

但是:当您需要阅读号码时,不应该使用nextLine()

因此:请改为呼叫temp.nextInt()temp.nextDouble()

答案 1 :(得分:0)

我相信你是java的新手。首先,您必须了解数据类型。好吧,你可以谷歌基础知识编程并从中学习。

解决问题的方法:

public class CtoF {

    public static void main(String[] args) {
        System.out.println("Enter temperature in Celsius:");
        Scanner temp = new Scanner(System.in);
        int T = temp.nextInt();
        T = (T - 32) * 5 / 9;
        System.out.println("Temperature in Fahrenheit =" + T);
    }
}

尝试为变量使用好名称(只是建议)