JAVA实际参数和形式参数列表长度不同
publicstaticStringencrypt(Stringmessage,intkey){System.out.println("encoding:"+messag...
public static String encrypt(String message, int key) {
System.out.println("encoding: "+message+", with key: "+key);
String encodedMessage = "" ;
System.out.print("Encrypted result: ");
String a = message.toUpperCase();
int length = a.length();
for(int i = 0; i < length; i++){
int y = (int)(a.charAt(i)) + key;
if(y > 90 ){
y = y - 26;}
char B = Character.toUpperCase((char)y);
System.out.print(B);
}
return encodedMessage;
}
/**
* This method tests the encrypt method using a Scanner
* to read in user input from the command line
*/
public static void userInteraction() {
Scanner input = new Scanner(System.in);
System.out.println("Type a message.");
String message = input.next ();
String a = message.toUpperCase();
Scanner input2 = new Scanner(System.in);
System.out.println("Type a key");
int key = input.nextInt();
System.out.print(encodedMessage);
encrypt();
}
=================================================================================就想在第二个里面让用户输入,然后运行第三个的算法QAQ但是出现什么形式列表和实际参数长度不同 展开
System.out.println("encoding: "+message+", with key: "+key);
String encodedMessage = "" ;
System.out.print("Encrypted result: ");
String a = message.toUpperCase();
int length = a.length();
for(int i = 0; i < length; i++){
int y = (int)(a.charAt(i)) + key;
if(y > 90 ){
y = y - 26;}
char B = Character.toUpperCase((char)y);
System.out.print(B);
}
return encodedMessage;
}
/**
* This method tests the encrypt method using a Scanner
* to read in user input from the command line
*/
public static void userInteraction() {
Scanner input = new Scanner(System.in);
System.out.println("Type a message.");
String message = input.next ();
String a = message.toUpperCase();
Scanner input2 = new Scanner(System.in);
System.out.println("Type a key");
int key = input.nextInt();
System.out.print(encodedMessage);
encrypt();
}
=================================================================================就想在第二个里面让用户输入,然后运行第三个的算法QAQ但是出现什么形式列表和实际参数长度不同 展开
2个回答
展开全部
你的encrypt方法有错误,你需要在System.out.print(B)下一行加入
encodedMessage += B;
不然你这个加密方法始终会返回一个empty string
我给下代码和运行结果,
import java.util.Scanner;
public class EncryptTest {
public static void main(String[] args) {
System.out.println(encrypt("hello", 20));
userInteraction();
}
public static String encrypt(String message, int key) {
System.out.println("encoding: " + message + ", with key: " + key);
String encodedMessage = "";
System.out.print("Encrypted result: ");
String a = message.toUpperCase();
int length = a.length();
for (int i = 0; i < length; i++) {
int y = (int) (a.charAt(i)) + key;
if (y > 90) {
y = y - 26;
}
char B = Character.toUpperCase((char) y);
System.out.print(B);
encodedMessage += B;
}
return encodedMessage;
}
/**
* This method tests the encrypt method using a Scanner to read in user
* input from the command line
*/
public static void userInteraction() {
String encodedMessage = "";
Scanner input = new Scanner(System.in);
System.out.println("Type a message.");
String message = input.next();
//String a = message.toUpperCase();
//Scanner input2 = new Scanner(System.in);
System.out.println("Type a key");
int key = input.nextInt();
input.close();
encodedMessage = encrypt(message, key);
System.out.print(encodedMessage);
//encrypt();
}
}
可以看出来你是在国外大学刚学java,好好学,多多练习,能看出来你对一些概念比较模糊和朦胧。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询