2016-12-11 12 views
1

私はこのプロジェクトを1時間作業していますが、解決策を見つけることができません。 ここに私のコードです:エラー:互換性のない型がfloatからcharへの不可逆変換可能

import java.util.Scanner; 



// Created by ab38 on 2015-12-01. 

public class EnigmaEncrypt { 

    // Static Scanner field 

    private static Scanner scan; 

    /** 
    * Get a char from the command line. 
    * 
    * @return the first char of the current line, typed by the user 
    * on the command line. 
    */ 
    private static char getChar() { 
     return scan.next().charAt(0); 
    } 

    /** 
    * Get an integer from the command line. 
    * 
    * @return the integer on the current line, typed by the user 
    * on the command line. 
    */ 
    private static int getInt() { 
     return scan.nextInt(); 
    } 

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

     // Example: How to get an integer from the command line: 
     System.out.println("Enter an integer:"); 
     int exampleIntInput = getInt(); 
     System.out.println("Rotorposition: " + exampleIntInput); 


     // Example: How to get a single char from the command line: 
     System.out.println("Enter a single character:"); 
     float exampleCharInput = getChar() + exampleIntInput; 
     char e = exampleCharInput; 

     System.out.println("Char from command line: " + e); 


    } 
} 
+0

"解決策がまだ見つかりません":どの問題を解決しようとしていますか?私はそれが暗号化に関係していると仮定しますが、それを綴ってください。また、charに浮動小数点を割り当てるのはなぜですか?その文字は何を意味するのですか? –

+0

intとcharの合計を浮動小数点に割り当てるのはなぜですか?その合計は何を意味するのですか? – greybeard

答えて

0
、私はいつも同じエラー( ^ 1エラー 文字E = exampleCharInputをcharへのフロートから可能な非可逆変換:エラー:互換性のないタイプがEnigmaEncrypt.java:44)を取得しています

charは2bytesに格納され、floatは4bytesに格納されます。キャストをせずに浮動小数点型をcharに配置しようとしています。代わりにこれを試してください

char e = (char) exampleCharInput; 
関連する問題