简单的java程序题

1. 调用类java.lang.Math的成员方法“public static double random()”运算下面表达式10000次。 (int)(Math.random()*20+0.5)
统计其中生成的整数0,1,2,…,20的个数分别是多少,并输出统计结果。
2. 调用类java.lang.Math的成员方法“public static double random()”,设法生成10个互相不同的从‘a’到‘z’字母,然后对这10个字母按从小到大的方式排序。输出排序的字母序列与排序后的字母序列。

public
class
student{
private
string
stuid;
private
string
stuname;
private
char
stusex;
private
int
stuage;
/**
*以下是各属性的getter/setter方法
*/
public
void
setstuid(string
stuid){
this.stuid=stuid;
}
public
string
getstuid(){
return
stuid;
}
public
void
setstuname(string
stuname){
this.stuname=stuname;
}
public
string
getstuname(){
return
stuname;
}
public
void
setstusex(char
stusex){
this.stusex=stusex;
}
public
char
getstusex(){
return
stusex;
}
public
void
setstuage(int
stuage){
this.stuage=stuage;
}
public
int
getstuage(){
return
stuage;
}
/**
*构造方法,构造学生信息
*/
public
student(string
stuid,string
stuname,char
stusex,int
stuage){
this.stuid=stuid;
this.stuname=stuname;
this.stusex=stusex;
this.stuage=stuage;
}
public
string
tostring(){//覆盖该类的tostring()方法
stringbuffer
buff=new
stringbuffer();
buff.append("学号:"+stuid);
buff.append("\n姓名:"+stuname);
buff.append("\n性别:"+stusex);
buff.append("\n年龄:"+stuage);
return
buff.tostring();
}
public
static
void
main(string[]
args){
student
stu=new
student("1000","zhangsan",'男',18);
system.out.println
(stu);//打印学生信息
system.out.println
("--修改姓名结果--");
stu.setstuname("lisi");
system.out.println
(stu);
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2019-03-20
写了半天··虽然有人块但辛苦了就得传上来!~~~~~~
class
Student{
private
int
sNo;
private
String
sName;
private
int
sAge;
private
double
sScore;
public
Student(int
no,
String
name,
int
age,
double
score)
{
super();
sNo
=
no;
sName
=
name;
sAge
=
age;
sScore
=
score;
}
public
int
getSNo()
{
return
sNo;
}
public
void
setSNo(int
no)
{
sNo
=
no;
}
public
String
getSName()
{
return
sName;
}
public
void
setSName(String
name)
{
sName
=
name;
}
public
int
getSAge()
{
return
sAge;
}
public
void
setSAge(int
age)
{
sAge
=
age;
}
public
double
getSScore()
{
return
sScore;
}
public
void
setSScore(double
score)
{
sScore
=
score;
}
public
static
void
main(String[]args){
Student
s
=
new
Student(1,"zhangsan",15,39);
Student
s1
=new
Student(2,"lisi",18,69);
Student
s2
=
new
Student(3,"wangwu",19,100);
Student
s3
=new
Student(4,"zhaoliu",20,10);
double
num1
=
s.getSScore();
double
num2
=
s1.getSScore();
double
num3
=
s2.getSScore();
double
num4
=s3.getSScore();
double
result
=(num1+num2)/2;
System.out.println(“2人平均成绩"+result);
//
double[]
list
=
{num1,num2,num3};
//
System.out.println(list.length);
double
re
=0
;
if(num1<num2)
{
re
=
num2;
if(num2
<
num3)
{
re
=num3;
}else{re
=num2;}
}else
{
re=num1;
}
System.out.println("最大成绩"+re);
if(num1<num2)
{
re
=num1;
if(num2
<
num3)
{
re
=num2;
if(num3<num4)
{
re=num3;
}else
{
re
=num4;
}
}else
{
re
=num3;
}
}else{re
=num2;}
System.out.println("最小成绩是"+re);
}
}
第2个回答  2009-06-17
1.
public class RandomTest
{
public static void main(String[] args)
{
int a = 0;
int[] count = new int[20];

for(int i=0;i<10000;i++)
{
a = (int)(Math.random()*20+0.5);
for(int j=1;j<=20;j++)
{
if(a==j)
count[j-1]+=1;
}
}

for(int i=0;i<count.length;i++)
{
System.out.print(count[i]+"\t");
if(i==9)
System.out.println();
}
}

}
2.
public class TestRandomChar
{
public static void main(String[] args)
{
char[] RandomArray = new char[10];//10个元素的字符型数组
char temp = ' ';//设置一个 临时替换字符temp 排序的时候用

for (int i = 0; i < 10; i++)//字符数组开始赋值
{

RandomArray[i] = (char) (Math.random() * 200 + 1);//生成字符数组下标为i的元素的值
if(RandomArray[i]>97&&RandomArray[i]<122)//如果生成的值大于97并且小于122 就将其赋值给第i个元素
{
for (int j = 0; j < i; j++)
{
if (RandomArray[j] == RandomArray[i]) //里层的循环是判断 下面生成字符是不是与前面的字符重复 如果重复 重新生成
{
i--;
break;
}

}
}
else
{
i--;
}
}
for (int i = 0; i < RandomArray.length; i++) //输出生成的数组 此时是无序的
{
System.out.print(RandomArray[i]+",");
}
System.out.println();

for(int m=0;m<RandomArray.length;m++)//排序方法
{
for(int n=0;n<=m;n++)
{
if(RandomArray[m]<RandomArray[n])
{
temp = RandomArray[m];
RandomArray[m] = RandomArray[n];
RandomArray[n] = temp;
}
}
}
for (int i = 0; i < RandomArray.length; i++) //输出排序后的数组
{
System.out.print(RandomArray[i]+",");
}
}
}本回答被网友采纳
第3个回答  2009-06-17
1.
public static void main(String[] args) {
int arr[] = new int[21];

for (int i = 0; i < 10000; i++) {
int a=(int)(Math.random()*20+0.5);
if(a<=20){
arr[a]+=1;
}
}

for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}

2.
public static void main(String[] args) {
char arrStr[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z'};

char arrChar[] = new char[10];

int count = 0;
int size=26;
while (count < 10) {
int a = (int) (Math.random() * (size));
System.out.print(arrStr[a]+" ");

arrChar[count++]=arrStr[a];
char temp=arrStr[a];
arrStr[a]=arrStr[size-1];
arrStr[size-1]=temp;
size--;
}
System.out.println();

for (int i = 0; i < arrChar.length; i++) {
for (int j = 0; j < arrChar.length; j++) {
if(arrChar[i]<arrChar[j]){
char temp = arrChar[i];
arrChar[i] = arrChar[j];
arrChar[j] = temp;
}
}
}

for (int i = 0; i < arrChar.length; i++) {
System.out.print(arrChar[i]+" ");
}
}
相似回答