我对java(以及一般的编程)相对较新,我正在努力改进。我正在努力研究这个项目(不是学校相关的),要求根据他们输入的字符串,他们希望改变的方向以及职位数量来创建凯撒密码他们希望在字母表中转换。下面是一个例子:
正常字母表:A B C D E F G H
右移2:Y Z A B C D E F
左移3:D E F G H I J K
事情是,如果它只是一个角色就很简单,但我不知道如何在不使用阵列的情况下继续进行,我不是热衷于此。有什么建议吗?
更新
我已经设法将一些代码(通过数小时和数小时的帮助)编译成下面的代码,但是我的主要问题是遇到了两个错误。这是我的方法文件:
import java.util.Scanner;
public class HW4Methods
{
public static String readString(Scanner kb)
{
Scanner kbTwo = new Scanner(System.in);
System.out.print("Please Enter A String: ");
String userString = kbTwo.nextLine();
while(userString.equals(null) || (userString.isEmpty()))
{
System.out.print("Field Cannot Be Empty. Please Re-Enter: ");
userString = kbTwo.nextLine();
}
return userString;
}
public static int readAmountToShift(Scanner kb)
{
int amountToShift;
Scanner kbThree = new Scanner(System.in);
System.out.print("Please Enter the Amount You Wish To Shift: ");
amountToShift = kb.nextInt();
while((amountToShift < 0) || (amountToShift > 2000000000))
{
System.out.print("Number Not Within Specified Parameters. Please Re-Enter: ");
amountToShift = kb.nextInt();
}
return amountToShift;
}
public static String readDirection(Scanner kb)
{
Scanner kbFour = new Scanner(System.in);
System.out.print("Enter the Direction You Wish to Shift (Left or Right): ");
String shiftD = kb.next();
while(!shiftD.equalsIgnoreCase("left") && (!shiftD.equalsIgnoreCase("right")))
{
System.out.print("Invalid Direction. Enter the Direction You Wish to Shift (Left or Right): ");
shiftD = kbFour.next();
}
return shiftD;
}
public static int menu(Scanner kb)
{
int userChoice;
Scanner kbFive = new Scanner(System.in);
System.out.println("Please Choose From the Following:");
System.out.println("1. Enter New String.");
System.out.println("2. Encrypt String.");
System.out.println("3. Decrypt String.");
System.out.println("4. Quit.");
System.out.println("Please Enter Your Choice: ");
userChoice = kbFive.nextInt();
while((userChoice < 1)||(userChoice > 4))
{
System.out.print("Invalid Menu Choice. Please Try Again.");
userChoice = kbFive.nextInt();
}
return userChoice;
}
public static String encryptString(String origString, int amount, String direction)
{
origString = "";
for(int i=0;i < origString.length();i++)
{
char a = origString.charAt(i);
if(direction.equalsIgnoreCase("right"))
{
if(a + amount > 122)
{
origString += 97 +(a + amount - 123);
}
else if(a + amount > 90)
{
origString += 65+(a + amount - 91);
}
else if(a + amount > 97 && a + amount <= 122)
{
origString += (char)(a + amount);
}
else if(a + amount > 65 && a + amount <= 90)
{
origString += (char)(a + amount);
}
}
if(direction.equalsIgnoreCase("left"))
{
if(a+ amount < 65)
{
origString += (char)(90);
}
else if(a - amount < 91)
{
origString += 113 +(a - amount + 91);
}
else if(a + amount >= 97 && a + amount <= 122)
{
origString += (char)(a - amount);
}
else if(a + amount <= 65 && a + amount >= 91)
{
origString += (char)(a - amount);
}
}
}
return origString;
}
public static String decryptString(String encryptedString, int amount, String direction)
{
String decrypt = "";
for(int i=0;i < encryptedString.length();i++)
{
char a = encryptedString.charAt(i);
if(direction.equals("right"))
{
if(a - amount < 97)
{
decrypt += (char)(122) - (a - amount - 123);
}
else if(a - amount < 65)
{
decrypt += 90 - ((char)(a + amount) - 91);
}
else if(a - amount >= 97 && a - amount <= 122)
{
decrypt += (char)(a - amount);
}
else if(a - amount >= 65 && a - amount <= 90)
{
decrypt += (char)(a - amount);
}
}
if(direction.equals("left"))
{
if(a + amount > 90)
{
decrypt += 65 +((char)(a - amount) - 64);
}
else if(a + amount > 122)
{
decrypt += 97 + ((char)(a + amount) - 97);
}
else if(a + amount >= 65 && a + amount <= 91)
{
decrypt += (char)(a + amount);
}
else if(a + amount >= 97 && a + amount <= 122)
{
decrypt += (char)(a + amount);
}
}
}
return decrypt;
}
public static void display(String stringOne, String stringTwo)
{
System.out.println(stringOne + stringTwo);
}
}
这是我的主要文件(在另一个文件夹中):
import java.util.Scanner;
public class CSCD210HW4
{
public static void main(String [] args)
{
int choice, amount;
Scanner kb = new Scanner(System.in);
String direction, origString, encryptedString, decryptedString;
origString = HW4Methods.readString(kb);
amount = HW4Methods.readAmountToShift(kb);
direction = HW4Methods.readDirection(kb);
encryptedString = HW4Methods.encryptString(origString, amount, direction);
HW4Methods.display("Your encrypted string is ", encryptedString);
do
{
choice = HW4Methods.menu(kb);
switch(choice)
{
case 1: origString = HW4Methods.readString(kb);
break;
case 2: amount = HW4Methods.readAmountToShift(kb);
direction = HW4Methods.readDirection(kb);
encryptedString = HW4Methods.encryptString(origString, amount, direction);
HW4Methods.display("Your encrypted string is ", encryptedString);
break;
case 3: direction = HW4Methods.readDirection(encryptedString);
amount = HW4Methods.readAmountToShift(encryptedString);
decryptedString = HW4Methods.decryptString(encryptedString, amount, direction);
HW4Methods.display("Your decrypted string is ", decryptedString);
break;
case 4: System.out.println("Thanks for playing");
}// end switch
}while(choice != 4);
}// end main
}// end class
我在main中遇到两个错误,如下所示:
CSCD210HW4.java:33: error: incompatible types: String cannot be converted to Scanner
amount = HW4Methods.readAmountToShift(encryptedString);
^
CSCD210HW4.java:32: error: incompatible types: String cannot be converted to Scanner
case 3: direction = HW4Methods.readDirection(encryptedString);
^
我不知道为什么我会收到这些错误,这让我发疯了。
答案 0 :(得分:2)
每个character is associated with a number。检查this以查看如何将char转换为java字符串。想法是你可以在不制作阵列的情况下获得数字形式的范围。
如果你知道他们的界限,你可以使用mod(%)来回绕。例如:
如果我们有B = 66.索引(B) - 64 = 2.(这是从1到26的数字列表中的顺序)。
Shift = -5。所以答案应该是:W(第23次)
(指数(B)-64-5)%26 = 23。
只需确保从A开始,或者从A开始。如果从1开始A,则需要添加1.
<强>更新强>
由于你花了很多时间在这上面,让我举一个简单的例子来说明它的样子。这并没有好代码应该具有的所有异常检查和验证:
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the string to encrypt: ");
String string = scanner.nextLine().toUpperCase();
System.out.println("Enter offset");
int offset = Integer.parseInt(scanner.nextLine());
System.out.println("Encrypting " + string + " with offset: " + offset);
StringBuilder sb = new StringBuilder();
for (char c : string.toCharArray())
{
//broken down for clarity
int order = ((int)c) - 65 + offset;
//that strange addition is to force negative modulo to the answer that we want.
int newOrder = (order % 26) + ((order < 0) ? 26 : 0);
int backToAscii = newOrder + 65;
sb.append(Character.toString((char)backToAscii));
}
System.out.println(sb.toString());
}
示例用法:
Enter the string to encrypt:
Bulka
Enter offset
-5
Encrypting BULKA with offset: -5
WPGFV
OR
Enter the string to encrypt:
Bulka
Enter offset
5
Encrypting BULKA with offset: 5
GZQPF