Java题目练习
⭕题目一:实现抽象方法
🌟代码演示
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// Sub是需要你定义的子类
Base base = new Sub();
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextInt()) {
int x = scanner.nextInt();
int y = scanner.nextInt();
base.setX(x);
base.setY(y);
System.out.println(base.calculate());
}
}
}
abstract class Base {
private int x;
private int y;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int calculate() {
if (avg() == 0) {
return 0;
} else {
return sum() / avg();
}
}
/**
* 返回x和y的和
*/
public abstract int sum();
/**
* 返回x和y的平均值
*/
public abstract int avg();
}
class Sub extends Base {
/**
* 返回x和y的和
*/
@Override
public int sum() {
return getX()+getY();
}
/**
* 返回x和y的平均值
*/
@Override
public int avg() {
return (getX()+getY())/2;
}
//write your code here......
}
💯思路解析
本题目思路是:"补充逻辑增加两数相加即可"
⭕题目二:实现接口
🌟代码演示
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Comparator comparator = new ComparatorImpl();
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextInt()) {
int x = scanner.nextInt();
int y = scanner.nextInt();
System.out.println(comparator.max(x, y));
}
}
}
interface Comparator {
/**
* 返回两个整数中的最大值
*/
int max(int x, int y);
}
//write your code here......
class ComparatorImpl implements Comparator{
@Override
public int max(int x, int y) {
return x > y ? x:y;
}
}
💯思路解析
本题目思路是:"补充实现类增加判断大小的逻辑即可"
⭕题目三:重写父类方法
🌟代码演示
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);
System.out.println(sub.sum());
}
}
}
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 final int getY() {
return y;
}
public final int sum() {
return getX() + getY();
}
}
class Sub extends Base {
public Sub(int x, int y) {
super(x, y);
}
//write your code here......
@Override
public int getX() {
return super.getX()*10;
}
}
💯思路解析
本题目思路是:"重写方法即可"
⭕题目四:创建单例对象
🌟代码演示
public class Main {
public static void main(String[] args) {
Singleton s1 = Singleton.getInstance();
Singleton s2 = Singleton.getInstance();
System.out.println(s1 == s2);
}
}
class Singleton {
private static Singleton instance;
private Singleton() {
}
//write your code here......
public static Singleton getInstance() {
if (instance == null) {
synchronized (Singleton.class) {
if (instance == null) {
instance = new Singleton();
}
}
}
return instance;
}
}
💯思路解析
本题目思路是:"就是经典的懒汉设计模式"
⭕题目五:动态字符串
🌟代码演示
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String str = scanner.next();
StringBuilder sb=new StringBuilder(str);
for(int i=sb.length()-3;i>=0;i-=3){
sb.insert(i,",");
}
System.out.print(sb.toString());
}
}
💯思路解析
本题目思路是:"输入字符串后转为 StringBuilder, StringBuilder有一个insert方法,可以按照索引插入指定的内容"
⭕题目六: 统计字符串中字母出现次数
🌟代码演示
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
String string = "H e l l o ! n o w c o d e r";
Scanner scanner = new Scanner(System.in);
String word = scanner.next();
int count=0;
for (int i = 0; i < string.length(); i++) {
if(string.charAt(i)==word.charAt(0)){
count++;
}
}
System.out.println(count);
}
}
💯思路解析
本题目思路是:"先统计原来字符串的长度,输入字符串后进行循环统计,原来字符串使用charAt进行一个一个比对即可"
⭕题目七: 十进制数转二进制数
🌟代码演示
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int a=scanner.nextInt();
System.out.println(Integer.toString(a,2));
}
}
💯思路解析
本题目思路是:"我们可以使用Integer类的toString()方法将十进制数转换为二进制数。此方法有两个参数,第一个参数是十进制数,第二个参数是我们要转换的基数。对于二进制,基数为2"
⭕题目八: 掷骰子游戏
🌟代码演示
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextInt()) {
int seed = scanner.nextInt();
Random random = new Random(seed);
//write your code here......
int a=random.nextInt(6)+1;
System.out.println(a);
}
}
}
💯思路解析
本题目思路是:
" 产生随机数的公式为":
产生[a,b]范围的随机数
公式:
Random r=new Random();
int r1=r.nextInt(b-a+1)+a;
作者:KJ.JK
文章对你有所帮助的话,欢迎给个赞或者 star,你的支持是对作者最大的鼓励,不足之处可以在评论区多多指正,交流学习