java中loop什么意思_有人能把这个java代码改用loop写吗?

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

import javax.swing.JOptionPane;

public class pg228ex3 {

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

int c0,c1,c2,c3,c4;

int d0,d1,d2,d3,d4;

String a=JOptionPane.showInputDialog("Please set up a five-digit password:");

String a0=a.substring(0,1);

String a1=a.substring(1,2);

String a2=a.substring(2,3);

String a3=a.substring(3,4);

String a4=a.substring(4,5);

String b=JOptionPane.showInputDialog("Please guess the five-digit password: ");

String b0=b.substring(0,1);

String b1=b.substring(1,2);

String b2=b.substring(2,3);

String b3=b.substring(3,4);

String b4=b.substring(4,5);

if (a0.compareTo(b0)==0)

{c0=1;

d0=Integer.parseInt(a0);}

else

{c0=0;

d0=0;}

if (a1.compareTo(b1)==0)

{c1=1;

d1=Integer.parseInt(a1);}

else

{c1=0;

d1=0;}

if (a2.compareTo(b2)==0)

{c2=1;

d2=Integer.parseInt(a2);}

else

{c2=0;

d2=0;}

if (a3.compareTo(b3)==0)

{c3=1;

d3=Integer.parseInt(a3);}

else

{c3=0;

d3=0;}

if (a4.compareTo(b4)==0)

{c4=1;

d4=Integer.parseInt(a4);}

else

{c4=0;

d4=0;}

int numberOfcorrect;

numberOfcorrect = c0 + c1 +c2 +c3 +c4;

int sum;

sum = d0 + d1 + d2 + d3 + d4;

switch (numberOfcorrect)

{

case 0:

JOptionPane.showMessageDialog(null,"You are out of luck.\n" + "The number of digits in the correct position is 0.\n" + "The sum of digits in the correct position is " + sum);

break;

case 1:

JOptionPane.showMessageDialog(null,"You are not good at guessing.\n" + "The number of digits in the correct position is 1\n" + "The sum of digits in the correct position is " + sum);

break;

case 2:

JOptionPane.showMessageDialog(null,"Not bad.Go find the know-how.\n" + "The number of digits in the correct position is 2\n" + "The sum of digits in the correct position is " + sum);

break;

case 3:

JOptionPane.showMessageDialog(null,"A good start.\n" + "The number of digits in the correct position is 3\n" + "The sum of digits in the correct position is " + sum);

break;

case 4:

JOptionPane.showMessageDialog(null,"Try it again.Very close.\n " + "The number of digits in the correct position is 4\n" + "The sum of digits in the correct position is " + sum);

break;

case 5:

JOptionPane.showMessageDialog(null,"Congratulations!\n" + "The number of digits in the correct position is 5\n" + "The sum of digits in the correct position is " + sum);

break;

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值