不是我牛叉
以前有过类似的练习
修改了一下就发了
我也同意楼下的观点 自己动手做一下
有什么不明白的可以参考我的这个
也许你有更好的解决方法
1.
import java.util.Scanner;
public class Test_If_Switch
{
public static void main(String[] args) {
System.out.println("输入一个数字:");
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
System.out.println("方法1:"+methodl(x));
System.out.println("方法2:"+method2(x));
}
static int methodl(int x)
{
int result = 0;
if(x<0)
result = 2*x-1;
else if(x>=0&&x<3)
result = 3*x+5;
else if(x>=3&&x<5)
result = x+1;
else if(x>=5&&x<10)
result=5*x-3;
else if(x>=10)
result = 7*x+2;
return result;
}
static int method2(int x)
{
int result = 0;
switch(x)
{
case 0:
case 1:
case 2:result = 3*x+5;break;
case 3:
case 4:result = x+1;break;
case 5:
case 6:
case 7:
case 8:
case 9:result=5*x-3;break;
case 10:result = 7*x+2;break;
default:result = 2*x-1;break;
}
return result;
}
}
2.
import java.util.Scanner;
public class Test_Tn {
public static void main(String[] args) {
System.out.println("输入一个数字:");
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
System.out.println("第"+x+"项的值为:"+method(x));
}
static long static method(int n) {
long Tn = 0;
if (x == 1)
Tn = 1;
else
Tn = method(x - 1) + x;
return Tn;
}
}
3.
public class test
{
public static void main (String args[])
{
int n=100;
while(n<=1000)
{
int i,j,k;
i=n/100;
j=(n-i*100)/10;
k=n%10;
if((Math.pow(i, 3)+Math.pow(j, 3)+Math.pow(k, 3))==n)
System.out.print(n+String.valueOf('\t'));
n++;
}
}
}
4.
public class Money {
public static void main(String[] args)
{
int a = 100,b = 50, c =10;
for(int i =5;i<=8;i++)
for(int j = 0;j<=6;j++)
for(int k = 0;k<30;k++)
{
if(a*i+b*j+c*k==800)
System.out.println("100元"+i+"张"+"50元"+j+"张"+"10元"+k+"张");
}
}
}
温馨提示:答案为网友推荐,仅供参考