我目前正在开展一个学校项目,我想我可以调用我的程序来调用一个非静态方法,这个方法仍然可以向前和向后移动我当前的数字。我很茫然。我该怎么办?这是我到目前为止的编码,一切都工作减去实现非静态选项。
import java.util.Scanner;
public class PictureViewer {
final static int MAX_NUMBER = 8;
final static int MIN_NUMBER = 1;
public static void main(String[] args) {
showMenu();
}
public static void showMenu() {
Scanner input = new Scanner(System.in);
int current_number = MIN_NUMBER;
boolean Continuation = true;
while (Continuation) {
System.out.println("Select One of the Options");
System.out.println("Option 1: Forward");
System.out.println("Option 2: Backward");
System.out.println("Option 3: CreateFileName");
System.out.println("Option 4: CreateRandomName");
System.out.println("Option 5: Non-Static Forward");
int Menu_Option = input.nextInt();
switch (Menu_Option) {
case 1:
current_number = forward(current_number);
System.out.println("The Current Number is: " + current_number);
break;
case 2:
current_number = backward(current_number);
System.out.println("The Current Number is: " + current_number);
break;
case 3:
String fileName = createFileName(current_number);
System.out.println("File Name: " + fileName);
break;
case 4:
fileName = createRandomName(current_number);
System.out.println("File Name: " + fileName);
break;
}
}
}
public static int forward(int current_number) {
if (current_number >= MAX_NUMBER) {
System.out.println(MIN_NUMBER);
current_number = MIN_NUMBER;
} else {
System.out.println(current_number++);
}
return current_number;
}
public static int backward(int current_number) {
if (current_number <= MIN_NUMBER) {
System.out.println(current_number);
current_number = MIN_NUMBER;
} else current_number --; {
}
return current_number;
}
public static String createFileName(int current_number) {
return "Picture" + current_number + ".gif";
}
public static String createRandomName(int current_number) {
current_number = MIN_NUMBER + (int) (Math.random() * MAX_NUMBER);
return "Picture" + current_number + ".gif";
}
public void forward () {
// overloaded method, use global variable as input and output
}
public void backward () {
// overloaded method, use global variable as input and output
}
}
答案 0 :(得分:2)
您无需将所有方法定义为static
,以便从main
进行调用。您可以在main
中创建一个对象并调用相同的方法。这就是我要做的事情:
main
除外)非静态(即删除static
关键字)更改main
方法实现以创建PictureViewer
类的对象并调用showMenu
,例如
public static void main(String[] args) {
new PictureViewer().showMenu();
}
答案 1 :(得分:0)
现在好了几乎所有事情,请详细说明你的问题,比如愿意学习的人,不仅“不工作”,只是免费的建议; - )
import java.util.Scanner;
public class PictureViewer {
final static int MAX_NUMBER = 8;
final static int MIN_NUMBER = 1;
private int currentNumber;
public static void main(String[] args) {
showMenu();
}
public static void showMenu() {
Scanner input = new Scanner(System.in);
int current_number = MIN_NUMBER;
boolean Continuation = true;
while (Continuation) {
System.out.println("Select One of the Options");
System.out.println("Option 1: Forward");
System.out.println("Option 2: Backward");
System.out.println("Option 3: CreateFileName");
System.out.println("Option 4: CreateRandomName");
System.out.println("Option 5: Non-Static Forward");
int Menu_Option = input.nextInt();
switch (Menu_Option) {
case 1:
current_number = forward(current_number);
System.out.println("The Current Number is: " + current_number);
break;
case 2:
current_number = backward(current_number);
System.out.println("The Current Number is: " + current_number);
break;
case 3:
String fileName = createFileName(current_number);
System.out.println("File Name: " + fileName);
break;
case 4:
fileName = createRandomName(current_number);
System.out.println("File Name: " + fileName);
break;
case 5:
PictureViewer pv = new PictureViewer();
pv.setCurrentNumber(current_number);
pv.forward();
current_number = pv.getCurrentNumber();
System.out.println("The Current Number is: " + current_number);
break;
}
}
}
public static int forward(int current_number) {
if (current_number >= MAX_NUMBER) {
System.out.println(MIN_NUMBER);
current_number = MIN_NUMBER;
} else {
System.out.println(current_number++);
}
return current_number;
}
public static int backward(int current_number) {
if (current_number <= MIN_NUMBER) {
System.out.println(current_number);
current_number = MIN_NUMBER;
} else
current_number--;
{
}
return current_number;
}
public static String createFileName(int current_number) {
return "Picture" + current_number + ".gif";
}
public static String createRandomName(int current_number) {
current_number = MIN_NUMBER + (int) (Math.random() * MAX_NUMBER);
return "Picture" + current_number + ".gif";
}
public void forward() {
// overloaded method, use global variable as input and output
this.currentNumber = this.currentNumber +1;
}
public void backward() {
// overloaded method, use global variable as input and output
}
public int getCurrentNumber() {
return currentNumber;
}
public void setCurrentNumber(int currentNumber) {
this.currentNumber = currentNumber;
}
}
答案 2 :(得分:0)
您不需要将所有方法设为静态只保留Main()静态。使用这个技巧,您将能够轻松访问非静态方法,这也将使您免于Critical Section问题.Follpwing是实现的步骤:
1.从除main()之外的所有方法中删除static关键字 2.的替换强>
showOptions()
。通过强>
new PictureViewer().showOptions();
希望这能解决您的问题:)