这是代码:
import java.util.Scanner;
public class sending {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
String text = giveMe(first);
System.out.println(text);
int x = scanner.nextInt();
x = number(x);
skrivUt(x);
}
//method for printing on screen
public static String giveMe(String first, String second){
first = ("Give me a number and I run down and add five to it");
second = ("Lol");
return first;
}
//method for doing math
public static int number(int x){
x = x + 5;
return x;
}
//method for printing out
public static void skrivUt(int x){
System.out.println(x);
}
}
如您所见,我对此很陌生,并且 main 方法和 GiveMe 方法有问题。
我想让 GiveMe 作为字符串集合工作,我可以在需要时调用它们。
但是当我尝试上面的示例时,我的 Eclipse 告诉我第六行“first 无法解析为变量”String text = GiveMe(first);
我做错了什么?
最佳答案
您正在尝试使用枚举,但从未声明过...在 Main 之外声明您的枚举。
enum s {FIRST, SECOND} //add this
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
String text = giveMe(s.FIRST); //add the s. so it knows to use your enum
System.out.println(text);
int x = scanner.nextInt();
x = number(x);
skrivUt(x);
}
然后你想修改你的方法以采用枚举,如下所示
public static String giveMe(s string) {
switch (string) {
case FIRST:
return "Give me a number and I run down and add five to it";
case SECOND:
return "Lol";
}
return "invalid string";
}
关于java - 初学者;方法和字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23139413/