该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
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;
}
}
}