Im having a lot of trouble with randoms in Java and was wondering if someone could help? I am quite new to coding

huangapple go评论88阅读模式
英文:

Im having a lot of trouble with randoms in Java and was wondering if someone could help? I am quite new to coding

问题

将一个随机字符转换为大写,将一个随机字符替换为随机数字,在密码中间添加波浪符~。以下是我在作业中已经完成的部分。

  1. import java.util.Scanner;
  2. public class Main {
  3. public static void main(String[] args) {
  4. String pw;
  5. int pwl;
  6. Scanner scan = new Scanner(System.in);
  7. System.out.println("Enter your word passcode:");
  8. pw = scan.next();
  9. pw = (pw.toLowerCase());
  10. int length = pw.length();
  11. String firstChar = pw.substring(0, 1);
  12. String lastChar = pw.substring(length - 1);
  13. pw = lastChar + pw.substring(1, length - 1) + firstChar;
  14. System.out.println(pw);
  15. math.random();
  16. }
  17. }
英文:

Convert a random character to an upper case,Replace a random character with a random digit,Add a tilde ~ character to the middle of the passcode. Here is what I have got so far in my assignment.

  1. import java.util.Scanner;
  2. public class Main
  3. {
  4. public static void main(String[] args)
  5. {
  6. String pw;
  7. int pwl;
  8. Scanner scan = new Scanner(System.in);
  9. System.out.println("Enter your word passcode:");
  10. pw = scan.next();
  11. pw = (pw.toLowerCase());
  12. int length = pw.length();
  13. String firstChar = pw.substring(0, 1);
  14. String lastChar = pw.substring(length - 1);
  15. pw = lastChar + pw.substring(1, length-1) + firstChar;
  16. System.out.println(pw);
  17. math.random();
  18. }
  19. }

答案1

得分: 2

  1. // 将随机字符转换为大写
  2. int random = (int) (Math.random() * pw.length());
  3. pw = pw.substring(0, random) + Character.toUpperCase(pw.charAt(random)) + pw.substring(random + 1);
  4. System.out.println(pw);
  5. // 将随机字符替换为随机数字
  6. random = (int) (Math.random() * pw.length());
  7. pw = pw.substring(0, random) + (int) (Math.random() * 10) + pw.substring(random + 1);
  8. System.out.println(pw);
  9. // 在密码的中间添加波浪符 ~ 字符
  10. int len = pw.length();
  11. pw = pw.substring(0, len / 2) + '~' + pw.substring(len / 2);
  12. System.out.println(pw);
英文:
  1. // Convert a random character to an upper case
  2. int random = (int) (Math.random() * pw.length());
  3. pw = pw.substring(0, random) + Character.toUpperCase(pw.charAt(random)) + pw.substring(random + 1);
  4. System.out.println(pw);
  5. // Replace a random character with a random digit
  6. random = (int) (Math.random() * pw.length());
  7. pw = pw.substring(0, random) + (int) (Math.random() * 10) + pw.substring(random + 1);
  8. System.out.println(pw);
  9. // Add a tilde ~ character to the middle of the passcode.
  10. int len = pw.length();
  11. pw = pw.substring(0, len / 2) + '~' + pw.substring(len / 2);
  12. System.out.println(pw);

huangapple
  • 本文由 发表于 2020年9月29日 02:30:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/64107701.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定