java 返回字符串函数_java – 返回字符串的方法

所以我正在用

Java做一个简单的加密程序.用户输入一个字符串(strTarget),然后该字符串被带到此函数.在for循环中,它应该取字符的ASCII值,将其减少4,然后将其返回到字符串(对字符串中的所有字符执行此操作).当你看到我的朋友时,我已经这样做了,但是,我不知道如何重建我希望得到的字符串(例如,如果用户输入’efg’,返回的字符串应该是’abc’)

所以,这是我得到的建议的结果.我在Menu类中显然做错了,不确定它是什么.当我输入要加密的字符串时,它停止工作.

import java.util.Scanner;

public class Menu {

public static String strTarget;

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

System.out

.println("Welcome to the encr/decr program");

System.out

.println("To encrypt a string, press 1, to decrypt a string, press 2");

int choice = in.nextInt();

if (choice == 1) {

System.out.println("Type the string you want to encrypt.");

strTarget = in.next();

System.out.println(Encrypt(strTarget));

}

if (choice == 2) {

System.out.println("Enter the string you want to decrypt.");

}

}

private static String Encrypt(String strTarget) {

// TODO Auto-generated method stub

int len = strTarget.length()-1;

String destination = "";

for (int i = 0; i

{

if (strTarget.charAt(i) != ' ')

{

char a = strTarget.charAt(i);

int b = (int) a;

b = strTarget.charAt(i)-4;

a = (char) b;

if ( b<70 && b>64)

{

b = strTarget.charAt(i)+26;

a = (char) b;

destination += a;

}

}

}

return destination;

}

}

编辑:添加完整的程序.

import java.util.Scanner;

public class Menu {

public static String strTarget;

public static String destination = "";

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

System.out.println("Welcome to the encr/decr program");

System.out.println("To encrypt a string, press 1, to decrypt a string, press 2");

int choice = in.nextInt();

if (choice == 1) {

System.out.println("Type the string you want to encrypt.");

strTarget = in.next();

StringBuilder zomg = new StringBuilder(strTarget);

System.out.println(Encrypt(zomg));

}

if (choice == 2) {

System.out.println("Enter the string you want to decrypt.");

}

}

private static String Encrypt(StringBuilder zomg) {

// TODO Auto-generated method stub

int len = strTarget.length()-1;

for (int i = 0; i

{

if (strTarget.charAt(i) != ' ')

{

char a = strTarget.charAt(i);

int b = (int) a;

b = strTarget.charAt(i)-4;

a = (char) b;

destination += a;

if ( b<70 && b>65)

{

b = strTarget.charAt(i)+26;

a = (char) b;

destination += a;

}

}

}

System.out.println(destination);

return destination;

}}

我做了你所说的改变(我认为),它开始起作用了,但它没有按预期工作.给出一些似乎没有意义的结果(对于’A’,它返回=,对于’V’,它返回’V’).有什么建议?

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值