简体   繁体   English

用于替换打印语句和扫描仪输入的 Java 类

[英]Java class to replace print statement and scanner input

Good Evening All,大家晚上好,

I need help writing a class that enables to write a code like:我需要帮助编写一个可以编写如下代码的类:

int number  = Utility.getInt("Enter an int", '>');

to replace something like:替换类似的东西:

System.out.println("Enter an int>");
Scanner scan = new Scanner(system.in);
int number = scan.nextInt();

Please find my solution below.请在下面找到我的解决方案。 I am not sure if I am understanding the prompt correctly, not sure if I need to use a Java pre-defined Utility class or if I just need to name the class Utility.我不确定我是否正确理解了提示,不确定我是否需要使用 Java 预定义的 Utility 类,或者我是否只需要命名类 Utility。 Any help or tip would be greatly appreciated thanks!任何帮助或提示将不胜感激,谢谢!

public class Utility {
private int number; 
public Auto setInt (int number){
return "Enter an int" + number;
if (number>0)
   this.number = newNumber;
return this;
}
public int getInt(){
return newNumber;
}
}

Have you tried static methods like below ?您是否尝试过像下面这样的静态方法?

public class Utility {
    public static Integer getInt(String message){
        System.out.println(message);
        Scanner scan = new Scanner(System.in);
        Integer number = scan.nextInt();
        return number;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM