导航:首页 > 编程语言 > java编程作业

java编程作业

发布时间:2022-07-31 21:12:45

A. 简单的java编程作业~~强烈求援助~~

import java.util.Scanner;

public class Mytest {
public static void main (String args[]){
Mytest tMytest=new Mytest();
tMytest.order();
tMytest.avg();
tMytest.min();
}
private void avg(){
float[] tNumbers=new float[10];
float sum=0;
for(int i=0;i<10;i++){
Scanner tScan=new Scanner(System.in);
tNumbers[i]=tScan.nextFloat();
sum+=tNumbers[i];
}
System.out.print("平均值为:"+sum/10+"\n");
int num=0;
String cont="";
for(int i=0;i<10;i++){

if(tNumbers[i]>sum/10){
num+=1;
cont+=tNumbers[i]+", ";
}
}
System.out.println(num+"个数字在平均值以上:"+cont);
}
private void min(){
float[] tNumbers=new float[10];
float tMin=0;
for(int i=0;i<10;i++){
Scanner tScan=new Scanner(System.in);
tNumbers[i]=tScan.nextFloat();
if(i==0){
tMin=tNumbers[i];
}else {
if(tMin>=tNumbers[i]){
tMin=tNumbers[i];
}
}

}
System.out.print("最小值为:"+tMin);
}
private void order(){
float[] tNumbers=new float[10];
for(int i=0;i<10;i++){
Scanner tScan=new Scanner(System.in);
tNumbers[i]=tScan.nextFloat();
}
for(int i=0;i<10;i++){
for(int j=1;j<10-i;j++){
if(tNumbers[j-1]>tNumbers[j]){
tNumbers[j]=tNumbers[j]+tNumbers[j-1];
tNumbers[j-1]=tNumbers[j]-tNumbers[j-1];
tNumbers[j]=tNumbers[j]-tNumbers[j-1];
}
}
System.out.print(tNumbers[10-i-1]+", ");
}

}
}

B. java编程作业

/**
* 实现3的阶乘
* @author Administrator * */
public class JieCheng {
public static void main(String[] args) {
System.out.println(getFactorialSum(3)+"");//通过循环实现阶乘
int sum=1,n=0;
Scanner scan = new Scanner(System.in);

System.out.println("输入一个数字");
int n= in.nextInt();//这里没做校验,可以用try防止错误输入

for(int i=1;i<=n;i++){
sum=sum*i;
}
System.out.println(sum);
}
//通过递归实现阶乘
private static int getFactorialSum(int n){
if(n==1||n==0){
return 1;
}else{
return getFactorialSum(n-1)*n;
}
}
}
需要注意的是n如果太大 int会溢出,所以换成biginteger

C. java编程作业,求答案

import java.awt.*;
import java.awt.event.*;

public class TestFrame {
public static void main(String[] args) {
Frame frame = new Frame("姓名:xxx");
frame.setLayout(new BorderLayout());
Panel panel1 = new Panel();
Panel panel2 = new Panel();
TextField tf = new TextField(20);
panel1.add(tf);
panel2.setLayout(new GridLayout(4,4));

panel2.add(new Button("0"));
panel2.add(new Button("1"));
panel2.add(new Button("2"));
panel2.add(new Button("3"));
panel2.add(new Button("4"));
panel2.add(new Button("5"));
panel2.add(new Button("6"));
panel2.add(new Button("7"));
panel2.add(new Button("8"));
panel2.add(new Button("9"));
panel2.add(new Button("A"));
panel2.add(new Button("B"));
panel2.add(new Button("C"));
panel2.add(new Button("D"));
panel2.add(new Button("E"));
panel2.add(new Button("F"));

frame.add(panel1, BorderLayout.NORTH);
frame.add(panel2, BorderLayout.SOUTH);
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.pack();
frame.setVisible(true);
}
}

D. java编程作业数列求解

代码如下:

importjava.util.Scanner;

publicclassApp{

publicstaticvoidmain(String[]args){

Scannerscanner=newScanner(System.in);

int[]numbers={8,4,2,1,23,344,12};

intval=scanner.nextInt();

booleanfound=false;

for(inti=0;i<numbers.length;i++){
if(val==numbers[i]){
found=true;
}
}

if(found){
System.out.println("数列中包含数"+val);
}else{
System.out.println("数列中不包含数"+val);
}
}
}

E. Java编程作业,急用

以下是源代码和运行结果截图,如果哪里有问题的话可以来找我。

classPet{

intage;

intweight;

publicPet(intage,intweight){

super();

this.age=age;

this.weight=weight;

}

publicvoidshowInfo(){

System.out.println("myweightis"+this.weight+"andmyageis"+this.age);

}

publicintgetWeightInfo(){

returnthis.weight;

}

publicintgetAgeInfo(){

returnthis.age;

}

}

interfaceEatable{

publicabstractvoidbeEatted();//默认为public和abstract

}

