将一个英文语句以单词为单位逆序排放。例如“I am a boy”,逆序排放后为“boy a am I”
所有单词之间用一个空格隔开,语句中除了英文字母外,不再包含其他字符
数据范围:输入的字符串长度满足
注意本题有多组输入
将一个英文语句以单词为单位逆序排放。例如“I am a boy”,逆序排放后为“boy a am I”
输入一个英文语句,每个单词用空格隔开。保证输入只包含空格和字母。
得到逆序的句子
I am a boy
boy a am I
nowcoder
nowcoder
public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextLine()) { // 注意 while 处理多个 case String a = in.nextLine(); String[] arr = a.split(" "); for (int i = arr.length - 1; i >= 0; i--) { System.out.print(arr[i]); if (i > 0) { System.out.print(" "); } } } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String[] input = in.nextLine().split(" "); for(int i=input.length-1;i>=0;i--) { System.out.print(input[i]+" "); } } }
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String str = in.nextLine(); String[] arr = str.split(" "); StringBuilder builder = new StringBuilder(); for (int i = arr.length - 1; i >= 0; i--) { builder.append(i == 0 ? arr[i] : arr[i] + " "); } System.out.println(builder.toString()); } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 String str = in.nextLine(); if(str.contains(" ")){ String [] strArr = str.split(" "); for(int i = strArr.length - 1; i >= 0 ; i--){ System.out.print(strArr[i] + " "); } }else{ System.out.print(str); } } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String s = sc.nextLine(); String[] split = s.split(" "); for (int i = split.length-1; i >=0 ; i--) { System.out.print(split[i]+" "); } } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String[] strArray = in.nextLine().split(" "); for (int i = strArray.length - 1; i >= 0; i--) { if (i == strArray.length - 1) { System.out.print(strArray[i]); continue; } System.out.print(" " + strArray[i]); } } }
import java.util.Scanner; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String input = br.readLine(); String[] split = input.split(" "); for (int i = split.length - 1; i >= 0; i--) { System.out.print(split[i] + " "); } } }
import java.util.Scanner; import java.util.Stack; // Stack栈 empty判空/push入栈/pop出栈/peek获取栈顶值/search查看是否在栈中 public class Main { public static void main(String[] args) throws Exception{ Scanner in = new Scanner(System.in); Stack<String> stack = new Stack<String>(); while(in.hasNext()){ stack.push(in.next()); } while(!stack.empty()){ System.out.print(stack.pop()+" "); } } }