提示:程序中的S31和S32是java的文件名,你创建时候的命名文件是什么名字就把它改成什么名字。
提示:程序中的S31和S32是java的文件名,你创建时候的命名文件是什么名字就把它改成什么名字。
提示:程序中的S31和S32是java的文件名,你创建时候的命名文件是什么名字就把它改成什么名字。
重要的事情说三遍
1.打印出所有的“水仙花数”,所谓“水仙花数”是指一个三位数,其各位数字的立方和等于该数本身。
class S31 {
public static void main(String[] args){
int a = 0;
int b = 0;
int c = 0;
for(int i =100;i<999;i++){
a = i%10;
b = i/10%10;
c = i/10/10%10;
if(a*a*a+b*b*b+c*c*c==i){
System.out.println(i);
}
}
}
}
2.打印出一下图案。
public class S32 {
public static void main(String[] args) {
int c = 4;
for (int i = 1; i <= c; i++) {
for (int x = 1; x <= c - i; x++) {
System.out.print(" ");
}
for(int j = 1; j <= 2 * i - 1 ; j++){
System.out.print("*");
}
System.out.println( );//println在结尾自动换行而print在结尾需要在加上\n才能换行。。。。
}
}
}
ps:注意程序中的注释。
3.编写一个Java语言程序,计算8+88+888+8888+88888+…的前10项之和。
public class S33 {
public static void main(String[] args){
int sum=0,temp=0;
for(int i=0;i<10;i++) {
temp+=8*Math.pow(10,i);
sum+=temp;
}
System.out.println(sum);
}
/*
Math.pow(2,5)==2**5
*/
}
ps:注意Math方法调用的pow里面的(2,5)是2的五次方。
4.用冒泡法实现下列数从大到小的排序
12,25,5,6,45,4,23
import java.util.Arrays;
public class S34 {
public static void main(String[] args) {
int[] ints = {12, 25, 5, 6, 7, 45, 4, 23};
for (int i = 0; i < ints.length - 1; i++) {
for (int j = 0; j < ints.length - i - 1; j++) {
//如果当前元素比后一位元素大 交换位置
if (ints[j] > ints[j + 1]) {
int temp = ints[j];
ints[j] = ints[j + 1];
ints[j + 1] = temp;
}
}
}
System.out.println(Arrays.toString(ints));
}
}
ps:程序引用了Arrays包。
5.编写程序,利用选择分支语句实现下列式子的计算,输出t=1.5时,y 的结果。
import java.util.Scanner;
public class S35 {
public static void main(String [] args){
Scanner reader = new Scanner(System.in);
System.out.print("请输入整数t: ");
double t = reader.nextDouble();//因为要输入1.5所以为了方便我直接使用double双精度的变量类型。
double y = 0;
if(t>=0&&t<1){
y = Math.pow(t,2)-1;
}
else if(t>=1&&t<3){
y = Math.pow(t,2)-2*t-2;
}
else if(t>=3&&t<5){
y = Math.pow(t,2)-t*Math.sin(t);
}
else if(t>=5&&t<7){
y = t+1;
}
else{
y = t-1;
}
System.out.println(y);
}
}
6.编写程程序计算10~20的整数的平方值,将结果保存在一个数组中,并输出该数组各元素的值,分两行输出,每行5个值。
public class S36 {
public static void main(String[] args){
int [] a = new int[11];
for(int i = 10 ; i <= 20; i++){
a[i-10] = i * i;
}
for(int j = 0 ; j <= 4; j++){
System.out.print(a[j]);
System.out.print(" ");
}
System.out.print("\n");
for(int k = 5 ; k <=9; k++){
System.out.print(a[k]);
System.out.print(" ");
}
}
}