Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 基于移位方向和移位空间数的Caesar密码_Java_String_Character_Java.util.scanner - Fatal编程技术网

Java 基于移位方向和移位空间数的Caesar密码

Java 基于移位方向和移位空间数的Caesar密码,java,string,character,java.util.scanner,Java,String,Character,Java.util.scanner,我对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 问题是,如果它只是一个字符,那么做起来很简单,但是我不知道如何在不使用数组的情况下继续,我不喜欢数组。有什么建议吗 更新 我已经设法将一些代码(通过数小时的帮助)

我对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);
   }



}
我主要遇到以下两个错误:

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);
                                                          ^
我不知道为什么我会犯这些错误,这让我发疯

每个。查看如何将字符转换为java字符串。这个想法是,你们可以得到一个数字形式的范围,而无需制作数组

如果您知道它们的边界,您可以使用mod(%)进行环绕。例如:

如果我们有B=66。指数(B)-64=2。(这是数字列表中从1到26的顺序)

移位=-5。所以答案应该是:W(23)

(指数(B)-64-5)%26=23

只需确保A从1或0开始。如果A从1开始,则需要添加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());
}


既然大写和小写ascii字符各有26个字母,我是否可以用%26来概括一下?很抱歉占用您一点时间。是的,只要你明白你是65岁开始的,只有资本。如果同时执行这两项操作,则需要容纳不属于旋转的中间字符。如果您只使用大写或小写,则更容易。另外,请记住,使用%26时,您假设A从0开始,因此Z=25,下一次到达26左右时,它将是26%26-->0。“如果它只是一个字符,那么这样做很简单,但是…”如果什么只是一个字符?它是从文件中读取的字符吗?它是来自命令行的字符吗?它在Swing文本框中吗?…(与学校无关)…我不知道如何在不使用数组的情况下继续,我不喜欢使用数组。“是的,唯一的问题是,老师经常说,“你可能不使用数组”,但不寻常的是,有人“不喜欢”使用数组来保存任何固定长度的序列。
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
Enter the string to encrypt: 
Bulka
Enter offset
5
Encrypting BULKA with offset: 5
GZQPF