当前位置: 首页 > 知识库问答 >
问题:

需要帮助保存和继续用户输入

赫连实
2023-03-14

我正在尝试创建一个文字游戏,它可以对一个单词进行加密,并根据用户输入的内容将字符移位一定的数字,解密加密,并检查该单词是否为回文。问题是我不知道如何保持输入,所以在我加密或检查它是否为回文后,程序结束,因此我无法解密加密的内容。

例如,如果用户输入单词“hello”并选择加密该单词,密钥为3,则应显示“khoor”。然后,我希望能够通过将“khor”解密回“hello”来继续,但程序结束。

此外,当用户输入键号时,字符比输入的数字多移位7个字符。

import java.util.Scanner;

public class WordPlayTester{

public static void main(String [] args){

String word, reverse="";
String original;
int key= 0;
String Menu= "1-Encrypt \n2-Decrypt \n3-Is Palindrome \n0-Quit \n-Select an option-";

Scanner in = new Scanner(System.in);
System.out.println("-Type any word-");
      String toUpperCase;
      word = in.nextLine();
System.out.println(Menu);

   int choice=in.nextInt();
   if(choice==1)
   {
  System.out.println("Insert a Key number");
   int select= in.nextInt();


      for (int i=0; i < word.length(); i++) {
         char c = word.charAt(i);
         if (c >= 'A' && c <= 'z') {
            c = (char)(c - 65);
            int n = c+select;
            n = n % 26;
            if (n < 0) {
               n = n + 26;
            }
            c = (char)(n + 65);
         }
         System.out.print(c);
      }
      }
      else if(choice==2)
   {
  System.out.println("Insert a Key number");
   int select2= in.nextInt();


      for (int i=0; i < word.length(); i++) {
         char c = word.charAt(i);
         if (c >= 'A' && c <= 'z') {
            c = (char)(c - 65);
            int n = c+select2;
            n = n % 26;
            if (n < 0) {
               n = n - 26;
            }
            c = (char)(n - 65);
         }
         System.out.print(c);

      }
      if(key==0)
      {
      System.out.println("Word has not been encrypted yet.");
      }

}



   else if(choice==3)
   {
   int length = word.length();
      for ( int i = length - 1 ; i >= 0 ; i-- )
         reverse = reverse + word.charAt(i);
      if (word.equals(reverse))
         System.out.println("Your word is a palindrome.");
      else
         System.out.println("Your word is not a palindrome.");


      }
      else if(choice==0)
      {
      System.exit(0);
      }

     else 
      {
      System.out.println(Menu);
      }

   }
}

谢谢你

共有2个答案

谷光誉
2023-03-14

有一个边做循环

do{
   //your logic
}while(choice!=0);
姜胤
2023-03-14

从第一个输入开始,将您的逻辑置于一个 do-while 循环中。然后在最后使用变量来决定何时退出。

String isExit = "N";
do{
    System.out.println("-Type any word-");
    String toUpperCase;
    word = in.nextLine();
    System.out.println(Menu);
.
.
.
.
System.out.println("-Exit? Y/N-");
    isExit = in.nextLine();

}while(!isExit.equals("Y"))
 类似资料:
  • 我已经看了这个程序几个小时了,但我仍然不明白其中的逻辑。程序要求用户输入整数。 一旦用户输入了整数,它将向用户输出所有的正数、负数和输入的总数,以及这些数字的平均值。 这是我陷入困境的地方: 然后它会询问用户是否要继续。用户将输入或以指示他们是否要继续。如果用户输入“y”,循环将再次进行,如果他们输入“n”,它将显示再见消息。我不知道如何设置。 我已经尝试了一个 do while 循环,我目前正在

  • 在一个HTML文件中, 包含许多 ,而在另一个 中, 包含许多 。使用我需要的JavaScript,当我悬停在第一个上时,第一个 的背景颜色会发生变化,以此类推... 匿名用户 你的问题是如此令人困惑,提供一个屏幕截图或绘图表明你实际想要什么。

  • 我在尝试将MyBatis和Javers(与Spring)集成并工作时遇到了问题。我已经按照http://Javers.org/documentation/spring-integration/上的说明进行了方面设置,注释了实体类并用Javers注册了它,MyBatis接口用@repository和@javersauditable正确地注释了适当的方法,但仍然没有使它工作,甚至在Javers方面设置

  • 我想深入了解Jmeter输出。 > 我对吞吐量率的概念感到困惑。这是否意味着服务器在给定负载下只能处理48.1个请求/分钟,还是意味着其他什么。总吞吐量速率和单个请求显示的吞吐量速率之间的差异是什么。在我的情况下,发送了8个请求,单个请求显示吞吐量为6.1/min。请解释一下。 我需要建议服务器端的任何更改/解释jmeter报告,请建议我如何解释需要做什么。 总的总结报告如下: 总用户: 100上

  • 首先,我是C、C++、C#、Android和Swift的开发人员,但我绝对没有JavaScript、PHP或Web开发经验。 即只接受整数值的输入。 这是刀片代码:

  • 我有以下代码: 但不管我做什么都会出错 2015/05/18 08:45:51[错误]14466#0:*907 FastCGI在stderr中发送:“PHP消息:PHP致命错误:在第44行的/var/http/submit.PHP中对非对象调用成员函数execute()”同时从上游读取响应头,客户端:xx,服务器:,请求:“POST/submit.PHP http/1.1”,上游:fastcgi: