[英]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
. 有几种方法可以帮助您解析用户输入,例如
nextInt
和nextLine
。 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.