帮忙解决Java的几道例题
发布网友
发布时间:2023-08-21 08:38
我来回答
共2个回答
热心网友
时间:2024-12-13 06:54
1.
public class Test1 {
public static void main(String[] args) {
int a,b,c;
for (int i=100;i<=999;i++){
a = i/100;
b = i/10 - 10*a;
c = i - 100*a - 10*b;
if (i==(a*a*a + b*b*b + c*c*c)){
System.out.println(i+"\t");
}
}
}
}
—————————————————————————————————————————————————————————————
2.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Test2 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("输入学生成绩:");
int score = Integer.parseInt(br.readLine());
System.out.println(score>=90?'A':score>=60?'B':'C');
}
}
—————————————————————————————————————————————————————————————
3.
public class Test3 {
public static void main(String[] args) {
for (int i=0;i<1000;i++){
int tempSum = 0;
for (int j=1;j<i;j++){
if (i%j==0){
tempSum += j;
}
}
if (i==tempSum){
System.out.println(i+"\t");
}
}
}
}
—————————————————————————————————————————————————————————————
4.
条件不明
—————————————————————————————————————————————————————————————
5.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Test5 {
public static void main(String[] args) throws NumberFormatException, IOException {
while (true){
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int x = Integer.parseInt(br.readLine());
int y = Integer.parseInt(br.readLine());
int z = Integer.parseInt(br.readLine());
int _x = x<y?x<z?x:z:y<z?y:z;
int _z = x>y?x>z?x:z:y>z?y:z;
int _y = x + y + z -_x -_z;
System.out.println(_x+" < "+_y+" < "+_z);
}
}
}
—————————————————————————————————————————————————————————————
6.
public class Test6 {
public static void main(String[] args) {
float fm = 1f;
float fz = 1f;
float temp;
float sum = 0f;
for (int i=0;i<20;i++){
temp = fm;
fm = fz;
fz = fz + temp;
sum += fz/fm;
//System.out.println(sum);
}
System.out.println(sum);
}
}
—————————————————————————————————————————————————————————————
7.
public class Test7 {
static int getAge(int n){
if (n==1){
return 10;
}
return 2 + getAge(n-1);
}
public static void main(String[] args) {
System.out.println("第五个的年龄为:"+getAge(5));
}
}
—————————————————————————————————————————————————————————————
8.
public class Test8 {
public static void main(String[] args) {
final float fz = 1f;
float fm = -1f;
float a = -1f,b = -1f;
for (int i = 0; i < 10000; i++) {
a *= b;
fm = fm + 2;
float each = fz / fm * a;
System.out.println(each);
}
}
}
—————————————————————————————————————————————————————————————
热心网友
时间:2024-12-13 06:55
1.
public class Test_1 {
public static void main(String[] args) {
for(int i=100; i<=999; i++) {
if((Math.pow(i/100, 3) + Math.pow(i/10%10, 3) + Math.pow(i%10, 3)) == i) {
System.out.print(i+" ");
}
}
}
}
结果:153 370 371 407
2.
public class Test_2 {
public static void main(String[] args) {
System.out.println(test(95));
System.out.println(test(85));
System.out.println(test(45));
}
public static String test(float record) {
String flag = record >= 90 ? "A" : record >=60 ? "B" : "C";
return flag;
}
}
输出结果:
A
B
C
3.
public class Test_3 {
public static void main(String[] args) {
for(int i=1; i<=1000; i++) {
if(isPerfectNumber(i)) {
System.out.print(i + " ");
}
}
}
public static boolean isPerfectNumber(int n) {
boolean b = false;
int sum = 0;
for(int i=1; i<=n/2; i++) {
if(n%i == 0) {
sum += i;
}
}
if(sum == n) {
b = true;
}
return b;
}
}
输出结果: 6 28 496
no time left.