public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("请输入您的消费金额:");
double sMoney = sc.nextDouble();
if (sMoney <= 0) {
System.out.println("您的输入有误!!!");
} else if (sMoney < 50) {
System.out.println("换购活动需要消费满50元才能参加!谢谢您的光临!");
} else if (sMoney >= 50) {
System.out.println("您的消费金额已到达要求,是否参见换购活动");
System.out.println("1.满50元,加2元换购百事可乐饮料1瓶");
System.out.println("2.满100元,加3元换购500ml可乐饮料一瓶");
System.out.println("3.满100元,加10元换购5公斤面粉");
System.out.println("4.满200元,加10元换购一个苏泊尔炒菜锅");
System.out.println("5.满100元,加20元换购欧莱雅爽肤水一瓶");
System.out.println("0.不换购");
System.out.print("请输入您的选择:");
int a = sc.nextInt();
if (a > 5 || a < 0) {
System.err.print("您的输入有误!如有换购需求请重新开始");
} else {
if (a == 5 && sMoney >= 200) {
System.out.println("换够成功了:欧莱雅爽肤水一瓶");
System.out.println("本次消费总金额为:" + (sMoney + 20.0));
} else if (a == 4 && sMoney >= 200) {
System.out.println("换够成功了:1个苏泊尔炒菜锅");
System.out.println("本次消费总金额为:" + (sMoney + 10.0));
} else if (a == 3 && sMoney >= 100) {
System.out.println("换够成功了:5公斤面粉");
System.out.println("本次消费总金额为:" + (sMoney + 10.0));
} else if (a == 2 && sMoney >= 100) {
System.out.println("换够成功了:500ml可乐饮料一瓶");
System.out.println("本次消费总金额为:" + (sMoney + 3.0));
} else if (a == 1 && sMoney >= 50) {
System.out.println("换够成功了:百事可乐饮料1瓶");
System.out.println("本次消费总金额为:" + (sMoney + 2.0));
} else if (a == 0) {
System.out.println("欢迎下次光临!!!!");
} else {
System.out.println("您没有达到相应的消费额度,如有换购需求请重新开始");
}
}
}
}
public static void main(String [] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("请输入一个年份:");
int yearNum = sc.nextInt();
if (yearNum%4==0 && yearNum%100!=0)
{
System.out.println("是普通润年");
}
else
{
if (yearNum%400==0)
{
System.out.println("是世纪润年");
}
else
{
System.out.println("不是润年");
}
}
}
public static void main(String[] args)
{
System.out.println("可输入的运算符为:+ (相加)\t-(相减)\t*(相乘)\t /(相除)\t %(取模)");
Scanner sc = new Scanner(System.in);
System.out.println("请先在输入运算符");
String str = sc.nextLine();
char ch = str.charAt(0);
System.out.println("请输入一个数");
double a = sc.nextDouble();
System.out.println("请在输入一个数");
double b = sc.nextDouble();
switch (ch) {
case '+':System.out.println(a+"+"+b+"="+(a+b));break;
case '-':System.out.println(a+"-"+b+"="+(a-b)+"\n"+b+"-"+a+"="+(b-a));break;
case '*':System.out.println(a+"*"+b+"="+(a*b));break;
case '/':System.out.println(a+"/"+b+"="+(a/b)+"\n"+b+"/"+a+"="+(b/a));break;
case '%':System.out.println(a+"%"+b+"="+(a%b)+"\n"+b+"%"+a+"="+(b%a));break;
default:System.out.println("您的输入有误!请重新运行输入");break;
}
}
每吨货物每公里运费P与运输距离S有关,路途越远,每公里运价越低。公式如下:
当 s<100 时 p=10
当 100<=s<150 时 p=8
当 150<=s<200 时 p=7
当 200<=s<300 时 p=6
当300<=s<500时 p=5.5
当 s>=500 时 p=5
如果所付的总运费超过5000元时,再给予九五折优惠
public static void main(String[] args)
{
double p = 0.0;
Scanner sc = new Scanner(System.in);
System.out.print("请输入货物的重量单位为吨:");
double weight = sc.nextDouble();
System.out.print("请输入运输距离单位为km:");
double distance = sc.nextDouble();
if(distance<100)
{
p = 10;
}
else if (distance <150 && distance >=100)
{
p=8;
}
else if (distance <200 && distance >=150)
{
p=7;
}
else if (distance <300 && distance >=200)
{
p=6;
}
else if (distance <500 && distance >=300)
{
p=5.5;
}
else if (distance >=500)
{
p=5;
}
double price = weight*distance*p;
if (price > 5000)
{
price = price * 0.95;
System.out.println("因为费用超过了五千所以我们给您在打个九五折优惠");
}
System.out.println("价格为:"+price);
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("请输入一个正整数:");
int num = sc.nextInt();
int a=0;
int temp = 0;
if(num >=0){
for(int i =0; i<99999; i++) {
int tempNum = num;
tempNum= tempNum/10;
a++;
if (tempNum == 0) {
break;
}
}
for(int j=(a-1); j>=0; j--) {
int c= (int)(Math.pow(10, j));
int b = (num/c)%10;
temp=temp + b;
}
System.out.println("各位数的和为:"+temp);
}
else {
System.out.println("您的输入有误!!!!");
}
}
public static void main(String [] args){
int sum = 0;
for (int i = 1; i<=100; i++)
{
if ((i%3)!=0) {
sum+=i;
}
}
System.out.println("1~100之间不能被3整除的数之和为:"+sum);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("请输入需要求阶层的数:");
int n = sc.nextInt();
int sum =1;
for(int i=n; i>0 ;i--) {
sum*=i;
}
System.out.println(n+"的阶层为:"+sum);
}
public static void main(String[] args) {
for(int i= 10000 ; i<100000; i++) {
int a = i%10; //取得个位上的数字
int b = (i/10)%10; //取得十位上的数字
int c = (i/1000)%10; //取得千位上的数字
int d = (i/10000)%10; //取得万位上的数字
if(a==d && b==c) {
//没有必要比较百位上的数字
System.out.println(i);
}
}
}
public static void main(String[] args) {
int num =0;
for(int i = 100; i<1000 ;i++) {
int a = i%10; //取得个位上的数字
int b = (i/10)%10; //取得十位上的数字
int c = (i/100)%10; //取得百位上的数字
int temp = (a*a*a)+(b*b*b)+(c*c*c);
if (temp==i)
{
num++;
System.out.println(i);
}
}
System.out.println("水仙花数的个数为:"+num);
}
public static void main(String[] args) {
double sHeight = 8848.13 * 1000.0;
double zHeight = 0.08;
int i=0;
while (zHeight <= sHeight) {
zHeight = zHeight+zHeight;
i++;
}
System.out.println("对折"+i+"次后可以");
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("请输入月份:");
int b=1;
int c=1;
int s=1;
int temp;
int a = sc.nextInt();
if( a==1 || a==2) {
System.out.println("兔子的数量为1对");
}else {
for(int i = 3;i<=a;i++) {
s=b+c;
c=b;
b=s;
}
}
System.out.println("兔子的数量为"+s+"对");
}
public static void main(String[] args) {
for(int i = 1; i<=5;i++) {
for(int a= 1;a<=1+(i-1)*2;a++)
{
System.out.print("*");
}
System.out.println("");
}
}
public static void main(String[] args) {
for(int i=5; i>=1;i--) {
for(int a=1 ;a<=i;a++)
{
System.out.print("*");
}
System.out.println("");
}
}
public static void main(String[] args) {
int s=5;
for(int i= 5; i>=1;i--) {
//1.首先把*号前面的空格打印出来
for( int a =1; a<=i;a++) {
System.out.print(" ");
}
//2.然后打印*
for(int b=0;b<=(5-i);b++) {
System.out.print("*");
System.out.print(" ");
}
System.out.println("");
}
}
public static void main(String[] args) {
for(int i=1;i<=9;i++) {
for(int a=1; a<=i;a++) {
System.out.print(a+"x"+i+"="+(a*i)+"\t");
}
System.out.println("");
}
}
public class Method03 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("请输入一个数:");
int a = sc.nextInt();
for(int i =2;i<a;i++) {
if (a%i==0) {
System.out.println(a+"不是素数");
return;
}
}
System.out.println(a+"是素数");
}
}
public class Test {
public static void main(String[] args) {
System.out.print("请输入一个正整数:");
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
System.out.println(convert(a));
}
public static String convert(int a) {
String str="";
int temp =a;
int i=0;
do {
temp= temp/10;
i++;
} while (temp!=0);
for(int x=(i-1); x>=0;x--) {
int num= (a/(int)(Math.pow(10, x)))%10;
str=str+num;
}
str=str+'?';
for(int j=0; j<=(i-1);j++) {
int num= (a/(int)(Math.pow(10, j)))%10;
str=str+num;
}
return str;
}
}
public class Method09 {
public static void main(String[] args) {
long num= 0;
Scanner sc = new Scanner(System.in);
do {
System.out.print("请输入一个正整数:");
num = sc.nextLong();
if(num<1) {
System.out.println("您的输入有误!!!!!!!请重新输入!");
}
} while (num<1);
String str ="";
str=isPalindromeNumber(num)? "是回文数":"不是回文数";
System.out.println(str);
}
public static boolean isPalindromeNumber(long num) {
int i=0;
long temp = num;
String str="";
String str1="";
//判断是几位数
do {
temp= temp/10;
i++;
} while (temp!=0);
//如果是否是偶数位
if (i%2==0) {
for(int j=0;j<(i/2);j++) {
long sum = (num/(long)(Math.pow(10, j)))%10;
//把整数的前半部分从前到后以String类的形式拼接
str=str+sum;
}
for(int j=(i-1);j>=(i/2);j--) {
long sum = (num/(long)(Math.pow(10, j)))%10;
//把整数的后半部分从后到前以String类的形式拼接
str1=str1+sum;
}
if (str.equals(str1)) {
return true;
}
}
//判断是否是奇数位
else if (i%2==1) {
for(int j=0;j<(i/2);j++) {
long sum = (num/(long)(Math.pow(10, j)))%10;
//把整数的前半部分从前到后(不拼接整数中间那个数)以String类的形式拼接
str=str+sum;
}
for(int j=(i-1);j>(i/2);j--) {
long sum = (num/(long)(Math.pow(10, j)))%10;
//把整数的后半部分从后到前(不拼接整数中间那个数)以String类的形式拼接
str1=str1+sum;
}
if (str.equals(str1)) {
return true;
}
}
return false;
}
}
public static void main(String[] args) {
System.out.println("请输入一个大写字母:");
Scanner sc=new Scanner(System.in);
while(true){
String str=sc.next();
char ch=str.charAt(0);
while(ch>'Z'||ch<'A')
{
System.out.println("请输入一个大写字母:");
str=sc.next();
ch=str.charAt(0);
}
print(ch);
}
}
public static void print(char ch) {
for(char j,i='A';i<=ch;++i){
for(j='A';j<=i;++j) {
System.out.print(j);
}
for(j-=2;j>='A';--j) {
System.out.print(j);
}
System.out.println();
}
}
public static void main(String[] args) {
print();
}
public static void print() {
for (int i=1;i<=5;i++) {
for(int a=1;a<=(5-i);a++) {
System.out.print(" ");
}
for(int j=1;j<=i;j++) {
System.out.print("* ");
}
System.out.println("");
}
System.out.println("\n");
for(int a=1;a<=5;a++) {
for(int i=1;i<=5;i++) {
System.out.print("* ");
}
System.out.println("");
}
System.out.println("\n");
for(int a=1;a<=5;a++) {
for(int i=1 ;i<a;i++) {
System.out.print(" ");
}
for(int j=0;j<=(5-a);j++) {
System.out.print("* ");
}
System.out.println("");
}
}
public static void main(String[] args) {
print();
}
public static void print() {
for(int a=1;a<=5;a++) {
//控制行数,换行
for(int i=0;i<=(5-a);i++) {
//控制打印等腰三角形前面的空格
System.out.print(" ");
}
for(int b=1;b<=a;b++) {
//控制打印组成等腰三角型的*号和空格
System.out.print("*");
if(b==a) {
//不要打印每行最后的空格
break;
}
System.out.print(" ");
}
for(int c=1;c<(5-a);c++) {
//打印三角型后边的空格
System.out.print(" ");
}
for(int d=1;d<=2;d++) {
//打印三角形后边的*号
System.out.print("*");
if(a==5) {
//使最后一行只打印一个*号
break;
}
}
System.out.println("");
}
for(int i=1;i<=6;i++) {
//打印最下面的矩形
for(int a=1;a<=6;a++) {
System.out.print("* ");
}
System.out.println("");
}
}
public static void main(String[] args) {
seekNum();
}
public static void seekNum() {
int temp=0;
int x=0;
outLoop:for(int a=39;a>1;a--) {
for(int b=2;b<a;b++) {
if(a%b==0) {
break;
}
else if (b==(a-1)) {
temp+=a; //求40以内最大的三个素数
x++;
if(x==3) {
break outLoop;
}
}
}
}
for(int a=1;a<1000;a++) {
if(a%temp==0) {
System.out.print(a+"\t");
}
}
}
public static void main(String[] args) {
for(int i=1;i<=6;i++) {
for(int a=1;a<=(6-i);a++) {
System.out.print(" ");
}
System.out.print("* ");
for(int b=2;b<i;b++) {
System.out.print(" ");
if(i>=3) {
System.out.print(" ");
}
}
if(i>=2) {
System.out.print("*");
}
System.out.println("");
}
for (int a=1;a<=5;a++) {
for(int i=1;i<=a;i++) {
System.out.print(" ");
}
System.out.print("* ");
for(int b=(5-a);b>1;b--) {
System.out.print(" ");
}
if(a<5) {
System.out.print("*");
}
System.out.println("");
}
}
public static void main(String[] args) {
for(int i=1;i<=4;i++) {
for(int a=1;a<=(4-i)*2;a++) {
System.out.print(" ");
}
for(int j=1;j<=6;j++) {
if((i==2||i==3) &&(j>1 && j<6) ) {
System.out.print(" ");
}
else {
System.out.print("* ");
}
}
System.out.println("");
}
}
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("提示如果输入的是偶数则程序会输出一个这个偶数加一之后的奇数的菱形");
System.out.println("请输入一个数:");
int a= sc.nextInt();
print(a);
}
public static void print(int a) {
for (int i=1;i<=(a/2)+1;i++) {
for(int b=i;b<=(a/2);b++) {
System.out.print(" ");
}
for(int c=1;c<=i;c++) {
System.out.print("*");
}
for(int c=2;c<=i;c++) {
System.out.print("*");
}
System.out.println("");
}
for (int i=1;i<=(a/2);i++) {
for(int b=1;b<=i;b++) {
System.out.print(" ");
}
for(int c=i;c<=(a/2);c++) {
System.out.print("*");
}
for(int c=i+1;c<=(a/2);c++) {
System.out.print("*");
}
System.out.println("");
}
}
}
例如:源数组,1 2 3 4 5 6 转置之后的数组,6 5 4 3 2 1
public static void main(String[] args) {
int[] arr= {
6, 5, 4, 3, 2, 1};
for(int i=0; i<(arr.length)/2;i++) {
int temp = arr[i];
arr[i] = arr[arr.length-1-i];
arr[arr.length-1-i] = temp;
}
System.out.println(Arrays.toString(arr));
}
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("程序将自动生成有233个元素的数组所有的元素的值都在0到99请输入一个数值,\n程序将查找这数值在不在这个随机生成的数组中,如果在则会把相应的下标打印出来,如果没有则会打印-1.");
System.out.print("请输入一个0到99的整数:");
int key = sc.nextInt();
int[] arr=new int[233];
for(int i=0;i<arr.length;i++) {
arr[i]=(int)(Math.random()*100);
}
System.out.println(Arrays.toString(arr));
int[] arr1 =ordinalSearch(arr,key);
System.out.println(Arrays.toString(arr1));
}
public static int[] ordinalSearch(int[]arr2,int b) {
int a=0;
int[] arr;
for(int i=0;i<arr2.length;i++) {
if(arr2[i]==b) {
a++;
}
}if(a==0) {
arr=new int[1];
arr[0]=-1;
}else {
arr=new int[a];
for(int i=0;i<arr2.length;i++) {
if(b == arr2[i]) {
a=a-1;
arr[a]=i;
}
}
}
return arr;
}
}
public class Test {
public static void main(String[] args) {
int oldArr[]={
1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} ;
int newArr[] =deleteZero(oldArr);
System.out.println(Arrays.toString(oldArr
));
System.out.println(Arrays.toString(newArr));
}
public static int[] deleteZero(int[] arr) {
int a=0;
for(int i=0;i<arr.length;i++) {
if(arr[i]!=0) {
a++;
}
}
int x=0;
int[] arr1= new int[a];
for(int i=0;i<arr.length;i++) {
if(arr[i]!=0) {
arr1[x]=arr[i];
x++;
}
}
return arr1;
}
}
public class Test {
public static void main(String[] args) {
int[] arr= {
1,7,9,11,13,15,17,19};
int[] arr1= {
2,4,6,8,10};
int[] newArr=harmony(arr, arr1);
System.out.println(Arrays.toString(newArr));
}
public static int[] harmony(int[] arr, int[] arr1) {
int[] newArr= new int[arr.length+arr1.length];
for(int i=0;i<arr.length;i++) {
newArr[i]=arr[i];
}
for(int i=0;i<arr1.length;i++) {
newArr[i+arr.length]=arr1[i];
}
System.out.println(Arrays.toString(newArr));
for(int i=0;i<newArr.length;i++) {
for(int a=i+1;a<newArr.length;a++) {
int temp=0;
if(newArr[i]>newArr[a]) {
temp=newArr[i];
newArr[i]=newArr[a];
newArr[a]=temp;
}
}
}
return newArr;
}
}
public class Exercises04 {
public static void main(String[] args) {
int[] score= {
72,89,65,58,87,91,53,82,71,93,76,68};
classification(score);
}
public static int[][] classification(int[] arr){
int[][] count=new int[5][];
int a=0,b=0,c=0,d=0,e=0;
for(int i=0;i<arr.length;i++) {
if(arr[i]>=90) {
a++;
}
if(arr[i]>=80 && arr[i]<=89) {
b++;
}
if(arr[i]>=70 && arr[i]<=79) {
c++;
}
if(arr[i]>=60 && arr[i]<=69) {
d++;
}
if(arr[i]<60) {
e++;
}
}
count[0] =new int[e];
count[1] =new int[d];
count[2] =new int[c];
count[3] =new int[b];
count[4] =new int[a];
for(int i=0;i<arr.length;i++) {
if(arr[i]>=90) {
a=a-1;
count[4][a]=arr[i];
}
if(arr[i]>=80 && arr[i]<=89) {
b=b-1;
count[3][b]=arr[i];
}
if(arr[i]>=70 && arr[i]<=79) {
c=c-1;
count[2][c]=arr[i];
}
if(arr[i]>=60 && arr[i]<=69) {
d=d-1;
count[1][d]=arr[i];
}
if(arr[i]<60) {
e=e-1;
count[0][e]=arr[i];
}
}
for(int i=0;i<count.length;i++) {
System.out.print(Arrays.toString(count[i]));
}
return count;
}
}
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("请输入需要打印杨辉三角形的行数:");
int a=sc.nextInt();
print(a);//打印上图第二种格式
println(a);//打印上图第一种格式
}
public static void print(int a) {
long[][]arr = new long[a][];
for(int i=0;i<arr.length;i++) {
arr[i]=new long[i+1];
}
for(int i=0;i<arr.length;i++) {
for(int j=0;j<arr[i].length;j++) {
if(j==0|| j==arr[i].length-1) {
arr[i][j]=1;
}else {
arr[i][j]= arr[i-1][j-1]+arr[i-1][j];
}
System.out.print(arr[i][j]+" ");
}
System.out.println("");
}
}
public static void println(int a) {
long[][]arr = new long[a][];
for(int i=0;i<arr.length;i++) {
arr[i]=new long[i+1];
}
for(int i=0;i<arr.length;i++) {
for(int x=a-i-1;x>0;x--) {
System.out.print(" ");
}
for(int j=0;j<arr[i].length;j++) {
if(j==0|| j==arr[i].length-1) {
arr[i][j]=1;
}else {
arr[i][j]= arr[i-1][j-1]+arr[i-1][j];
}
System.out.print(arr[i][j]+" ");
}
System.out.println("");
}
}
}