一道Java编程题,通过继承java.util.Random类设计一个类RandomEx,并实现以下功能
发布网友
发布时间:2023-05-05 02:02
我来回答
共1个回答
热心网友
时间:2023-11-06 18:11
import java.util.Random;
public class RandomEx extends Random{
public char nextChar(char x){
Random r = new Random();
if((int)x >= 48&&(int)x<=57)
return (char) (r.nextInt(9)+48);
else if(x>=65&&x<=90)
return (char)(r.nextInt(25)+65);
else if(x>=97&&x<=122)
return (char)(r.nextInt(25)+97);
else
return '?';
}
public String toString(){
return "RandomEx";
}
}
import java.util.Random;
public class TestR {
public static void main(String[] args) {
Random r = new Random();
RandomEx ra = new RandomEx();
for(int j = 0;j < 2;++j)
for(int i = 48;i <= 57;++i)
System.out.print(ra.nextChar((char)i)+" ");
System.out.println();
int i = 0;
char x = 'a';
while(i != 20){
System.out.print(ra.nextChar(x++) + " ");
i++;
}
System.out.println();
int j = 0;
char y = 'A';
while(j != 20){
System.out.print(ra.nextChar(y++) + " ");
j++;
}
System.out.println();
System.out.println(ra.toString());
}
}
热心网友
时间:2023-11-06 18:11
import java.util.Random;
public class RandomEx extends Random{
public char nextChar(char x){
Random r = new Random();
if((int)x >= 48&&(int)x<=57)
return (char) (r.nextInt(9)+48);
else if(x>=65&&x<=90)
return (char)(r.nextInt(25)+65);
else if(x>=97&&x<=122)
return (char)(r.nextInt(25)+97);
else
return '?';
}
public String toString(){
return "RandomEx";
}
}
import java.util.Random;
public class TestR {
public static void main(String[] args) {
Random r = new Random();
RandomEx ra = new RandomEx();
for(int j = 0;j < 2;++j)
for(int i = 48;i <= 57;++i)
System.out.print(ra.nextChar((char)i)+" ");
System.out.println();
int i = 0;
char x = 'a';
while(i != 20){
System.out.print(ra.nextChar(x++) + " ");
i++;
}
System.out.println();
int j = 0;
char y = 'A';
while(j != 20){
System.out.print(ra.nextChar(y++) + " ");
j++;
}
System.out.println();
System.out.println(ra.toString());
}
}
热心网友
时间:2023-11-06 18:11
import java.util.Random;
public class RandomEx extends Random{
public char nextChar(char x){
Random r = new Random();
if((int)x >= 48&&(int)x<=57)
return (char) (r.nextInt(9)+48);
else if(x>=65&&x<=90)
return (char)(r.nextInt(25)+65);
else if(x>=97&&x<=122)
return (char)(r.nextInt(25)+97);
else
return '?';
}
public String toString(){
return "RandomEx";
}
}
import java.util.Random;
public class TestR {
public static void main(String[] args) {
Random r = new Random();
RandomEx ra = new RandomEx();
for(int j = 0;j < 2;++j)
for(int i = 48;i <= 57;++i)
System.out.print(ra.nextChar((char)i)+" ");
System.out.println();
int i = 0;
char x = 'a';
while(i != 20){
System.out.print(ra.nextChar(x++) + " ");
i++;
}
System.out.println();
int j = 0;
char y = 'A';
while(j != 20){
System.out.print(ra.nextChar(y++) + " ");
j++;
}
System.out.println();
System.out.println(ra.toString());
}
}