classCatextendsPet{

publicCat(intage,intweight){

super(age,weight);

//TODOAuto-generatedconstructorstub

}

privateintcry;

publicvoidshowInfo(){

System.out.println("I'maCat,myweightis"+this.weight+"andmyageis"+this.age);

}

publicintgetWeightInfo(){

returnsuper.getWeightInfo();

}

publicintgetAgeInfo(){

returnsuper.getAgeInfo();

}

}

{

publicDog(intage,intweight){

super(age,weight);

//TODOAuto-generatedconstructorstub

}

privateintcry;

publicvoidshowInfo(){

System.out.println("I'maDog,myweightis"+this.weight+"andmyageis"+this.age);

}

publicintgetWeightInfo(){

returnsuper.getWeightInfo();

}

publicintgetAgeInfo(){

returnsuper.getAgeInfo();

}

publicvoidbeEatted(){

}

}

classFruitimplementsEatable{

publicFruit(Stringcolor,Stringaddress){

super();

this.color=color;

this.address=address;

}

Stringcolor;

Stringaddress;

publicvoidbeEatted(){

System.out.println("I'mbeeneaten!");

}

publicvoidshowInfo(){

System.out.println("mycoloris"+this.color+"andmyaddressis"+this.address);

}

publicStringgetColorInfo(){

returnthis.color;

}

publicStringgetAddressInfo(){

returnthis.address;

}

}

classBananaextendsFruit{

publicBanana(Stringcolor,Stringaddress){

super(color,address);

//TODOAuto-generatedconstructorstub

}

publicvoidbeEatted(){

System.out.println("I'maBanana,I'mbeeneaten!");

}

publicvoidshowInfo(){

System.out.println("I'maBanana,mycoloris"+this.color+"andmyaddressis"+this.address);

}

@Override

publicStringgetColorInfo(){

//TODOAuto-generatedmethodstub

returnthis.getColorInfo();

}

@Override

publicStringgetAddressInfo(){

returnthis.getAddressInfo();

}

}

classAppleextendsFruit{

Stringtype;

publicApple(Stringcolor,Stringaddress,Stringtype){

super(color,address);

this.type=type;

//TODOAuto-generatedconstructorstub

}

publicStringgetType(){

returntype;

}

publicvoidbeEatted(){

System.out.println("I'manapple,I'mbeeneaten!");

}

publicvoidshowInfo(){

System.out.println("I'manApple,mycoloris"+this.color+",myaddressis"+this.address

+"andmytypeis"+type);

}

@Override

publicStringgetColorInfo(){

//TODOAuto-generatedmethodstub

returnthis.getColorInfo();

}

@Override

publicStringgetAddressInfo(){

returnthis.getAddressInfo();

}

}

publicclassPrograms{

publicstaticvoidmain(String[]args){

Catmycat=newCat(10,20);

Dogmydog=newDog(10,24);

Bananamybanana=newBanana("黄色","北京");

Applemyapple=newApple("红色","上海","新品种");

mycat.showInfo();

mydog.showInfo();

mybanana.showInfo();

myapple.showInfo();

mydog.beEatted();

mybanana.beEatted();

myapple.beEatted();

}

}

F. java编程作业,求打救,急.............................

import java.util.Random;

public class StudentsAward {

public static void main(String[] args) {
int[] id = {1,2,3,4,5,6,7,8,9,10};
boolean[] used = new boolean[id.length];
Random r = new Random();
int i ,j=0;
do{
i = r.nextInt(id.length);
if(used[i]){
continue;
}
j++;
System.out.println("三等奖获得者是:"+id[i]+"号");
used[i] =true;
}while(j!=3);
do{
i = r.nextInt(id.length);
if(used[i]){
continue;
}
j++;
System.out.println("二等奖获得者是:"+id[i]+"号");
used[i] =true;
}while(j!=5);
do{
i = r.nextInt(id.length);
if(used[i]){
continue;
}
j++;
System.out.println("一等奖获得者是:"+id[i]+"号");
used[i] =true;
}while(j!=6);
}
}

G. Java程序作业编写

这个题的写法有几种
写法一:
public
class
Circle
{
public
static
void
main(String[]
args)
{
//定义圆的半径
int
r=10;
//定义π的取值范围
double
π=3.14;
System.out.println("当圆的半径为"+r+"时"+"其周长为"+2*r*π);
}
}
写法二:
public
class
Circle
{
public
static
void
main(String[]
args)
{
//定义圆的半径
int
r=10;
//直接调用API中现成的方法math.PI
//Math
类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数
//PI比任何其他值都更接近
pi(即圆的周长与直径之比)的
double
值。
System.out.println("当圆的半径为"+r+"时"+"其周长为"+2*r*Math.PI);
}
}
写法三:
public
class
Circle
{
public
static
void
main(String[]
args)
{
//定义圆的半径
int
r=10;
//定义π的取值范围
double
π=3.14;
//计算圆的周长
double
perimeter=2*r*π
System.out.println("当圆的半径为"+r+"时"+"其周长为"+perimeter);
}
}
写法四
//利用面向对象的编程方法,设计类Circle计算圆的周长。
public
class
TestCircle
{
public
static
void
main(String[]
args)
{
Circle
c1=new
Circle();
c1.setRais(10);//c1.rais
System.out.println(c1.findArea());
}
}
class
Circle
{
//定义半径
int
radius=10;
//提供方法用于获取圆的面积
public
double
findArea(){
System.out.println("当圆的半径为"+radius+"时"+"其面积为");
return
3.14*2*radius;
}
//返回圆的半径
public
double
getRais(){
return
radius;
}
//设置圆的半径
public
void
setRais(int
r){
radius
=
r;
}
}
等等种写法

