试图让循环通过备用数组工作

Posted

技术标签:

【中文标题】试图让循环通过备用数组工作【英文标题】:Trying to get a loop to work through alternate arrays 【发布时间】:2021-08-21 13:27:47 【问题描述】:

我正在尝试通过交替字母大小写来打印出一个字符串。我希望YourStringYoUrStRiNg 的形式出现。我已经尝试了三件事,但我无法让循​​环按照我需要的方式工作。到目前为止,这是我所拥有的:

//one attempt
String s = "yourString";
String x = "";

for (int i = 0; i < s.length(); i += 2) 
    for (int j = 1; j < s.length(); j += 2) 
        x += Character.toUpperCase(s.charAt(i));
        x += Character.toLowerCase(s.charAt(j));
    

System.out.println(x);
//the desired result but not the ideal solution
String[] sArray = "Your", "String";
String f = "";
for (String n : sArray) 
    f += n;


char[] c = f.toUpperCase().toCharArray();
char[] d = f.toLowerCase().toCharArray();

System.out.print(c[0]);
System.out.print(d[1]);
System.out.print(c[2]);
System.out.print(d[3]);
System.out.print(c[4]);
System.out.print(d[5]);
System.out.print(c[6]);
System.out.print(d[7]);
System.out.print(c[8]);
System.out.print(d[9]);
System.out.println();
//third attempt with loop but the first loop keeps starting from zero
String t = "";
for (int i = 0; i < c.length; i += 2) 
    for (int j = 1; j < d.length; j += 2) 
        t += Character.toUpperCase(c[i]);
        t += Character.toLowerCase(d[j]);
    
    System.out.print(t);

我做错了什么?

【问题讨论】:

我不想破坏完整的答案,因为这可能是一些编程任务。所以你应该只迭代字符串一次,如果一个字符是大写的,你应该把它变成小写,反之亦然。像docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/… 这样的函数可能会很方便。 @Sebi 目标不是交换现有案例,而是根据位置交替。 也不想破坏答案,遍历字符串字符并使用i % 2 == 0 ? Character.toUpperCase(text.charAt(i)) : Character.toLowerCase(text.charAt(i)) 之类的东西来决定要做什么。 【参考方案1】:

实际上,没有必要对 String 的元素进行多次迭代。由于您需要更改字符的大小写,您可以使用运算符% 计算迭代的位置。因此,例如,给定c 作为当前字符串字符,操作将是这样的:

System.out.print(i % 2 == 0, (char)Character.toUpperCase(c) : (char)Character.toLowerCase(c));

但是,您实际上可以利用 Java Stream 和 lambda 表达式,从而实现一个非常优雅的解决方案。 我将向您展示我的建议解决方案。唯一的问题是您实际上无法拥有正确的循环变量,因为您在 Lamba 表达式中访问的变量必须是 final 或有效的 final,所以我使用了一种技巧。 这只是给您一个想法,您实际上可以对其进行个性化、使其可重复使用并根据您的意愿进行改进:

public class MyStringTest 
    public static void main(String args[]) 
      String s = "yourString";
      
      initializeCycleVariable();
      s.chars().forEach(c -> 
        System.out.print( MyStringTest.getAndIncrement() %2 == 0 ? 
                           (char)Character.toUpperCase(c) : 
                           (char)Character.toLowerCase(c));
        );
    
    
    private static int i = 0;
    
    public initializeCycleVariable()   i = 0; 
    public static int getAndIncrement()  return i++; 

这是输出:

YoUrStRiNg

【讨论】:

不知道模操作数可以用于迭代,谢谢。 @DazedandConfucius 如果您对答案感到满意,请接受并投票,以便将来帮助其他开发人员。谢谢。【参考方案2】:

您应该逐个字符地遍历字符串。偶数索引可以大写,奇数索引可以小写。很抱歉没有提供更多细节,但很明显这是一项任务。

【讨论】:

你至少可以举个例子来展示你写的东西。 这不是作业,我什至没有参加任何暑期课程。这只是我为提高自己的编程技能而进行的众多练习之一。【参考方案3】:

试试这个,

String s = "yourString", x = "";
for(int i = 0; i < str.length(); i++)

    if(i % 2 == 0)
        x += Character.toUpperCase(s.charAt(i));
    else
        x += Character.toLowerCase(s.charAt(i));

System.out.println(x);

【讨论】:

我觉得这个sn-p比较容易。

以上是关于试图让循环通过备用数组工作的主要内容,如果未能解决你的问题,请参考以下文章

试图让高阶函数在powershell中工作

通过for循环在nodejs中构造数组

Ruby嵌套for循环以填充数组数组

在迭代期间,循环在数组元素上停顿

PHP试图在while循环中向数组添加值

python selenium 循环通过一些链接