仅使用循环反向打印字符串(JAVA)

问题描述 投票:0回答:4

编写一个程序,将一行文本作为输入,并反向输出该行文本。程序重复,当用户为文本行输入“Done”、“done”或“d”时结束。

如果输入是: 你好呀 嘿 完成

输出是: 正确的观点 是啊

//下面是我的代码。我很困惑为什么在测试 2-4 行的输入时会出现以下错误。

    Exception in thread "main" java.util.NoSuchElementException
at java.base/java.util.Scanner.throwFor(Scanner.java:937)
at java.base/java.util.Scanner.next(Scanner.java:1478)
at LabProgram.main(LabProgram.java:9)

import java.util.Scanner;

public class LabProgram {
  public static void main(String[] args) {
  Scanner scnr = new Scanner(System.in);
  
  String strInput = scnr.nextLine();
  String strInput2 = scnr.nextLine();
  String strInput3 = scnr.next();
  int i = 0;
  int j = 0;
  int k = 0;
  
  for (i = strInput.length() - 1; i >= 0; --i) { 
        if (strInput.equals("d") || strInput.equals("done") || strInput.equals("Done")) {
           break;
        }
        else {
        System.out.print(strInput.charAt(i));
        }
  }
  System.out.println();
  
  for (j = strInput2.length() - 1; j >= 0; --j) {
        if (strInput2.equals("d") || strInput2.equals("done") || strInput2.equals("Done")) {        
           break;
        }
        else {
           System.out.print(strInput2.charAt(j));
        }
  }
  System.out.println();
  
  for (k = strInput3.length() - 1; k >= 0; --k) {
        if (strInput3.equals("d") || strInput3.equals("done") || strInput3.equals("Done")) {
           break;
        }
        else {
        System.out.print(strInput3.charAt(k));
        }
  }

} }

java reverse
4个回答
0
投票

试试这个代码

 import java.util.Scanner;

public class ReverseString {
   public static void main(String[] args) {
      Scanner scanner = new Scanner(System.in);
      System.out.print("Enter a string: ");
      String str = scanner.nextLine();
      
      // Convert the string to a character array
      char[] chars = str.toCharArray();
      
      // Loop through the characters in reverse order and print them
      for (int i = chars.length - 1; i >= 0; i--) {
         System.out.print(chars[i]);
      }
      
      System.out.println(); // Print a newline character at the end
   }
}

0
投票

我给你做了一个完整的程序,它会一直要求反转行,直到他找到“D”或“d”并反转它然后存在。

Check the program output image here

    import java.util.*;

    public class HelloWorld {

        public static String Reverse(String content) {
            String tnetnoc = "";

            if (content.contains(" d"))
                content = content.split(" d")[0];
            else if (content.contains(" D"))
                content = content.split(" D")[0];

            System.out.println("We will reverse the line : ['" + content + "'] \n");

            int len = content.length();
            for (int i = 0; i < len; i++) {
                tnetnoc += content.charAt(len - i - 1);
            }

            System.out.println(tnetnoc);
            return tnetnoc;
        }

        public static void main(String[] args) {


            String line;
            while (true) {
                Scanner sc = new Scanner(System.in);
                System.out.println("\n");
                System.out.println("Enter line:");
                line = sc.nextLine();
                Reverse(line);

                if (line.contains(" d") || line.contains(" D")) {
                    System.out.println("Exiting the program now, GoodBye!");
                    break;

                }
            }
        }

    }

0
投票

我将您的代码复制并粘贴到 https://www.programiz.com/java-programming/online-compiler/ 但我没有发现您的代码有任何问题。也许您的ide配置有问题?你的JAVA环境设置了吗?


-1
投票
/*                                             
*This code is working fine for the same operation check it 
 out;                                                 
*/                              
                                                                                                 
import java.  util. Scanner;                      
public class prog{                               
public static void main(String[] args ) {                   
    Scanner sc = new Scanner(System.in);                       
    while(true){                                
        System.out.println("please enter the String");               
        String s=sc.nextLine();              
        for(int i=s.length()-1;i>=0;i--){               
            if(s.equals("d") | s.equals("done")){            
                System.exit(0);                           
            }                                               
            else{                               
                System.out.print(s.charAt(i));                                          
            }                                                                                                
        }                                                                                       

        System.out.println();                                                                
    }                                                                       
  }
}                                                                                                                                                                                         
© www.soinside.com 2019 - 2024. All rights reserved.