简体   繁体   English

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

[英]Operand error in temperature conversion in Java?

I am doing my first Java assignment and I'm struggling with an error here.我正在做我的第一个 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);
             }
}

The error it gives me is as follows.它给我的错误如下。

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)

The error is in the line where I perform the math, but I've tried everything and I can't seem to fix it.错误出现在我执行数学运算的那一行,但我已经尝试了所有方法,但似乎无法修复它。 Please help!请帮忙!

You're calling the wrong function.你调用了错误的函数。

You need to call temp.nextLine() instead of scan.nextLine() in order to read the next line.您需要调用temp.nextLine()而不是scan.nextLine()以读取下一行。 (scan isn't even defined in the code you posted) (扫描甚至没有在你发布的代码中定义)

HOWEVER: You shouldn't use nextLine() when you need to read a number.但是:当您需要读取数字时,不应使用nextLine()

Therefore: Call temp.nextInt() or temp.nextDouble() instead.因此: temp.nextInt()调用temp.nextInt()temp.nextDouble()

I believe you are newbie to java.我相信你是java的新手。 First of all, you will have to learn about the data types.首先,您必须了解数据类型。 Well, you can google basics about programming and learn from it.好吧,您可以在 google 上搜索有关编程的基础知识并从中学习。

The solution for your problem :您的问题的解决方案:

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);
    }
}

Try to use good names for the variables (just a suggestion)尝试为变量使用好名字(只是一个建议)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM