java - 初学者;方法和字符串

标签 java string

这是代码:

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/

相关文章:

java - 调用抛出异常的方法时,是否必须在 Java 中包含 catch block ?

Java http cookie

javascript - 选择函数的子集作为字符串并评估

c++ - 字符串插入 C++

Android 更改了 app_name 字符串,现在它崩溃了

java - 从 Java 客户端向我的 Whatsapp 发送消息

java - 服务器如何将数字证书传递给客户端/浏览器

java - 使用 jdbc 连接 MySQL 时出错 |命名异常

c - 在 C 中递归地反转字符串 - 仅使用指向字符串的指针

c++ - 在 C++ 中从用户输入中剥离/删除符号