今天在网上看了几到比较适合初学者的JAVA题我把题目与其对应的答案代码规整下来供大家参考:
1.九九乘法表
package b;
public class A1 {
public static void main(String[] args) {
for(int i=1;i<=9;i++) {
for(int k=1;k<=i;k++) {
System.out.print(k+"*"+i+"="+i*k+"\t");
}
System.out.println();
}
}
}
2.交换两个变量的值(不用中间量)
package b;
public class A3 {
public static void main(String[] args) {
int k1=10,k2=200;
System.out.println(k1+" "+k2);
k1=k1+k2;
k2=k1-k2;
k1=k1-k2;
System.out.println(k1+" "+k2);
}
}
3.比较三个数的大小
package b;
public class A4 {
public static void main(String[] args) {
int i = 1, j = 2, z = 3;
int max;
int min = max = i;
if (max < j) {
max = j;
}
if (max < z) {
max = z;
}
if (min > j) {
min = j;
}
if (min > z) {
min = z;
}
System.out.println("max=" + max + ",min=" + min);
}
}
4.输出一百以内的奇数,6个数字一行
方法一:
package b;
public class A5 {
public static void main(String[] args) {
int count=0;
for(int i=1;i<=100;i+=2) {
System.out.print(i+"\t");
if(++count%6==0) {
System.out.println();
}
}
}
}
方法二:
package NEW_YEAR_DAY;
public class A3 {
public static void main(String[] args) {
int i,k=0;
for(i=1;i<=100;i++) {
if(i%2!=0) {
System.out.print(i+"\t");
k++;
}
if(k==6) {
System.out.println();
k=0;
}
}
}
}
5.分别用for,while,do while 三种循环语句来实现从1加到100
package b;
public class A6 {
public static void main(String[] args) {
int for_res = 0, while_res = 0, do_res = 0;
int j = 0, z = 0;
//for语句
for (int i = 1; i <= 100; i++) {
for_res += i;
}
System.out.println("for语句:" + "1+2+3+...+100=" + for_res);
//while语句
while (j <= 100) {
while_res += j;
j++;
}
System.out.println("while语句:" + "1+2+3+...+100=" + while_res);
//do while语句
do {
do_res += z;
} while (z <= 100);
System.out.println("do while语句:" + "1+2+3+...+100=" + do_res);
}
}
6.10以内偶数之和
package b;
public class A10 {
public static void main(String[] args) {
int res=0;
for(int i=1;i<=10;i++) {
if(i%2!=0) {
continue;
}
res +=i;
}
System.out.println(res);
}
}
7.100以内奇数之和
package b;
public class A11 {
public static void main(String[] args) {
int res=0;
for(int k=1;k<=100;k++) {
if(k%2==0) {
continue;
}
res+=k;
}
System.out.println(res);
}
}
8.输出直角三角形的*
package b;
public class A12 {
public static void main(String[] args) {
int k=5;
for(int i=1;i<=5;i++) {
for(int j=0;j<i;j++) {
System.out.print("*");
}
System.out.println();
}
}
}
9.一张纸对折几次能达到珠穆朗玛峰的高度(8848.13m)
package b;
public class A14 {
public static void main(String[] args) {
long res = 8;
int count = 0;
while (true) {
res *= 2;
count++;
if (res > 884813000)
break;
}
System.out.println("共对折了" + count + "次!");
}
}
10.随机一个年龄,输入数字猜,三次机会猜对
package b;
import java.util.Random;
import java.util.Scanner;
public class A16 {
public static void main(String[] args) {
Random r= new Random();
int target = r.nextInt(40)+20;
System.out.println(target);
Scanner input =new Scanner(System.in);
for(int i=0;i<3;i++) {
System.out.println("请输入年龄");
int kk = input.nextInt();
if(target == kk) {
System.out.println("猜对了!");
break;
}
}
}
}
11.100以内能被3和5同时整除的数字及个数
package b;
public class A18 {
public static void main(String[] args) {
int res=0;
for(int i=1;i<=100;i++) {
if(i%3==0&&i%5==0){
System.out.print(i+" ");
res++;
}
}
System.out.println();
System.out.println("能被5和3同时整除的个数为:"+res);
}
}
12.输出水仙花数
package b;
public class A19 {
public static void main(String[] args) {
for (int i = 100; i < 1000; i++) {
int hun = i / 100;
int ten = (i - hun * 100) / 10;
int len = i % 10;
if ((hun * hun * hun + ten * ten * ten + len * len * len) == i) {
System.out.println(i + "是水仙花数");
}
}
}
}
13.判断一个数是否为质数
package b;
import java.util.Scanner;
public class A21 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
boolean b = true;
System.out.println("请输入一个正整数:");
int i = input.nextInt();
if (i > 0) {
for (int k = 2; k <i/2; k++) {
if (i % k == 0) {
b = false;
break;
}
}
if (b) {
System.out.println(i + "是质数");
} else {
System.out.println(i + "不是质数");
}
} else {
System.out.println("輸入錯誤,请输入一个正整数!");
}
}
}
14.鸡兔同笼
package b;
public class A22 {
public static void main(String[] args) {
int sum = 35;
int foot = 94;
for (int ji = 1; ji <= foot / 2; ji++) {
int rabbit = sum - ji;
if (rabbit * 4 + ji * 2 == foot) {
System.out.println("鸡:" + ji);
System.out.println("兔:" + rabbit);
break;
}
}
}
}
15.公鸡一只5块,母鸡一只3块,鸡儿子3只1块,共一百块买一百只鸡,公鸡母鸡小鸡各几只
package b;
public class A23 {
public static void main(String[] args) {
/* 公鸡一只5块,母鸡一只3块,鸡儿子3只1块,共一百块买一百只鸡,公鸡母鸡小鸡各几只 */
int money = 100;
int sum = 100;
for (int gongji = 1; gongji <= money / 5; gongji++) {
for (int xiaoji = 3; xiaoji <= money; xiaoji += 3) {
int muji = sum - gongji - xiaoji;
if (gongji * 5 + xiaoji / 3 + muji * 3 == 100 && gongji + muji + xiaoji == 100) {
if (gongji >= 0 && muji >= 0 && xiaoji >= 0) {
System.out.println("公鸡:" + gongji);
System.out.println("母鸡:" + muji);
System.out.println("小鸡:" + xiaoji);
System.out.println();
break;
}
}
}
}
}
}
16.輸出等腰三角形的*
package d;
public class A14 {
public static void main(String[] args) {
int k = 5;
for (int i = 1; i <= k; i++) {
for (int j = 0; j < k - i; j++)
System.out.print(" ");
for (int j = 0; j < 2 * i - 1; j++)
System.out.print("*");
System.out.println();
}
}
}
17.求s=a+aa+aaa+aaaa+…的值(數字和需要進行計算的數字均由鍵盤輸入
package d;
import java.util.Scanner;
public class A3 {
public static int aaa(Scanner input) {
int a;
while (true) {
try {
String ss = input.nextLine();
a = Integer.parseInt(ss);
if (a > 0) {
break;
} else
System.out.println("请输入正整数");
} catch (Exception e) {
// TODO: handle exception
System.out.println("请输入合法的数字");
}
}
return a;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int number = 0;
int number1 = 0;
String ss;
String qwq = "";
System.out.println("请输入要计算的数字");
int a = aaa(input);
System.out.println("请输入要计算的次数");
int n = aaa(input);
for (int i = 0; i < n; i++) {
ss = String.valueOf(a);
qwq += ss;
number = Integer.parseInt(qwq);
number1 += number;
}
System.out.println(number1);
}
}
18.有1,2,3,4四個數字,能組成多少種互不相同且無重複的三位數,都是多少?
package d;
public class A9 {
public static void main(String[] args) {
StringBuilder sb=new StringBuilder();
int count=0;
for(int i=1;i<=4;i++) {
for(int m=1;m<=4;m++) {
for(int k=1;k<=4;k++) {
if(i!=m&&m!=k&&i!=k) {
String ss=i+""+m+""+k;
if(sb.indexOf(ss)<0) {
count++;
sb.append(ss);
if(count%6==0)
sb.append("\n");
else
sb.append("\t");
}
}
}
}
}
if(sb.length()>0) {
sb.deleteCharAt(sb.length()-1);
System.out.println(sb);
System.out.println(sb.length()/4+1);
}
}
}
19.求1!+2!+3!+…+20!
package d;
public class A11 {
public static void main(String[] args) {
long res1=0;
for(int j=1;j<=20;j++) {
long res =1;
for(int i=j;i>=1;i--) {
res*=i;
}
res1+=res;
}
System.out.println(res1);
}
}
20.一球從100米高度自由落下,每次落地都反彈上次高度的一半再落下,求它在第十次落地的時候,共經過多少米?第十次反彈多高?
package d;
public class A8 {
public static void main(String[] args) {
double h=100;
double pop10=pop(h,10);
System.out.println("第10次弹起的高度为:"+pop10);
double res=100;
for(int i=1;i<10;i++) {
res +=pop(h,i)*2;
}
System.out.println("第10次落地的经过的路程:"+res);
}
public static double pop(double hight,int n) {
// TODO Auto-generated method stub
double res=hight;
for(int i=0;i<n;i++) {
res /=2.;
}
return res;
}
}
21.一個10萬以內的整數,它加上100後是一個完全平方數,再加上168有時一個完全平方數,請問該數是多少?
package d;
public class A17 {
public static void main(String[] args) {
for (int i = 1; i < 100000; i++) {
boolean b1 = wanQuan(i + 100);
if (b1) {
boolean b2 = wanQuan(i + 168);
if (b2) {
System.out.println(i);
}
}
}
}
public static boolean wanQuan(int num) {
// TODO Auto-generated method stub
boolean res = false;
if (num > 0) {
int ss = (int) Math.sqrt(num);
res = ss * ss == num;
}
return res;
}
}