Java编程题目求大佬帮忙
答案:2 悬赏:70 手机版
解决时间 2021-03-13 01:58
- 提问者网友:浪荡绅士
- 2021-03-12 08:29
Java编程题目求大佬帮忙
最佳答案
- 五星知识达人网友:佘樂
- 2021-03-12 09:58
代码如下:
// App1.java
public class App1 {
public static void main(String[] args) {
String str = "kjkjdJKSDJFK932849384KSDJFK&&*&2334989~~2343L4ks";
int upperCnt = 0, lowerCnt = 0, otherCnt = 0;
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (ch >= 'A' && ch <= 'Z') {
upperCnt++;
} else if (ch >= 'a' && ch <= 'z') {
lowerCnt++;
} else {
otherCnt++;
}
}
System.out.println("大写字母:" + upperCnt);
System.out.println("小写字母:" + lowerCnt);
System.out.println("非字母字符:" + otherCnt);
}
}// App2.java
import java.util.Random;
public class App2 {
public static void main(String[] args) {
int[] numbers = new int[10];
Random random = new Random(System.nanoTime());
for (int i = 0; i < numbers.length; i++) {
numbers[i] = random.nextInt(100) + 1;
}
int max, min;
max = numbers[0];
min = numbers[0];
for (int i = 1; i < numbers.length; i++) {
if (numbers[i] > max)
max = numbers[i];
if (numbers[i] < min)
min = numbers[i];
}
System.out.println("最大值:" + max);
System.out.println("最小值:" + min);
}
}// App3.java
public class App3 {
public static void main(String[] args) {
int[] data = new int[5];
try {
data[6] = 100;
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("数组索引溢出。");
e.printStackTrace();
}
try {
data[0] = Integer.parseInt("123abc");
} catch (NumberFormatException e) {
System.out.println("转换失败。");
e.printStackTrace();
}
}
}// App4.java
abstract class Animal {
private String name;
private int weight;
public Animal(String name, int weight) {
this.name = name;
this.weight = weight;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public void run() {
System.out.println("Animal run.");
}
public void catchFeed() {
System.out.println("Animal catchFeed.");
}
}
class Cat extends Animal {
public Cat(String name, int weight) {
super(name, weight);
}
@Override
public void run() {
System.out.println(getName() + " run.");
}
@Override
public void catchFeed() {
System.out.println(getName() + " catchFeed.");
}
}
class Dog extends Animal {
public Dog(String name, int weight) {
super(name, weight);
}
@Override
public void run() {
System.out.println(getName() + " run.");
}
@Override
public void catchFeed() {
System.out.println(getName() + " catchFeed.");
}
}
public class App4 {
public static void main(String[] args) {
Cat cat = new Cat("Tom", 10);
cat.run();
cat.catchFeed();
Dog dog = new Dog("Goff", 25);
dog.run();
dog.catchFeed();
}
}
// App1.java
public class App1 {
public static void main(String[] args) {
String str = "kjkjdJKSDJFK932849384KSDJFK&&*&2334989~~2343L4ks";
int upperCnt = 0, lowerCnt = 0, otherCnt = 0;
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (ch >= 'A' && ch <= 'Z') {
upperCnt++;
} else if (ch >= 'a' && ch <= 'z') {
lowerCnt++;
} else {
otherCnt++;
}
}
System.out.println("大写字母:" + upperCnt);
System.out.println("小写字母:" + lowerCnt);
System.out.println("非字母字符:" + otherCnt);
}
}// App2.java
import java.util.Random;
public class App2 {
public static void main(String[] args) {
int[] numbers = new int[10];
Random random = new Random(System.nanoTime());
for (int i = 0; i < numbers.length; i++) {
numbers[i] = random.nextInt(100) + 1;
}
int max, min;
max = numbers[0];
min = numbers[0];
for (int i = 1; i < numbers.length; i++) {
if (numbers[i] > max)
max = numbers[i];
if (numbers[i] < min)
min = numbers[i];
}
System.out.println("最大值:" + max);
System.out.println("最小值:" + min);
}
}// App3.java
public class App3 {
public static void main(String[] args) {
int[] data = new int[5];
try {
data[6] = 100;
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("数组索引溢出。");
e.printStackTrace();
}
try {
data[0] = Integer.parseInt("123abc");
} catch (NumberFormatException e) {
System.out.println("转换失败。");
e.printStackTrace();
}
}
}// App4.java
abstract class Animal {
private String name;
private int weight;
public Animal(String name, int weight) {
this.name = name;
this.weight = weight;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public void run() {
System.out.println("Animal run.");
}
public void catchFeed() {
System.out.println("Animal catchFeed.");
}
}
class Cat extends Animal {
public Cat(String name, int weight) {
super(name, weight);
}
@Override
public void run() {
System.out.println(getName() + " run.");
}
@Override
public void catchFeed() {
System.out.println(getName() + " catchFeed.");
}
}
class Dog extends Animal {
public Dog(String name, int weight) {
super(name, weight);
}
@Override
public void run() {
System.out.println(getName() + " run.");
}
@Override
public void catchFeed() {
System.out.println(getName() + " catchFeed.");
}
}
public class App4 {
public static void main(String[] args) {
Cat cat = new Cat("Tom", 10);
cat.run();
cat.catchFeed();
Dog dog = new Dog("Goff", 25);
dog.run();
dog.catchFeed();
}
}
全部回答
- 1楼网友:山河有幸埋战骨
- 2021-03-12 10:54
兄dei。。。题目都很简单,考试的话就自己想吧,好好学了肯定都能做出来🙄(编程这么好玩,为何不来编程呢,来呀快活呀)
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