Java源码-将一句话倒过来说(Displaying a Sentence with Its Words Reversed)

英语中词和词之间是有空格作为分隔符的,所以每句话都可轻松的拆分成各个单词,然后逆序输出,就是倒过来说了。

对于中文,要这样玩必须在输入时人为的分词(如,在词、短语之间加上空格)。

如果分词的间隔拉长,反过来说也能大概猜出原句的意思;但是,如果拆的很细,那就变天书了,呵呵。


运行结果:

请输入一句话(短语之间用空格分隔):我们 曾经 天真的 以为 努力 一定 会有 回报 后来才明白 世界上 没有一定这回事
这句话到过来说是这样的:没有一定这回事 世界上 后来才明白 回报 会有 一定 努力 以为 天真的 曾经 我们


代码如下:

import java.util.*;
/**Java how to program, 10th edition
 14.9 (Displaying a Sentence with Its Words Reversed) Write an application that
  inputs a line of text, tokenizes the line with String method split and outputs
   the tokens in reverse order. Use space characters as delimiters.
 *  @author pandenghuang@163.com*/
public class ReverseSentence 
{
   public static void main(String[] args)
   {
	   Scanner input=new Scanner(System.in);
	   System.out.print("请输入一句话(短语之间用空格分隔):");
	   String s=input.nextLine();
	   String[] tokens=s.split(" ");
	   
	   String reversed="";
	   for (int i=tokens.length-1;i>=0;i--)
		   reversed+=tokens[i]+" ";
	   System.out.printf("这句话到过来说是这样的:");
		   System.out.println(reversed);
   } 
}



Certainly! I can help you write a comprehensive Java program that includes all the common features of Object-Oriented Programming (OOP) and displays the results of variables using methods. Here's an example program: ```java public class OOPExample { // Define a class public static class Person { // Define instance variables private String name; private int age; // Define constructor public Person(String name, int age) { this.name = name; this.age = age; } // Define instance methods public void sayHello() { System.out.println("Hello, my name is " + name); } public void displayAge() { System.out.println("I am " + age + " years old"); } } // Define another class that inherits from Person public static class Student extends Person { private String major; public Student(String name, int age, String major) { super(name, age); this.major = major; } public void displayMajor() { System.out.println("I am majoring in " + major); } } // Main method public static void main(String[] args) { // Create an instance of Person and call methods Person person = new Person("John", 25); person.sayHello(); person.displayAge(); // Create an instance of Student and call methods Student student = new Student("Alice", 20, "Computer Science"); student.sayHello(); student.displayAge(); student.displayMajor(); } } ``` In this program, we define a `Person` class with instance variables `name` and `age`, as well as instance methods `sayHello()` and `displayAge()`. We also define a `Student` class that inherits from `Person` and adds an additional instance variable `major` and a method `displayMajor()`. In the `main` method, we create an instance of `Person` and call its methods to display the name and age. Then, we create an instance of `Student` and call its methods to display the name, age, and major. When you run this program, it will output the following: ``` Hello, my name is John I am 25 years old Hello, my name is Alice I am 20 years old I am majoring in Computer Science ``` This example program demonstrates the common features of OOP in Java, including classes, objects, instance variables, constructors, and instance methods. It also shows how to use methods to display the results of variables.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值