Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/394.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 递增多个字母_Java_For Loop - Fatal编程技术网

Java 递增多个字母

Java 递增多个字母,java,for-loop,Java,For Loop,我对这段代码有问题,它不断重复同一件事27次,而不是每次转换。因此,如果用户输入“ASDFG”,它每次都会重复“BTEGH” import java.io.* ; import java.util.Scanner; public class what { public static void main (String[] args) { Scanner hey = new Scanner(System.in); System.out.print("Enter a word: "); String

我对这段代码有问题,它不断重复同一件事27次,而不是每次转换。因此,如果用户输入“ASDFG”,它每次都会重复“BTEGH”

import java.io.* ;
import java.util.Scanner;
public class what
{
public static void main (String[] args)
{
Scanner hey = new Scanner(System.in);
System.out.print("Enter a word: ");
String w = hey.nextLine();
System.out.println(w);
int j=0;
while(j<28){
for(int i=0; i<w.length(); i++)
{
  char ch = w.charAt(i);
  ch++;
  System.out.print(ch);
}
j++;
System.out.println();
}
}}
import java.io.*;
导入java.util.Scanner;
公共课什么
{
公共静态void main(字符串[]args)
{
扫描仪hey=新扫描仪(System.in);
System.out.print(“输入单词:”);
字符串w=hey.nextLine();
系统输出打印LN(w);
int j=0;

while(jwhile循环导致代码重复27次。如果您只想将用户输入的字符串转换一次,请删除while循环

Scanner hey = new Scanner(System.in);
System.out.print("Enter a word: ");
String w = hey.nextLine();
System.out.println(w);

for (int i = 0; i < w.length(); i++) {
  char ch = w.charAt(i) + 1;
  System.out.print(ch);
}
Scanner-hey=新的扫描仪(System.in);
System.out.print(“输入单词:”);
字符串w=hey.nextLine();
系统输出打印LN(w);
对于(int i=0;i
您永远不会实际更改
字符串中的字符。如果除了打印递增的字符外,您还想实际更改
字符串中的字符,那么您可以使用
setCharAt()
from,即:

然后在循环中,您可以执行以下操作:

for(int i=0; i < sb.length(); i++)
{
  char ch = sb.charAt(i);
  sb.setCharAt(i) = ++ch;
  System.out.print(ch);
}
for(int i=0;i
它应该是某种加密/解密程序,因此它应该显示27次,每次都是不同的。其中一个输出应该是可读的。然后,您不会将字符保存回字符串中。您可以将字符附加到字符串中,对于每个while步骤,打印字符串并替换w变量,字符串为previous。但是,请考虑这是一种可预测的行为。:@sgmart这可能是学校作业……是的,我想我从未更改过字符串。Thanks@user2968215是的。没问题。祝你好运!
for(int i=0; i < sb.length(); i++)
{
  char ch = sb.charAt(i);
  sb.setCharAt(i) = ++ch;
  System.out.print(ch);
}