救救孩子,没有其他办法了

编写一个Java程序来读取用户名作为输入。你的程序应该打印用户名和它有多少个字符。

请输入你的名字:蒂姆

请输入你的姓:泰勒

你好,蒂姆·泰勒!

你的名字有3个字符。

你的姓有6个字符。

使用StringBuffer类,修改程序以按相反顺序打印用户的全名

修改程序以随机顺序打印用户名和数组中每个元素的索引值。不得重复任何字符。

例如,i t a y r o m t l

0 1 2 3 4 5 6 7 8 9

修改程序以询问用户是否要输入其他名称。给用户一个选项,读入字符“Y”以再次播放,读入字符“N”以退出程序。

全面测试你的程序。包括错误检查,这样如果用户输入了无效字符(名称不能包含特殊字符或标点符号),程序会告诉用户必须再次输入名称。

你的问题需要用到很多代码,我建议自己试着一步步写,先定义两变量接收姓名,然后再完成剩下需求!

你这个最起码自己写一点啊,不会后面的条件最起码会写个循环写个Scanner吧。把你写完的复制出来别人好帮你,你自己也能进步,直接给你写完,你还是一脸懵

package worksheet4;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class Exercise4 {
    public static void input() {
        @SuppressWarnings("resource")
        Scanner input=new Scanner(System.in);
        System.out.println("Please input your first name:");
        @SuppressWarnings("unused")
        String firstname=input.nextLine();
        System.out.println("Please input your family name:");
        @SuppressWarnings("unused")
        String familyname=input.nextLine();
        String fullname=familyname+firstname;
        @SuppressWarnings("unused")
        StringBuffer str=new StringBuffer(fullname);
         
        System.out.println("Hello "+str);
        System.out.println("Your full name has"+firstname.length()+"characters。");
        System.out.println("Your family name has"+familyname.length()+"characters。");    
        String a[]=new String[str.length()];
         
        for(int i=0;i<str.length();i++) {
                a[i]= str.charAt(i) + "";            
        }
         
        String out;
        int outIndex=0;
        ArrayList<String> list = new ArrayList<String>();
        for(String i:a){
        list.add(i);
        }
        for(int i=0;i<a.length;i++){
        Random r= new Random();
        outIndex = r.nextInt(list.size());
        out = list.get(outIndex);
        list.remove(outIndex);
        System.out.printf("%s  ",out);
        }
        System.out.printf("\n");
        for(int i=0; i<str.length(); i++) {
            System.out.printf("%d  ",i);
        }
    }
    public static void main(String args[]) throws IOException {
        input();
        System.out.println("\nif input other name");
        System.out.println("input char“Y”for input again");
        System.out.println("input char“N”for out program");
        char c=(char)System.in.read(); 
        if(c=='Y') {
            input();
        }
        if(c=='N') {
            System.exit(-1);
        }

    }

}
 

您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~

如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632