定义一个类,它包含了一个int类型的变量x,若干个构造函数和一个输出方法show()。编程:从键盘输入一个两位以上的数,将这个数传递给这个的变量x,使用方法show()逆序输出这个数。各位大哥大姐帮帮我吧 刚学这个问题对于我来说还有些难度

解决方案 »

  1.   

    作业题啊?
    好像哪儿有个reverse()的方法来着……
      

  2.   


    import java.io.*;public class Nixu { int x; StringBuffer s;// 用StringBuffer类来实现倒序输出; public Nixu() {
    } public Nixu(int a, int b) {
    } public void shuru() {

    System.out.println("请输入整数,当输入exit时退出");
    BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));// 建立一个输入流,读取键盘输入字符
    while (true) {
    try {
    s = new StringBuffer(bf.readLine());
    x = Integer.parseInt(s.substring(0));
    if (s.substring(0).equalsIgnoreCase("exit"))
    break;

    show();
    System.out.println();//换行输入;
    } catch (IOException e) {
     
    e.printStackTrace();
    } catch (NumberFormatException e) {
    System.out.println("输入的非数字,请重新输入:");
    }
    } } public void show() { for (int i = s.length() - 1; i >= 0; i--) {
    System.out.print(s.charAt(i));
    }
    } public static void main(String args[]) {
    Nixu n = new Nixu(); n.shuru();
     
    }}已经运行调式过,应该可以满足你们的要求
      

  3.   

    import java.io.*;public class Nixu { int x; StringBuffer s;// 用StringBuffer类来实现倒序输出; public Nixu() {
    } public Nixu(int a, int b) {
    } public void shuru() {

    System.out.println("请输入整数,当输入exit时退出");
    BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));// 建立一个输入流,读取键盘输入字符
     
    while (true) {
    try {
    s = new StringBuffer(bf.readLine());

    if (s.substring(0).equalsIgnoreCase("exit"))
    break;
    x = Integer.parseInt(s.substring(0));
    show();
    System.out.println();//换行输入;
    } catch (IOException e) {
     
    e.printStackTrace();
    } catch (NumberFormatException e) {
    System.out.println("输入的非数字,请重新输入:");
    }
    } } public void show() { for (int i = s.length() - 1; i >= 0; i--) {
    System.out.print(s.charAt(i));
    }
    } public static void main(String args[]) {
    Nixu n = new Nixu(); n.shuru();
     
    }}
    发现有点错误,就是异常抛出的地方导致输入exit不退出,这样行了,把 x = Integer.parseInt(s.substring(0)); 放到跳出条件语句的后面