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++;
}
}
}
}