Question One
编写一个应用程序,输出100以内的全部偶数,并以10个一行的形式输出。
public class Main{
public static void main(String[] args){
int cnt = 0;
for(int i = 2; i <= 100; i += 2){
cnt ++;
System.out.print(i);
if(cnt % 10 != 0) System.out.print(' ');
else System.out.println();
}
}
}
Question Two
声明一些变量类型,用作练习
public class Main{
public static void main(String[] args){
byte a1 = 0x55;
byte a2 = 95;
System.out.println(a1 + " " + a2);
short b = 0x55ff;
System.out.println(b);
int i = 1000;
long l1 = 0xffff;
long l2 = 2147483648L;
System.out.println(l1 + " " + l2);
char c = 'a';
float f = 0.23F;
System.out.println(f);
double d1 = 0.007;
double d2 = 0.7E-3;
System.out.println(d1 + " " + d2);
boolean fg = false;
String s = "123";
}
}
Question Three
练习 switch 语句语法
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
switch (x * 2) {
case 2 :
System.out.println("Case One");
break;
case 3 :
System.out.println("case Two");
break;
case 4 :
System.out.println("case Three");
break;
default :
System.out.println("case Four");
break;
}
}
}
Question Four
随机数生成练习
public class Main{
public static void main(String[] args){
for(int i = 1; i <= 10; i ++){
int num = (int)(100 * Math.random());
System.out.print(num + " ");
}
System.out.println();
for(int i = 1; i <= 10; i ++){
// Random 生成 [0, 1) 的浮点数
double num = Math.random();
System.out.print(num + " ");
}
}
}
Question Five
输入 5 个整数求平均值
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double[] d = new double[5];
double avg = 0.0d;
for(int i = 0; i < 5; i ++){
System.out.println("请输入第" + (i + 1) + "个整数: ");
d[i] = sc.nextDouble();
avg += d[i];
}
System.out.println("平均分数是: " + avg / 5.0);
}
}
注意 : (同 C++ 的除法运算法则)
System.out.println(5 / 3); // 1
System.out.println(5.0 / 3); // 1.6666666666666667
Question Six
保留指定小数位数
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
System.out.printf("Two decimal places : %.2f", 5.0 / 3); // 1.6666666666666667
System.out.printf("%n");
System.out.printf("Three decimal places : %.3f", 5 / 3.0);
}
}
Question Seven
GUI 实现 a + b,加入异常抛出
import javax.swing.*;
public class Main {
public static void main(String[] args) {
try {
// 用InputDialog获取用户输入的第一个数字
int num1 = Integer.parseInt(JOptionPane.showInputDialog("Input first number:"));
// 用InputDialog获取用户输入的第二个数字
int num2 = Integer.parseInt(JOptionPane.showInputDialog("Input second number:"));
// 计算两个数字的和
int sum = num1 + num2;
// 显示结果,使用INFORMATION_MESSAGE以显示信息图标
JOptionPane.showMessageDialog(null, "Sum is " + sum,
"Result", JOptionPane.INFORMATION_MESSAGE);
}
catch (NumberFormatException e) {
// 捕获NumberFormatException并显示错误消息
JOptionPane.showMessageDialog(null, "Invalid input. " +
"Please enter a valid integer.", "Error", JOptionPane.ERROR_MESSAGE);
}
}
}
Question Eight
编写一个函数fun,它的功能是:求出1000以内能被7或11整除,但不能同时被7和11整除的数,存放到数组myarray中,fun函数返回该数组。在main()函数中输出该数组中的所有元素。
public class Main{
public static void main(String[] args) {
for(var x : fun()){
System.out.print(x + " ");
}
}
public static int[] fun(){
int cnt = 0;
int[] arr = new int[100];
for(int i = 1; i <= 100; i ++){
if((i % 7 == 0 || i % 11 == 0) && (i % 77 != 0)){
arr[cnt ++] = i;
}
}
return java.util.Arrays.copyOf(arr, cnt);
}
}
需要注意 :
1. 如果想直接返回数组,这样写 : return new int[] {1, 2, 3}; 不需要指定长度
2. 返回子数组这样写,return java.util.Arrays.copyOf(arr, cnt)
Question Nine
区间复制部分数组 + 顺便实现 9*9 Multiplication
import java.util.Arrays;
public class Main{
public static void main(String[] args) {
int[] arr = new int[] {1, 2, 3, 4, 5, 6, 7, 8};
for(int x : arr){
System.out.print(x + " ");
}
System.out.println();
int[] part = Arrays.copyOfRange(arr, 1, 4);
System.out.println(Arrays.toString(part));
Multiplication();
}
public static void Multiplication(){
for(int i = 1; i <= 9; i ++){
for(int j = 1; j <= i; j ++){
System.out.print(i + "*" + j + "=" + (i * j));
if(j == i) System.out.println();
else System.out.print(" ");
}
}
}
}