私はjavaを初めて使い、現在は学部のプログラミングコースに在籍しています。私は、テスタークラスを作成して暗号化クラスを作成する必要がある割り当てを受けました。テスタークラスでは、暗号化クラスの2つのオブジェクトを作成します.1つはデフォルトコンストラクターを使用し、もう1つはパラメーターとしてコンストラクターを使用し、2つのスキャナー入力を引数(パスワードとキー)として使用します。これは私が難しいことです。私はこれを正しくやっているとは思わない。ここにコードがあります。コンパイル時に受け取ったエラーは、「互換性のない型、文字列をint型に変換できません」と表示されます。私を助けることができる人には感謝します。javaのコンストラクタ引数としてスキャナ入力を使用する
public class Encryption
{
private int key;
private String encryptedPassword;
Scanner scan = new Scanner(System.in);
public Encryption()
{
key = 0;
encryptedPassword = "";
}
public Encryption(int key, String password)
{
this.key = key;
password = password;
setPassword(password);
}
public void encrypt(String password)
{
char ch;
for(int i=0; i<password.length(); i++){
ch = password.charAt(i);
if (ch >= '!' && ch <= 'z'){
ch = (char)(ch + key);
if (ch > 'z'){
ch = (char)(ch - 'z' + '!' - 1);}
else if (ch < '!'){
ch = (char)(ch +'z' - '!' +1);}
encryptedPassword = password + ch;
}
}
}
public boolean isValidPassword (String password)
{
encrypt(password);
if (password.equals(encryptedPassword))
return true;
else
return false;
}
public String getEncryptedPassword()
{
return encryptedPassword;
}
public void setPassword(String password)
{
encrypt(password);
}
public int getKey()
{
return key;
}
public String toString()
{
return "The encrypted password is " + getEncryptedPassword() + ". " + "The key used to generate this password is " + getKey() + ".";
}
}
ここにテスターのクラスコードがあります。
import java.util.Scanner;
public class EncryptionTester
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Enter a password.");
String password = scan.next();
while (password.length() < 8)
{
System.out.println("The password must be at least 8 characters long. Your password is only " + password.length() + " characters long.");
System.out.println("Enter a password.");
password = scan.next();
}
System.out.println("Enter a number between 1 and 10.");
int key = scan.nextInt();
while (key < 1 || key > 10)
{
System.out.println("The key must be between 1 and 10. You entered " + key);
System.out.println("Enter a number between 1 and 10");
key = scan.nextInt();
}
Encryption defaultEncryption = new Encryption();
Encryption argEncryption = new Encryption(password, key);
}
'公共の暗号化(int型のキー、文字列のパスワード)'と '新しい暗号化(パスワード、キー)を試してみてください;'、 – tkausl