簡體   English   中英

所需的不兼容類型:發現的int:java.lang.String(基本編程)

[英]incompatible types required: int found: java.lang.String (basic programming)

我已經幾次遇到此錯誤,並且我嘗試了很多方法來解決它,但似乎無法找出問題所在。 這是代碼:

import java.util.Scanner;
public class InputExample {
    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);
        String name1 = scan.nextLine();
        String name2 = scan.nextLine();
        int num1 = scan.nextLine();
        double num2 = scan.nextLine();

        System.out.println("Employee Name:" + name2 + name1);
        System.out.println("Age:" + num1);
        System.out.println("Salary:" + (double)num2);
    }
}

具體顯示的錯誤是:

[文件:/InputExample.java行:9,列:25]所需的不兼容類型:發現的整數:java.lang.String

[文件:/InputExample.java行:10,列:28]所需的類型不兼容:找到了兩次:java.lang.String

您正在讀取一個String,但想將其另存為Integer / Double。 嘗試這個:

int num1 = Integer.valueOf(scan.nextLine());

Double也是如此。

或如OldCurmudgeon所述:

int num1 = scan.nextInt();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM