减少字调试

时间:2016-01-05 17:11:50

标签: java

我应该这样做,例如它从包到ba到b。当我运行程序时,我没有得到任何错误代码,但它出现如下:

Enter a word :: happy

TriangleOne@5c647e05

所以我查看了该程序,但我发现它没有任何问题。任何帮助将不胜感激。提前谢谢!

import static java.lang.System.*;
import java.util.Scanner;

public class TriangleOne {
    private String word;

    public TriangleOne() {
        word="";
    }
    public TriangleOne(String s) {
        word = s;
    }
    public void setWord(String s) {
        word = s;
    }
    public void print( )
    {
        int length = word.length();
        for(int i = length; i >= 0 ; i --) {
            System.out.println(word.substring(0, i));
        }
    }
    public static void main ( String[] args )
    {
        Scanner keyboard = new Scanner(System.in);
        out.print("Enter a word :: ");
        String a =keyboard.nextLine();
        TriangleOne test = new TriangleOne(a);
        out.print(test);
        System.out.println("");
    }
}

1 个答案:

答案 0 :(得分:1)

您的代码非常完美,您只需调用方法print()即可打印所需的输出。我已修改您的代码,如下所示

import java.util.Scanner;


public class TriangleOne {


    private String word;


    public TriangleOne()
    {
        word="";
    }

    public TriangleOne(String s)
    {
    word = s;
    }

    public void setWord(String s)
    {
    word = s;
    }

    public void print( )
    {
        int length = word.length();
        for(int i = length; i >= 0 ; i --) {
            System.out.println(word.substring(0, i));
        }
    }
    public static void main ( String[] args )
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Enter a word :: ");
        String a =keyboard.nextLine();
        TriangleOne test = new TriangleOne(a);
        test.print();//modification for method call
        System.out.println("");
}}

示例输出 -

Enter a word :: happy
happy
happ
hap
ha
h

我希望它会对你有所帮助。