首页编程java编程java编程题及答案 java经典基础编程题及其答案

java编程题及答案 java经典基础编程题及其答案

编程之家2026-05-291107次浏览

大家好,今天给各位分享java编程题及答案的一些知识,其中也会对java经典基础编程题及其答案进行解释,文章篇幅可能偏长,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在就马上开始吧!

java编程题及答案 java经典基础编程题及其答案

java新手编程题目

思路如下:

随即4个数字(1~6)用来模拟4个色子

数字全部存入数组ary,.然后升序排列.

如果满足两两相等,那么已经排序好的数字,就是ary[0]=ary[1]; ary[2]=ary[3];

然后判断ary[0]+ary[2]==6.如果等于6那么满足要求,不等于6,那么继续下次循环

参考代码

java编程题及答案 java经典基础编程题及其答案

importjava.util.Arrays;

publicclassRandomDemo{

publicstaticvoidmain(String[]args){

intloop=5;//重复5次试验

for(intk=0;k<loop;k++){

inttimes=0;//循环的次数

int[]ary;//数组,存储4个随机数

while(true){

times++;//次数+1

ary=newint[4];

for(inti=0;i<ary.length;i++){

ary[i]=getNum();//添加随机数

}

Arrays.sort(ary);//用数组工具类进行排序

//因为有两两相等的情况,那么就是ary[0]=ary[1]ary[2]=ary[3]能减少很多的ifelse判断

//如果两两相等.且两值和等于6,那么跳出循环

if(ary[0]==ary[1]&&ary[2]==ary[3]&&ary[0]+ary[2]==6){

break;//跳出

}

}

System.out.println("两个数字分别是"+ary[0]+"和"+ary[2]+"\t"+"循环了"+times+"次");

}

}

//该方法用于返回一个[1,6]之间的数字

privatestaticintgetNum(){

return(int)(Math.random()*6)+1;//1~6之间的随即数

}

}测试结果

两个数字分别是1和5循环了22次

两个数字分别是1和5循环了12次

两个数字分别是3和3循环了105次

两个数字分别是1和5循环了128次

两个数字分别是2和4循环了96次

求一个java编程题的答案

1)定义一个接口People,里面包含一个方法voidcomplexion();

package cn;

public interface People{

void complexion();

}

2)定义一个抽象类Asian,里面包含一个抽象方法voidnation();

package cn;

public abstract class Asian{

public abstract void nation();

}

3)定义一个类Chinese,继承自Asian,并且实现父类的抽象方法;要求在nation方法中输出

"China";

package cn;

public class Chinese extends Asian{

@Override

public void nation(){

System.out.print("china");

}

}

4)定一个学生类Student,继承自Chinese,并实现接口People,在方法complexion中,输出:

“complexionisyellow”

5)学生类中包含两个私有成员变量name和age,一个构造方法Student(Stringname,intage),用来实现对两私有成员的初始化,两个方法getName()和getAge(),用来返回name和age的

值.

6)在学生类中定义main方法,在其中生成一个Student的对象S,name为“张三”

,age为

20;并调用方法getName(),getAge,nation()和complexion(),输出信息:"张三,20,

China,complexionisyellow"。

package cn;

public class Student extends Chinese implements People{

private String name;

private int age;

@Override

public void complexion(){

System.out.print("complexionisyellow");

}

public static void main(String[] args){

Student student= new Student();

student.setName("张三");

student.setAge(20);

String name= student.getName();

int age= student.getAge();

System.out.print(name+","+ age+",");

student.nation();

System.out.print(",");

student.complexion();

}

public String getName(){

return name;

}

public void setName(String name){

this.name= name;

}

public int getAge(){

return age;

}

public void setAge(int age){

this.age= age;

}

}

java编程题

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.OutputStreamWriter;

public class Demo{

public static void main(String[] args){

System.out.println(求和(1000));

保存姓名和住址();

}

/**

*求从0开始连续整数之和

*@param max从0开始,一直加到max为止

*@return和

*/

private static int求和(int max){

int result= 0;

for(int i=0;i<=max;i++)

result+= i;

return result;

}

private static void保存姓名和住址(){

System.out.println("请输入姓名和住址,用逗号隔开。输入‘quit’退出。");

String strInput= null;

File file= new File("姓名住址.txt");//建立文件

BufferedReader br= new BufferedReader(new InputStreamReader(System.in));//接收控制台输入

BufferedWriter bw= null;

try{

bw= new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));//输出到文件

bw.append("姓名\t住址");

bw.newLine();

} catch(FileNotFoundException e1){

e1.printStackTrace();

} catch(IOException e){

e.printStackTrace();

}

while(true){

System.out.print("_>");

try{

strInput= br.readLine();

if(strInput.equalsIgnoreCase("quit")){//不论quit大小写,退出程序

bw.flush();

break;

}else{

if(bw!= null){

//不论姓名和住址的分隔符是英文逗号还是中文逗号,均替换为制表符,主要是为了输出的美观

bw.append(strInput.replaceFirst("[,,]","\t"));

bw.newLine();

}

}

} catch(IOException e){

e.printStackTrace();

}

}

try{

//关闭输入和输出

if(br!= null)

br.close();

if(bw!= null)

bw.close();

}catch(IOException e){

e.printStackTrace();

}

}

}

文章分享结束,java编程题及答案和java经典基础编程题及其答案的答案你都知道了吗?欢迎再次光临本站哦!

和平精英小黄鸭(xl6666·ch和平精英)html定义下拉列表?html编辑器