此代码是一个在屏幕上垂直显示我的名字的类,每个字母最多使用5行5列字符输入,每个字母之间有一个空行。
它运行得很好,但问题是我使用static作为字符串,我的教授说在代码中的任何地方都不应该有静态而是main。当我从字符串c中删除静态时,它会破坏代码,即使在main
中声明字符串cpublic 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);
}
}
答案 0 :(得分:0)
主要问题是,如果变量不是static
,那么它附加到类的实例,因此您无法从static
上下文中引用它。这是因为非静态变量表示特定实例的某种状态。
您的main()
是静态的,因此c = ..
是非法的。尝试使用classy.c = ..
,这样您就会引用您在静态方法中创建的String
实例的特定Lab3
。
答案 1 :(得分:0)
有几个方面可以解决这个问题,一个可能是: 在主mehod中定义c String,然后将c String作为参数传递给所有方法。
其他方法可以将c String定义为类属性(非静态),然后在方法中使用“this.c”来引用c String。
1-如果将属性声明为静态,则表示该属性将与该类的所有实例共用。 非静态属性仅是当前类实例的一部分。
答案 2 :(得分:0)
仅在方法main
中初始化StringString c = input.next();
答案 3 :(得分: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("*");
答案 4 :(得分: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());
答案 5 :(得分: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);
}
}
答案 6 :(得分:0)
更改
public class Lab3 {
static String c;
in
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);
.....
}