public class Main {
public static int reverseInt(int input) {
int reversedNum = 0;
while (input != 0) {
reversedNum = reversedNum * 10 + input % 10;
input = input / 10;
}
return reversedNum;
}
public static void main(String[] args) {
int input = 352;
int reversed = reverseInt(input);
System.out.println("Reversed=" + String.valueOf(reversed));
}
}
B. 3道java编程题,求解
packageTestPerson;
/**
*(1)编写程序实现如下功能:已知Person类包含三个公共成员变量(姓名、性别、年龄)和一个构造方法,
*Student类是Person类的派生类,包含两个新的公共成员变量(学号、班号)、两个公共方法(修改年龄、显示基本信息)及一个构造方法。
*在测试类Test1中,定义一组学生对象,并初始化他们的基本信息,然后依次输出。
*/
publicclassTest1{
publicstaticvoidmain(String[]args){
Student[]student=newStudent[3];
student[0]=newStudent("小李","男",12,20181101,01);
student[1]=newStudent("小南","女",13,20001102,01);
student[2]=newStudent("小李","男",12,20181103,01);
for(Studentstu:student){
stu.showInformation();
}
}
}
classPerson{
publicStringname;
publicStringsex;
publicintage;
publicPerson(Stringname,Stringsex,intage){
super();
this.name=name;
this.sex=sex;
this.age=age;
}
}
classStudentextendsPerson{
publiclongstudentId;
publiclongclassId;
publicvoidsetAge(intage){
age=this.age;
}
publicvoidshowInformation(){
System.out.println("我的姓名是"+name+","+"我的性别是"+sex+","+"我的年龄是"+age
+"岁,"+"我的学号是"+studentId+","+"我的班号是"+classId+"班");
}
publicStudent(Stringname,Stringsex,intage,longstudentId,
longclassId){
super(name,sex,age);
this.studentId=studentId;
this.classId=classId;
}
}
不可否认,我现在是有点闲,所以我就帮你写第一个吧,至于后面两个,我就不写了,看看还有没有其他人有点闲时间,看缘分吧
运行结果:
我的姓名是小李,我的性别是男,我的年龄是12岁,我的学号是20181101,我的班号是1班
我的姓名是小南,我的性别是女,我的年龄是13岁,我的学号是20001102,我的班号是1班
我的姓名是小李,我的性别是男,我的年龄是12岁,我的学号是20181103,我的班号是1班
C. 8道简单的Java编程题
第一题
private static void tuzinum(){
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(1);
//总数
int allnum = 1;
//成年数
int chengniannum = 0;
//出生数
int chushengnum = 0;
for(int i=0;i<12;i++){
for(int j=0;j<list.size();j++){
Integer age = list.get(j);
age++;
list.set(j, age);
if(age>3){
chengniannum++;
chushengnum++;
allnum++;
list.add(1);
}
}
System.out.println("第"+i+"个月兔子对数为:"+list.size());
}
}
第二题
private static void fenjie(){
int num = 90;
boolean canchu = true;
while(canchu){
int time = 0;
for(int i=2;i<num/2;i++){
if(num%i==0){
num = num/i;
System.out.println(i);
time++;
}
}
if(time == 0 ){
canchu = false;
System.out.println(num);
}
}
}
D. 五道java语言描述的数据结构编程题,请求给予详细解答
第一题:
//使用集合提供的工具方法
public static List<Integer> merge(List<Integer> a, List<Integer> b) {
//a,b not null
//全部放到一个set里面,使得元素合并
Set<Integer> set = new HashSet<Integer>(a);
set.addAll(b);
//将set里面的元素放到列表再转为数组
Integer[] array = new ArrayList<Integer>(set).toArray(new Integer[1]);
//升序排序
Arrays.sort(array);
//将排序后的数组转为list
return Arrays.asList(array);
}
//自己写的算法, a为升序列表,b为降序列表
public static List<Integer> merge2(List<Integer> a, List<Integer> b) {
//a,b not null
int aSize = a.size();
int bSize = b.size();
List<Integer> result = new ArrayList<Integer>();
int aIndex = 0;// 升序列表从首位开始
int bIndex = bSize - 1;// 降序列表从末尾开始
int aEl;
int bEl;
// 循环终止条件为: a 或者 b 列表遍历完
while (aIndex < aSize && bIndex >= 0) {
aEl = a.get(aIndex);
bEl = b.get(bIndex);
if (aEl < bEl) {
result.add(aEl);
aIndex++;
} else {
result.add(bEl);
bIndex--;
}
}
// 将某个未遍历完的列表中的元素添加到结果(包括了任意一个列表为空列表的情况)
if (aIndex < aSize) {
for (int i = aIndex; i < aSize; i++) {
result.add(a.get(i));
}
}
else if (bIndex > 0) {
for (int i = bIndex; i >= 0; i--) {
result.add(b.get(i));
}
}
return result;
}
E. java编程题
1.package test;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
public class Test {
public static void main(String[] args) {
String str = "afdjasdg&&&&&$$jfsjdfjdjjdjdjdjdjdj";
int max=0;
Object chars=null;
Map tree = new TreeMap();
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if ((ch >= 1 && ch <= 255) ) {
if (!tree.containsKey(ch)) {
tree.put(ch, new Integer(1));
} else {
Integer in = (Integer) tree.get(ch) + 1;
tree.put(ch, in);
}
}
}
Iterator tit = tree.keySet().iterator();
while (tit.hasNext()) {
Object temp = tit.next();
if(max<=Integer.parseInt(tree.get(temp)+""))
{
max=Integer.parseInt(tree.get(temp)+"");
chars=temp;
}
}
System.out.print(chars.toString() + "出现" + max + "次");
}
}
只要用assic码做范围就可以了.任何字符都可以过滤.
2.方法很多,hashmap或是arraylist,数组都可以的.就是对应关系而已.
package test;
public class ListTest {
static String[] to_19 = { "zero", "one", "two", "three", "four", "five",
"six", "seven", "eight", "nine", "ten", "eleven", "twelve",
"thirteen", "fourteen", "fifteen", "sixteen", "seventeen",
"eighteen", "nineteen" };
static String[] tens = { "twenty", "thirty", "forty", "fifty", "sixty",
"seventy", "eighty", "ninety" };
static String[] denom = { "", "thousand ", "million", "billion",
"trillion", "quadrillion", "quintillion", "sextillion",
"septillion", "octillion", "nonillion", "decillion", "undecillion",
"odecillion", "tredecillion", "quattuordecillion",
"sexdecillion", "septendecillion", "octodecillion",
"novemdecillion", "vigintillion" };
public static void main(String[] argv) throws Exception {
long tstValue = 12345;
ListTest itoe = new ListTest();
System.out.println(itoe.english_number(tstValue));
}
private String convert_nn(int val) {
if (val < 20) return to_19[val];
int flag = val / 10 - 2; if (val % 10 != 0)
return tens[flag] + "-" + to_19[val % 10];
else return tens[flag];
}
private String convert_nnn(int val) {
String word = "";
int rem = val / 100;
int mod = val % 100;
if (rem > 0) {word = to_19[rem] + " hundred ";}
if (mod > 0) {word = word + convert_nn(mod);}
return word;
}
public String english_number(long val) {
if (val < 100) {System.out.println((int) val);return convert_nn((int) val);}
if (val < 1000) {return convert_nnn((int) val); }
for (int v = 0; v < denom.length; v++) {
int didx = v - 1;
long dval = new Double(Math.pow(1000, v)).longValue();
if (dval > val) {
long mod = new Double(Math.pow(1000, didx)).longValue();
int l = (int) (val / mod);
long r = (long) (val - (l * mod));
String ret = convert_nnn(l) + " " + denom[didx];
if (r > 0) {ret = ret + ", " + english_number(r);}
return ret;
}
}
return null;
}
}
F. 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();
}
}
}
G. 几道JAVA题目,求好心人给下答案,感激不尽
1、public static void main(String[] args)
2、public finall static
3、3
4、抽象类
5、implements
6、类:一个包含属性、方法,使用class关键字定义
对象:创建对象必须使用new关键字,对象其实在内存中存储的是类的引用地址。
7、try是可能发生异常的内容;
catch是发生异常后要处理的代码;
finally是始终执行的,比如关闭资源等。
8、
publicclassDemo{
//小明今年12岁,他母亲比他大20岁。编程计算多少年后他母亲的年龄是小明的2倍。
publicstaticvoidmain(String[]args){
System.out.println(getYeah());
}
publicstaticintgetYeah(){
intxmAge=12,mqAge=20,yeah=0;
while(true){
if(mqAge/2==xmAge){
returnyeah;
}
mqAge++;
yeah++;
}
}
}
9、
publicclassDemo{
publicstaticvoidmain(String[]args){
newThread(newPiao()).start();
newThread(newPiao()).start();
newThread(newPiao()).start();
newThread(newPiao()).start();
}
}
classPiaoimplementsRunnable{
privatestaticintpiao=1;
publicvoidrun(){
while(true){
synchronized(Piao.class){
if(piao>100){
System.exit(0);
}
System.out.println(Thread.currentThread().getName()+"出票:"+piao+"号");
piao++;
}
}
}
}