英文:
I need help understanding how this code works
问题
我需要一些帮助来理解这段代码。不同部分的含义是什么,尤其是带有try函数的部分?
public static void main(String[] args) {
try (Scanner scanner = new Scanner(System.in)) {
System.out.print("Enter a number: ");
int num = scanner.nextInt();
if (num % 2 == 0)
System.out.println(num + " is even");
else
System.out.println(num + " is odd");
}
}
英文:
I need some help in understanding this code. what do the different parts say and so on especially the part with the try function?
public static void main(String[] args) {
try (Scanner scanner = new Scanner(System.in)) {
System.out.print("Enter a number: ");
int num = scanner.nextInt();
if (num % 2 == 0)
System.out.println(num + " is even");
else
System.out.println(num + " is odd");
}
}
答案1
得分: 2
// 使用 try-with-resources 语句确保在块结束时关闭 System.in 流
try (Scanner scanner = new Scanner(System.in)) {
// 在控制台打印一行
System.out.print("输入一个数字:");
// 等待直到在控制台输入内容并且用户按下回车
// 然后将输入解析为整数值
int num = scanner.nextInt();
// 获取输入数字除以 2 的余数。
// 所以 3 % 2 == 1, 2 % 2 == 0
if (num % 2 == 0)
System.out.println(num + " 是偶数");
else
System.out.println(num + " 是奇数");
}
英文:
// The try-with-resources statement ensures that the System.in stream is closed at the end of the block
try (Scanner scanner = new Scanner(System.in)) {
// print a line to the console
System.out.print("Enter a number: ");
// wait till input is typed into the console and the user pressed enter
// and parse the input as an integer value
int num = scanner.nextInt();
// get the remainder of a division of the input number by 2.
// so 3 % 2 == 1, 2 % 2 == 0
if (num % 2 == 0)
System.out.println(num + " is even");
else
System.out.println(num + " is odd");
}
答案2
得分: 1
根据Roddy所示,该块是一个try-with-resources块。当程序进入try
块时,它生成以下资源:
Scanner scanner = new Scanner(System.in);
唯一的约束是实例化的资源必须实现java.lang.AutoCloseable
接口,以便在try
块执行完毕后可以关闭。
英文:
As indicated by Roddy, that block is a try-with-resources. When the program enters the try
block, it generates the following ressource:
Scanner scanner = new Scanner(System.in);
The only constraint is that the instantiated ressource must implement java.lang.AutoCloseable
, so that it can be closed after the try
block is finished.
答案3
得分: 1
public static void main(String[] args) {
try(Scanner scanner = new Scanner(System.in))
{
System.out.print("Enter a number: ");
int num = scanner.nextInt();
if(num % 2 == 0)
System.out.println(num + " is even");
else
System.out.println(num + " is odd");
}
}
Scanner scanner = new Scanner(System.in)
- 创建一个 Scanner 类型的实例,其以输入流作为参数。
System.in
- 返回标准输入流。(标准输入是程序从中读取输入数据的流)。
try() { }
- 这在 Java 中称为 try-with-resources 语句,它声明了一个或多个资源的尝试,这些资源在程序终止后需要关闭,以安全释放系统资源。大括号表示一个代码块。
System.out.println();
- 可以将字符、字符串、布尔值或字符数组打印到控制台。
在这个例子中,System.out.println(num + " is even");
会在 num 的值为 2 时打印出 "2 is even"。
关于 println 方法可以根据传递给它的参数类型打印什么内容的列表,请参阅这个链接:https://docs.oracle.com/javase/8/docs/api/java/io/PrintStream.html#println
int num = scanner.nextInt();
- 这将扫描在控制台上键入的数字,并将其作为整数赋值给变量 num。
nextInt()
方法的使用方式可以在这里找到:https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html
num % 2
- 将变量 num 中的值除以 2,并得到余数。如果 num 是 3,则返回的余数是 1。
num % 2 == 1
- 检查 num % 2 返回的值是否等于 1,如果相等则返回 true,否则返回 false。如果条件满足,则进入 if 代码块,并在控制台上打印 num 为偶数。如果条件不满足,则在控制台上打印 num 为奇数。
<details>
<summary>英文:</summary>
public static void main(String[] args) {
try(Scanner scanner = new Scanner(System.in))
{
System.out.print("Enter a number: ");
int num = scanner.nextInt();
if(num % 2 == 0)
System.out.println(num + " is even");
else
System.out.println(num + " is odd");
}
}
`Scanner scanner = new Scanner(System.in)` - Create an instance of type Scanner, which takes an input stream as argument.
`System.in` - returns the standard input stream. (Standard input is a stream from which a program reads its input data).
`try() { }` - This is called a try-with-resources statement in java, that declares try with one or more resources, that will need to be closed after the program is terminated to safely release the system resources. The open and closed curly braces makes it a block.
`System.out.println();` - This can print a character, a string, a boolean or an array of characters to the console.
In this case `System.out.println(num + " is even");` prints out 2 is even if the value of num is 2.
For a list of what the println method can print depending on the type of argument passed to it, please refer this https://docs.oracle.com/javase/8/docs/api/java/io/PrintStream.html#println
`int num = scanner.nextInt();` - This will scan the number typed on the console as an int and assigns it to the variable num.
`nextInt()` method used can be found here - https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html
`num % 2` - divides the value in the variable num by 2 and gives the reminder. If num is 3, then the reminder returned is 1.
`num % 2` == 1 - checks if value returned by num % 2 is equal to 1, returns true if equal and false if not. If the condition satisfies then it enters into if block and prints to the console that the num is even. If the condition does not satisfy it prints to console that the num is odd.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论