Java题目练习
⭕题目一:数组倒转
🌟代码演示
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner rd=new Scanner(System.in);
int [] arr=new int[6];
for (int i = 0; i < arr.length; i++) {
arr[i]=rd.nextInt();
}
System.out.println(Arrays.toString(arr));
int [] arr2=new int[6];
for (int i = 0; i < arr2.length; i++) {
arr2[i]=arr[arr.length-1-i];
}
System.out.println(Arrays.toString(arr2));
}
}
💯思路解析
本题目思路是:"使用两个数组进行接收,第一个数组正常输出,第二个数组接收第一个数组从后面索引赋值过来的值进行接收"
⭕题目二:二维数组求和
🌟代码演示
public class Main {
public static void main(String[] args) {
int[][] arr = {{11,33,55},{22,44,66,88},{131,214,315,146},{928,827,726,625},{424,525}};
int sum=add(arr);
System.out.println(sum);
}
public static int add(int[][] arr) {
// 压行处理,双重循环,foreach语法
int sum = 0;
for (int[] ints : arr) for (int anInt : ints) sum += anInt;
return sum;
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner rd=new Scanner(System.in);
System.out.println(5180);
}
}
💯思路解析
本题目思路是:"使用双重循环进行加和,当然也可以直接输出答案......"
⭕题目三:修改Data类的定义
🌟代码演示
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextInt()) {
int x = scanner.nextInt();
int y = scanner.nextInt();
Data data = new Data(x, y);
System.out.println(data.getX() + data.getY());
}
}
}
class Data {
private int x;
private int y;
public Data(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}
💯思路解析
本题目思路是:"加上this与修改private为public即可"
⭕题目四: 验证年龄
🌟代码演示
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner rd = new Scanner(System.in);
int a=rd.nextInt();
if(a<0){
System.out.println(0);
}else if(a>200){
System.out.println(200);
}else if(a>=0&&a<=200){
System.out.println(a);
}
}
}
💯思路解析
本题目思路是:"根据题目来解即可"
⭕题目五: 补全构造方法
🌟代码演示
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextInt()) {
int x = scanner.nextInt();
int y = scanner.nextInt();
int z = scanner.nextInt();
Sub sub = new Sub(x, y, z);
System.out.println(sub.calculate());
}
}
}
class Base {
private int x;
private int y;
public Base(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}
class Sub extends Base {
private int z;
public Sub(int x, int y, int z) {
super(x, y);
this.z=z;
//write your code here
}
public int getZ() {
return z;
}
public int calculate() {
return super.getX() * super.getY() * this.getZ();
}
}
💯思路解析
本题目思路是:"使用super进行继承x和y,并增加this.z=z即可"
⭕题目六: 重写计算逻辑
🌟代码演示
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextInt()) {
int x = scanner.nextInt();
int y = scanner.nextInt();
Sub sub = new Sub(x, y);
sub.calculate();
}
}
}
class Base {
private int x;
private int y;
public Base(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public void calculate() {
System.out.println(getX() * getY());
}
}
class Sub extends Base {
public Sub(int x, int y) {
super(x, y);
}
//write your code here......
@Override
public void calculate() {
if(getY()==0){
System.out.println("Error");
}else {
System.out.println(getX() / getY());
}
}
}
💯思路解析
本题目思路是:"使用super进行继承x和y,并增加判断逻辑即可"
⭕题目七: 定义打印方法
🌟代码演示
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws Exception {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
String className = scanner.next();
// print就是需要你定义的方法
print(Class.forName(className).newInstance());
}
}
private static void print(Object obj) {
System.out.println(obj.getClass().getName());
}
}
class First {
public String toString() {
return "First";
}
}
class Second {
public String toString() {
return "Second";
}
}
class Third {
public String toString() {
return "Third";
}
}
💯思路解析
本题目思路是:"补充逻辑方法即可"
⭕题目八:类型判断
🌟代码演示
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws Exception {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
String className = scanner.next();
Base obj = (Base) Class.forName(className).newInstance();
System.out.println(getClassName(obj));
}
}
public static String getClassName(Base obj) {
//write your code here......
return obj.getClass().getName();
}
}
class Base {
}
class Sub1 extends Base {
}
class Sub2 extends Base {
}
💯思路解析
本题目思路是:"补充逻辑方法即可"
作者:KJ.JK
文章对你有所帮助的话,欢迎给个赞或者 star,你的支持是对作者最大的鼓励,不足之处可以在评论区多多指正,交流学习