Public Class Exametest{ Public static Void Main(string[] args){ Try{ Returni }finally{ System.out.println("run Here?"); }}
时间: 2024-05-03 15:19:29 浏览: 181
这段代码有一些语法问题,可能无法通过编译。下面是修复后的代码:
```
public class Exametest {
public static void main(String[] args) {
try {
return;
} finally {
System.out.println("run Here?");
}
}
}
```
这段代码的作用是:在 try 块中使用 return 语句,无论 try 块是否抛出异常,finally 块中的代码都会被执行。在这个例子中,无论 try 块中的 return 语句是否执行,finally 块中的 System.out.println() 都会被执行,并输出 "run Here?"。
相关问题
(Rename files) Suppose you have a lot of files in a directory named Exercisei_j, where i and j are digits. Write a program that pads a 0 before i if i is a single digit. For example, a file named Exercise2_1 in a directory will be renamed to Exercise02_1. In Java, when you pass the symbol * from the command line, it refers to all files in the directory (see Supplement III.V). Use the following command to run your program: java Exercise12_28 *
Sure, I can help you with that! Here's a possible implementation in Java:
```java
import java.io.File;
public class Exercise12_28 {
public static void main(String[] args) {
if (args.length == 0) {
System.out.println("Usage: java Exercise12_28 *");
return;
}
String pattern = "Exercise%d_%d";
for (String filename : args) {
File file = new File(filename);
if (file.isFile() && file.getName().startsWith("Exercise")) {
String[] parts = file.getName().split("_");
if (parts.length == 2) {
try {
int i = Integer.parseInt(parts[0].substring(8));
int j = Integer.parseInt(parts[1]);
String newFilename = String.format(pattern, i < 10 ? 0 : "", i, j);
File newFile = new File(file.getParentFile(), newFilename);
if (file.renameTo(newFile)) {
System.out.printf("Renamed %s to %s%n", file.getName(), newFilename);
} else {
System.out.printf("Failed to rename %s to %s%n", file.getName(), newFilename);
}
} catch (NumberFormatException e) {
System.out.printf("Invalid filename format: %s%n", file.getName());
}
} else {
System.out.printf("Invalid filename format: %s%n", file.getName());
}
}
}
}
}
```
Here's how it works:
- The program takes command line arguments as filenames to be renamed, using the wildcard symbol `*` to match all files in the directory.
- It checks each file to see if it has a name starting with "Exercise" and containing an underscore character, which separates the two digits `i` and `j`.
- If the file name is valid, it parses `i` and `j` as integers and uses them to format a new filename with a leading zero before `i` if `i` is less than 10.
- Finally, it uses the `renameTo` method of the `File` class to rename the file to the new name, and prints a message to indicate success or failure.
To run this program, you can save it as `Exercise12_28.java` in the directory containing the files to be renamed, and then open a command prompt or terminal window in that directory and type:
```
javac Exercise12_28.java
java Exercise12_28 *
```
This will compile and run the program, renaming all matching files in the current directory.
阅读全文