-3

这个代码是一个类,它在屏幕上垂直显示我的名字,每个字母使用我输入的最多 5 行 5 列的字符,每个字母之间有一个空行。

它工作得很好,但问题是我对字符串使用了静态,我的教授说代码中的任何地方都不应该是静态的,而是主要的。当我从字符串 c 中删除静态时,即使声明字符串 c,它也会破坏代码内部主要

public class Lab3
{

    static String c;

    public static void main(String[] args)
    {

        Lab3 classy = new Lab3();
        Scanner input = new Scanner(System.in);

        System.out.print("Which character would you like to display?");

        c = input.next();

        System.out.println("My name is Jonathan");
        classy.displayLetterJ();
        classy.displayLetterO();
        classy.displayLetterN();
        classy.displayLetterA();
        classy.displayLetterT();
        classy.displayLetterH();
        classy.displayLetterA();
        classy.displayLetterN();

    }//end of main

    public void displayLetterJ()
    {
        System.out.println("  " + c + c + c);
        System.out.println("    " + c);
        System.out.println("    " + c);
        System.out.println(c + "   " + c);
        System.out.println(c + c + c + c + c);

    }

    public void displayLetterA()
    {
        System.out.println();
        System.out.println("   " + c);
        System.out.println("  " + c + " " + c);
        System.out.println(" " + c + " " + c + " " + c);
        System.out.println(c + "     " + c);
        System.out.println(c + "     " + c);

    }

    public void displayLetterO()
    {
        System.out.println();
        System.out.println("  " + c + c);
        System.out.println(" " + c + "  " + c);
        System.out.println(c + "    " + c);
        System.out.println(c + "   " + c);
        System.out.println("  " + c + c);

    }

    public void displayLetterN()
    {
        System.out.println();
        System.out.println(c + "    " + c);
        System.out.println(c + " " + c + "  " + c);
        System.out.println(c + "  " + c + " " + c);
        System.out.println(c + "   " + c + c);
        System.out.println(c + "    " + c);

    }

    public void displayLetterH()
    {//H accessor
        System.out.println();
        System.out.println(c + "   " + c);
        System.out.println(c + "   " + c);
        System.out.println(c + c + c + c + c);
        System.out.println(c + "   " + c);
        System.out.println(c + "   " + c);

    }

    public void displayLetterT()
    {
        System.out.println();
        System.out.println(c + c + c + c + c);
        System.out.println("  " + c);
        System.out.println("  " + c);
        System.out.println("  " + c);
        System.out.println("  " + c);

    }
}
4

7 回答 7

0

我认为您应该使用私有实例字段c,例如公共 getter 和 setter:

public class Lab3 {

   private String c;

   public void setC(String c) 
   {
        this.c = c;
   }

   public String getC() 
   {
        return this.c;
   }

....


}

然后你可以在你的主要方法中说:

Lab3 classy  = new Lab3();
Scanner input=new Scanner(System.in);

System.out.print("Which character would you like to display?");

classy.setC(input.next());
于 2013-02-06T15:58:04.030 回答
0

我不知道这样做的目的是什么......但这应该在没有静态的情况下工作

import java.util.Scanner;


/**
 *
 * @author john
 */
 public class Lab3 {


 public static void main(String[] args) {
     String c;
     Lab3 classy  = new Lab3();
     Scanner input=new Scanner(System.in);

          System.out.print("Which character would you like to display?");

          c = input.next();

        System.out.println("My name is Jonathan");
        classy.displayLetterJ(c);
        classy.displayLetterO(c);
        classy.displayLetterN(c);
        classy.displayLetterA(c);
        classy.displayLetterT(c);
        classy.displayLetterH(c);
        classy.displayLetterA(c);
        classy.displayLetterN(c);

      }//end of main
public void displayLetterJ(String c) {
    System.out.println("  "+c+c+c);
    System.out.println("    " +c);
    System.out.println("    " +c);
    System.out.println(c + "   " + c);
    System.out.println(c+ c+ c + c +c);

 }
 public void displayLetterA(String c){
    System.out.println();
    System.out.println("   "+c);
    System.out.println("  "+c+" "+c);
    System.out.println(" "+c+" "+c+" "+c);
    System.out.println(c+"     "+c);
    System.out.println(c+"     "+c);

}
  public void displayLetterO(String c){
    System.out.println();
    System.out.println("  "+c+c);
    System.out.println(" "+c+"  "+c);
    System.out.println(c+"    "+c);
    System.out.println(c+"   "+c);
    System.out.println("  "+c+c);

}
  public void displayLetterN(String c){
    System.out.println();
    System.out.println(c+"    "+c);
    System.out.println(c+" "+c+"  "+c);
    System.out.println(c+"  "+c+" "+c);
    System.out.println(c+"   "+c+c);
    System.out.println(c+"    "+c);

}
 public void displayLetterH(String c){//H accessor
    System.out.println();
    System.out.println(c+"   "+c);
    System.out.println(c+"   "+c);
    System.out.println(c+c+c+c+c);
    System.out.println(c+"   "+c);
    System.out.println(c+"   "+c);

}
  public void displayLetterT(String c){
    System.out.println();
    System.out.println(c+c+c+c+c);
    System.out.println("  "+c);
    System.out.println("  "+c);
    System.out.println("  "+c);
    System.out.println("  "+c);

    }

}

于 2013-02-06T15:58:29.477 回答
0

改变

public class Lab3 {
   static String c;

public class Lab3 {
     private String c;   
     public void setC(String c){this.c=c;}

就像是:

public static void main(String[] args) {

     Lab3 classy  = new Lab3();
     Scanner input=new Scanner(System.in);
     c = input.next();
     classy.setC(c);

     .....
    }
于 2013-02-06T15:59:18.227 回答
0

主要问题是,如果一个变量不是,static那么它会附加到一个类的实例上,因此你不能从static上下文中引用它。这是因为非静态变量表示特定实例的一种状态。

main()是静态的,所以c = ..是非法的。尝试使用,classy.c = ..以便您将参考您在静态方法中创建String的实例的具体情况。Lab3

于 2013-02-06T15:53:12.630 回答
0

有几种方法可以解决这个问题,一种可能是:在 main 方法中定义 c 字符串,然后将 c 字符串作为参数传递给所有方法。

其他方法可以将 c String 定义为类属性(非静态),然后在您的方法中使用“this.c”来引用 c String。

1-如果您将属性声明为静态,则意味着该属性将与该类的所有实例共用。非静态属性只是当前类实例的一部分。

于 2013-02-06T15:53:43.713 回答
0

仅在方法 main 内初始化字符串

字符串 c = input.next();

于 2013-02-06T15:55:53.457 回答
0

您应该重构您的方法以这种方式使用字符 c

public void displayLetterJ(String c)
{
   System.out.println("  "+c+c+c);
   System.out.println("    " +c);
   System.out.println("    " +c);
   System.out.println(c + "   " + c);
   System.out.println(c+ c+ c + c +c);
}

并从 main 调用

classy.displayLetterJ("*");
于 2013-02-06T15:56:25.350 回答