用for循环完成如下案例
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
求和
public class ForCircle{
public static void main(String[] args){
int sum=0;
int random=0;
for(int i=1;i<=10;i++){
random=(int)(Math.random()*100+1);
sum +=random;
System.out.println(random);
}
System.out.println("合为"+sum);
System.out.println("平均值为"+sum/10.0);
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
求偶数和
public class D{
public static void main(String[] args){
int sum=0;
int random=0;
for(int i=1;i<=10;i++){
random=(int)(Math.random()*100+1);
if(random % 2==0){
sum +=random;
System.out.println(random);
}
}
System.out.println("合为"+sum);
System.out.println("平均值为"+sum/10.0);
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
求奇数和
public class D{
public static void main(String[] args){
int sum=0;
int random=0;
for(int i=1;i<=10;i++){
random=(int)(Math.random()*100+1);
if(random % 2!=0){
sum +=random;
System.out.println(random);
}
}
System.out.println("合为"+sum);
System.out.println("平均值为"+sum/10.0);
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
打印水仙花数
public class D{
public static void main(String[] args){
for(int i=100 ;i<=999; i++){
if( ((i%10)*(i%10)* (i%10)+(i/10%10)*(i/10%10)* (i/10%10)+(i/10/10)* (i/10/10)* (i/10/10) )==i){
System.out.print("水仙花数:"+i+" , ");
}
}
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
统计水仙花数
public class D{
public static void main(String[] args){
int count=0;
for(int i=100 ;i<=999; i++){
if( ((i%10)*(i%10)* (i%10)+(i/10%10)*(i/10%10)* (i/10%10)+(i/10/10)* (i/10/10)* (i/10/10) )==i){
System.out.print("水仙花数:"+i+" , ");
count++;
}
}
System.out.print("水仙花数个数:"+count);
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
九九乘法表
public class D{
public static void main(String[] args){
int count=0;
for(int i=1 ;i<=9; i++){
for(int j=1;j<=i;j++){
System.out.print(j+"*"+i+"="+(i*j)+" ");
}
System.out.println();
}
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
用while循环完成如下案例
求和
public class D{
public static void main (String[] args){
int i=1;
int sum=0;
while(i<=10){
sum += (int)(Math.random()*100+1);
i++;
}
System.out.println("和为:"+sum);
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
纸张折叠成珠穆朗玛峰高度的次数
public class D{
public static void main(String[] args){
int num=0;
int count=8848;
int high=1;
do{
high = high*2;
num++;
}while(count>=high);
System.out.print(num);
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
使用冒泡排序把随机生成的10个数从小到大排列
public class D{
public static void main (String[] args){
int[] str = new int[10];
for(int i = 0 ; i < 10 ; i ++){
str[i] = (int)(Math.random()*100);
}
System.out.println("排序前");
for(int q=0;q<str.length;q++){
System.out.print(str[q]+" ");
}
System.out.println("");
for(int x=0 ; x < str.length-1 ; x++){
for(int y=0;y < str.length-x-1 ; y++ ){
int temp;
if (str[y]>=str[y+1]){
temp=str[y+1];
str[y+1]=str[y];
str[y]=temp;
}
}
}
System.out.println("排序后");
for(int w=0;w<str.length;w++){
System.out.print(str[w]+" ");
}
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
:银行登录业务逻辑事项
//以下for实现
import java.util.Scanner;
public class ForCircle{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
int i=1;
for(;i<=3;i++){
System.out.println("请输入密码:");
int pass = scan.nextInt();
if(pass == 123){
System.out.println("密码正确");
break;
}else{
System.out.println("密码错误");
}
}
if(i == 4){
System.out.println("三次密码输入错误,请到银行办理吞卡业务");
}
return ;
}
}
//以下while实现
import java.util.Scanner;
public class ForCircle{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
int count=1;
while( true ){
System.out.println("请输入密码:");
int pass = scan.nextInt();
if(pass == 123){
System.out.println("密码正确");
break;
}else{
System.out.println("密码错误,剩余"+(3-count)+"次");
}
count++;
if(count==4){
System.out.println("多次输入不正确,被吞卡");
break;
}
}
return ;
}
}
嵌套循环实例:冒泡排序
public class BubbleSort {
public static void main (String[] args){
int[] str = new int[10];
for(int i = 0 ; i < 10 ; i ++){
str[i] = (int)(Math.random()*100);
}
System.out.println("排序前");
for(int q=0;q<str.length;q++){
System.out.print(str[q]+" ");
}
System.out.println("");
for(int x=0 ; x < str.length-1 ; x++){
for(int y=0;y < str.length-x-1 ; y++ ){
int temp;
if (str[y]>=str[y+1]){
temp=str[y+1];
str[y+1]=str[y];
str[y]=temp;
}
}
}
System.out.println("排序后");
for(int w=0;w<str.length;w++){
System.out.print(str[w]+" ");
}
}
}