有人可以帮我修复此代码吗? (Java方法)[关闭]

问题描述 投票:-3回答:7

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

它正常工作,但问题是我对字符串使用了static,而我的教授说,代码中的任何地方都不应是static,而是main。当我从字符串c中删除static时,即使声明,它也会破坏代码main内部的字符串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);

    }
}
java methods static
7个回答
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);

    }

}


0
投票

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

您的main()是静态的,因此c = ..是非法的。尝试使用classy.c = ..,以便您将引用在静态方法中创建的String实例的特定Lab3


0
投票

仅在方法main内部初始化String

String c = input.next();


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的形式从main调用>

classy.displayLetterJ("*");
    

0
投票

我认为您应该像这样使用私有实例字段c以及公共获取器和设置器:


0
投票

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


0
投票

更改

© www.soinside.com 2019 - 2024. All rights reserved.