H. Java四个编程作业: <1>从键盘输入一个双精度数,再向屏幕输出; <2>判断

importjava.util.Scanner;

publicclassexp{
publicstaticvoidmain(String[]args){
Scannerscanner=newScanner(System.in);
System.out.print("输入数:");
intn=scanner.nextInt();
inttemp=1,sum=0;
for(inti=1;i<=n;i++){
//*temp保存每一个数的阶乘
temp=temp*i;
//sum保存所有数的阶乘的和
sum=sum+temp;
}
System.out.println("结果是:"+sum);
}
}

I. JAVA编程作业

importjava.math.BigDecimal;
importjava.util.Random;

publicclassIncrease{

publicstaticbooleanisPrime(inta){

booleanflag=true;

if(a<2){//素数不小于2
returnfalse;
}else{

for(inti=2;i<=Math.sqrt(a);i++){

if(a%i==0){//若能被整除,则说明不是素数,返回false

flag=false;
break;//跳出循环
}
}
}
returnflag;
}

publicstaticvoidtest1(){
inti,n,k=0;
System.out.println("1-1000内素数");
for(n=3;n<=1000;n++){//3~1000的所有数
i=2;
while(i<n){
if(n%i==0)break;//若能整除说明n不是素数,跳出当前循环
i++;
}


if(i==n){//如果i==n则说明n不能被2~n-1整除,是素数
k++;//统计输出数的个数
System.out.print(i+" ");
if(k%6==0)//每输出5个则换行
System.out.println();
}
}
}
publicstaticvoidtest2()
{
Randomr=newRandom();
System.out.println(r.nextInt(301)-100);
}
publicstaticinttest3(intm,intn){
//辗转相除法
intr;
do{
if(m<n)
{
r=m;
m=n;
n=r;
}
r=m%n;
m=n;
n=r;
}while(r!=0);
returnm;
}
publicstaticdoubletest4(intn){
doublee=1f;
doubletotal=1.0;
for(inti=0;i<n;i++)
{
total/=i+1;
e+=total;
}
BigDecimalb=newBigDecimal(e);
e=b.setScale(4,BigDecimal.ROUND_HALF_UP).doubleValue();//表明四舍五入,保留四位小数
returne;
}
publicstaticvoidmain(String[]args){
//第一题测试
System.out.println(isPrime(131));
test1();
//第二题测试
test2();
//第三题测试
System.out.println("最大公约数为:"+test3(1302,19924));
//第四题测试
System.out.println("e="+test4(100));
}
}

运行结果:

true

1-1000内素数

3 5 7 11 13 17

19 23 29 31 37 41

43 47 53 59 61 67

71 73 79 83 89 97

101 103 107 109 113 127

131 137 139 149 151 157

163 167 173 179 181 191

193 197 199 211 223 227

229 233 239 241 251 257

263 269 271 277 281 283

293 307 311 313 317 331

337 347 349 353 359 367

373 379 383 389 397 401

409 419 421 431 433 439

443 449 457 461 463 467

479 487 491 499 503 509

521 523 541 547 557 563

569 571 577 587 593 599

601 607 613 617 619 631

641 643 647 653 659 661

673 677 683 691 701 709

719 727 733 739 743 751

757 761 769 773 787 797

809 811 821 823 827 829

839 853 857 859 863 877

881 883 887 907 911 919

929 937 941 947 953 967

971 977 983 991 997 106

最大公约数为:2

e=2.7183

阅读全文

与java编程作业相关的资料

热点内容
战地联盟3解压密码 浏览:801
s型命令 浏览:23
php年薪5年 浏览:67
如何上网上设个人加密账户 浏览:44
linux打开ssh服务 浏览:78
微信位置可以加密吗 浏览:470
算法蛮力法 浏览:438
随机排练命令 浏览:147
python多进程并发 浏览:41
安卓软件安装如何躲避安全检测 浏览:647
奇幻潮翡翠台源码百度云盘 浏览:187
什么软件可以免费pdf转word 浏览:15
php正则表达式大全 浏览:394
androidntp时间 浏览:299
轮机长命令簿英文 浏览:148
oppo铃声设置被加密怎么处理 浏览:548
粤苗app图形验证码怎么填 浏览:899
管家婆架设云服务器 浏览:254
php的登录界面代码 浏览:997
php开发客户端 浏览:998