Java 如何从名字中随机抽取3个字母,从姓氏和电话号码中随机抽取2个字母,从给定字符串中随机抽取1个符号';特殊卡';
程序应随机生成8个字符长的密码,如下所示:Java 如何从名字中随机抽取3个字母,从姓氏和电话号码中随机抽取2个字母,从给定字符串中随机抽取1个符号';特殊卡';,java,string,random,passwords,generator,Java,String,Random,Passwords,Generator,程序应随机生成8个字符长的密码,如下所示: 3个来自名字的随机字母 2个来自姓氏的随机字母 移动电话号码中的2个随机数字 变量“specialChar”中的1个随机字符 1/n次循环 int index=(new Random()).nextInt(specialChar.length()); 然后将specialChar.charAt(索引)放入每个字符串的结果数组中。将问题分解为更小的部分。说第一个名字是“帕特里克”。如何从字符串“Patrick”中随机选取三个字母?编写一小段代码来实现这一
然后将specialChar.charAt(索引)放入每个字符串的结果数组中。将问题分解为更小的部分。说第一个名字是“帕特里克”。如何从字符串“Patrick”中随机选取三个字母?编写一小段代码来实现这一点,并对其进行测试。然后将其添加到程序的正确位置
使用类似的逻辑从姓氏和特殊字符字符串中选择。您可以这样做
public class testingstring {
public static void main(String[] args)
{
Scanner userInput = new Scanner (System.in) ;
String specialChar = "!@#$%^&*()" ;
System.out.println("Enter your first name: ");
String firstName = userInput.next() ;
System.out.println("Enter your last name: ") ;
String lastName = userInput.next() ;
System.out.println("Enter your phone number: ") ;
String phoneNum = userInput.next() ;
}
}
import java.util.Scanner;
公共类生成类{
公共静态void main(字符串[]args){
字符串[]Arr=新字符串[4];
扫描仪S=新的扫描仪(System.in);
int[]N=新的int[4];
char[]Pass=新字符[8];
System.out.println(“您的名字是什么?”);
Arr[0]=S.nextLine();
System.out.println(“你姓什么?”);
Arr[1]=S.nextLine();
System.out.println(“你的手机是什么?”);
Arr[2]=S.nextLine();
Arr[3]=“!@$%^&*()”;
用于(int pos=0;pos<4;pos++){
N[pos]=Arr[pos].length()-1;
开关(pos){
案例0:for(int count=0;count<3;count++)
Pass[count]=Arr[0].charAt((int)Math.round(Math.random()*N[0]);
打破
案例1:for(int count=3;count<5;count++)
Pass[count]=Arr[1].charAt((int)Math.round(Math.random()*N[1]);
打破
案例2:for(int count=5;count<7;count++)
Pass[count]=Arr[2].charAt((int)Math.round(Math.random()*N[2]);
打破
案例3:for(int count=7;count<8;count++)
Pass[count]=Arr[3].charAt((int)Math.round(Math.random()*N[3]);
打破
}}
System.out.print(“您的新密码:”);
用于(int pos=0;pos<8;pos++){
系统输出打印(通过[pos]);
}}}
你能给我一个名字的示例代码吗?能给我一个示例代码吗?不,我们不是代码编写服务。你自己写代码,贴在这里,我们会帮你的。
import java.util.Scanner;
public class GeneratePass {
public static void main(String[] args) {
String[] Arr = new String[4];
Scanner S = new Scanner(System.in);
int[] N = new int[4];
char[] Pass = new char[8];
System.out.println("What is your first name?");
Arr[0] = S.nextLine();
System.out.println("What is your last name?");
Arr[1] = S.nextLine();
System.out.println("What is your mobile phone?");
Arr[2] = S.nextLine();
Arr[3] = "!@#$%^&*()";
for (int pos = 0; pos < 4; pos++) {
N[pos] = Arr[pos].length() - 1;
switch (pos) {
case 0:for (int count = 0; count < 3; count++)
Pass[count] = Arr[0].charAt((int) Math.round(Math.random()*N[0]));
break;
case 1:for (int count = 3; count < 5; count++)
Pass[count] = Arr[1].charAt((int) Math.round(Math.random()*N[1]));
break;
case 2:for (int count = 5; count < 7; count++)
Pass[count] = Arr[2].charAt((int) Math.round(Math.random()*N[2]));
break;
case 3:for (int count = 7; count < 8; count++)
Pass[count] = Arr[3].charAt((int) Math.round(Math.random()*N[3]));
break;
}}
System.out.print("Your new password: ");
for (int pos = 0; pos < 8; pos++) {
System.out.print(Pass[pos]);
}}}