import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int num1 = sc.nextInt(); int num2 = sc.nextInt(); /********* Begin *********/ System.out.println(num1/num2); /********* End *********/ } }
时间: 2023-07-18 07:44:30 浏览: 47
这段 Java 代码的作用是读取用户输入的两个整数,然后输出它们的商。如果输入的第二个数为0,则会抛出 `ArithmeticException` 异常,导致程序终止。为了避免这种情况,可以使用 `try-catch` 结构来处理异常,如下所示:
```
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num1 = sc.nextInt();
int num2 = sc.nextInt();
try {
int result = num1 / num2;
System.out.println(result);
} catch (ArithmeticException e) {
System.out.println("除数不能为0!");
}
}
}
```
在上面的代码中,`try` 块中的代码会尝试计算 `num1/num2` 的值,如果 `num2` 为0,则会抛出 `ArithmeticException` 异常,此时程序会跳转到 `catch` 块中,输出相应的提示信息。这样就可以避免程序因为除数为0而终止。
相关问题
import java.util.Scanner; public class Test1 { 晋涛 public static void main(String[] args) { Sy
import java.util.Scanner;
public class Test1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入您的姓名:");
String name = scanner.nextLine();
.out.println("您好," + name + "!");
}
}
这段代码是一个简单的Java程序,它使用了Java标准库中的Scanner类来实现用户输入和输出。首先,我们通过创建Scanner对象来获取用户的输入。然后,使用nextLine()方法读取用户输入的姓名,并将其存储在name变量中。最后,通过System.out.println()方法将问候语和用户的姓名输出到控制台。
import java.io.*; import java.util.Arrays; import java.util.Scanner; public class FileTest { public static void main(String[] args) throws IOException { // 接收给定的一行字符串 Scanner scanner = new Scanner(System.in); String line = scanner.nextLine();
// 创建文件对象,并检查文件是否存在 File file = new File(line); if (!file.exists()) { System.out.println("文件不存在!"); return; } // 读取文件内容 BufferedReader reader = new BufferedReader(new FileReader(file)); String content = ""; String temp; while ((temp = reader.readLine()) != null) { content += temp + "\n"; } // 关闭文件读取器 reader.close(); // 输出文件内容 System.out.println("文件内容:\n" + content); } }
这段代码是一个读取文件并输出文件内容的Java程序。首先,通过Scanner接收用户输入的文件路径,然后判断文件是否存在。如果文件不存在,则输出提示信息并结束程序。如果文件存在,则创建文件读取器并读取文件内容,将读取到的内容存储在一个字符串变量中。最后,关闭文件读取器并输出文件内容。
相关推荐
![text/x-java](https://img-home.csdnimg.cn/images/20210720083646.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)