简体   繁体   English

如何创建一个接受用户输入的Java程序?

[英]How to create a Java Program that accepts user inputs?

I'm solving multiple exercises online and I cant answer this one. 我正在网上解决多项练习,我无法回答。 The task is to create a program that accepts an input with format of TITLE-CHARACTER-YEAR and prints out the character's name and the manga's year category as indicated below 任务是创建一个程序,该程序接受TITLE-CHARACTER-YEAR格式的输入,并打印出角色名称和漫画的年份类别,如下所示

year less than 2000 and print "90s"
2000 less than or equal to year but less than 2006 and print "early 2000s"
2006 less than or equal to year and print "latest"

I tried coding it but I'm lacking of logical thinking on how can I run it right. 我尝试对其进行编码,但是我缺乏如何正确运行的逻辑思考。 Tried searching syntaxes but failed. 尝试搜索语法,但失败。

public class HelloWorld {

    public static void main(String[] args) {
        String title1 = "Yuyu Hakusho";
        String title2 = "Bleach-Ichigo";
        String title3 = "Bakuman";
        String name1 = "Eugene";
        String name2 = "Ichigo Kurosaki";
        String name3 = "Moritaka Mashiro";
        int year1 = 1994;
        int year2 = 2004;
        int year3 = 2008;


        if (year1 < 2000);
        System.out.println(name1 + " 90s");    
    }
}
if (year1 < 2000);
   System.out.println(name1 + " 90s");

Is equivalent to: 等效于:

if (year1 < 2000) { }
System.out.println(name1 + " 90s"); //will be always executed

Remove the redundant ; 删除多余的; after the if statement: if语句之后:

if (year1 < 2000); 
                 ↑

Now the other thing you need to have in your code is a Scanner . 现在您需要在代码中包含的其他内容是Scanner Go through the docs to understand how to use it. 浏览文档以了解如何使用它。

To allow your application reading from user input you have several ways. 为了允许您的应用程序从用户输入中读取,您有几种方法。 The easiest would be using Scanner class. 最简单的方法是使用Scanner类。 Here's an example: 这是一个例子:

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    System.out.print("Hello, please tell me your name: ");
    String name = Scanner.nextLine();
    System.out.println("Hello " + name);
}

There are several methods that will help you parsing the user input like nextInt and nextLine . 有几种方法可以帮助您解析用户输入,例如nextIntnextLine For more info about them, check the proper Javadoc linked at the beginning of this post. 有关它们的更多信息,请检查本文开头链接的正确的Javadoc。


Apart of that, be careful when writing your block statements, like your if : 除此之外,在编写block语句时要小心,例如if

if (year1 < 2000);

Above means that there's nothing to do in case the int variable year1 is less than 2000 . 以上表示如果int变量year1小于2000 ,则无需执行任何操作。

Try this one. 试试这个。

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    System.out.print(" please tell input with format of TITLE-CHARACTER-YEAR ");
    String input = scanner.nextLine(); //reads the input from console 
    String arr[] = new String[3]; // size ur wish
    arr=input.split("-"); //splits the input with the - delimiter into array of strings
    String name=arr[1]; //contains character
    int year=Integer.parseInt(arr[2]); //contains year
    if(year<2000)
        System.out.println( name + " 90's");
    else if(year>=2000 && year<2006)
        System.out.println(name + " early 2000's");
    else if(year>=2006)
        System.out.println(name + " latest");
}

There are also many other ways to do it, the simple and easy to understand is this 还有很多其他方法可以做到,简单易懂的是

For codingbat.com check out this one. 对于codingbat.com,请查看此内容。

public String methodName(String input){
     String arr[] = new String[3]; // size ur wish
    arr=input.split("-"); //splits the input with the - delimiter into array of strings
    String name=arr[1]; //contains character
    int year=Integer.parseInt(arr[2]); //contains year
    if(year<2000)
       return  name + " 90's";
    else if(year>=2000 && year<2006)
        return name + " early 2000's";
    else if(year>=2006)
       return name + " latest";
    else 
       return "wrong format";
 }

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

相关问题 如何在 powershell 脚本中为 java 程序传递用户输入 - How to pass user inputs for a java program in powershell script Java编程:如何使接受用户输入的for循环程序正确减去输入? - Java programming: How do I make this for loop program that accepts user input subtract the input properly? Java程序接受用户输入的问题 - Problem with java program taking user inputs 如何创建一个在Java中接受回调函数的方法? - How to create a method that accepts a callback function in java? 如何编写一个程序来反转用户输入的数字 - How to write a program that reverses a number a user inputs 当用户输入“结束”时如何结束这个程序? - How to end this program when user inputs “end”? 如何让程序创建自己的变量以匹配(无限)用户输入? - How do I have a program create its own variables to match (up to an infinite) amount of user inputs? 用户输入以创建Java程序 - user input to create java program 如何将所有用户输入存储在 do while 循环中,并在 Java 程序中稍后调用它们进行打印? - How to store all the user inputs in do while loop, and call them to print later in Java program? 如何在Java中输入(除非用户输入0以终止程序)? - How do I get while input (except when the user inputs 0 that terminates the program) in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM