注册 登录
编程论坛 JAVA论坛

java新手,怎么将字符串转变为*号

swchvs 发布于 2015-04-18 17:57, 1948 次点击
输入字符串,但在屏幕上只显示*号,应该怎么做?求举例
8 回复
#2
凌风zx2015-04-19 20:33
不太明白什么意思
都显示*号的话
String str="字符串";
String str2="";
for(i=0;i<str.length;i++)
{
    str2+="*";
}
str=str2;
#3
秦时的明月夜2015-04-19 20:56
程序代码:
import java.util.Scanner;

public class Test{
    public static void main(String[] args) {

        Scanner sc=new Scanner(System.in);
        while(true){
            String s=sc.next();
            if(s.equals("over"))
                break;
            for (int i = 0; i < s.length(); i++) {
                System.out.print("*");
            }
            System.out.println();
        }
        System.out.println("Test结束……");
        
    }   
}
#4
秦时的明月夜2015-04-19 20:57
回复 3楼 秦时的明月夜
程序代码:
abc
***
123414
******
abcdser12341
************
#5
日知己所无2015-04-21 20:05
LZ想要的应该是像ATM上输入密码的功能,你输入1234时,屏幕上显示的只有****,不能显示数字
#6
ComeZjBlaze2015-04-23 22:39
你可以用及时输出的方式,用制表符实现删除等等
#7
swchvs2015-04-27 23:23
回复 5楼 日知己所无
对对,就是想要这个
#8
秦时的明月夜2015-04-29 09:26
回复 楼主 swchvs
程序代码:
import *;
import java.util.*;
public class 无回显密码输入 {
    public static void main(String[] args) {
        Console console = System.console();
        if (console == null) {
            System.err.println("Failed to get console!");
            return;
        }
        String name = console.readLine("Please type your name:");
        char[] password1 = null;
        char[] password2 = null;
        console.printf("Your name is %s\n", name);
      
        password1 = console.readPassword("Please type your password:");
        password2 = console.readPassword("Please type your password again:");
        if (!Arrays.equals(password1, password2)) {
            System.err.println("Password doesn't match the confirmation.");
            return;
        }
        console.printf("Hi, %s, Your password is '%s'\n", name,  new String(password1));
        Arrays.fill(password1, '\0'); /* 安全清理 */
        Arrays.fill(password2, '\0');
    }
}
只支持原生的控制台,像eclipse的控制台是不行的……找不到回显为*的功能……

[ 本帖最后由 秦时的明月夜 于 2015-4-29 09:35 编辑 ]
#9
秦时的明月夜2015-04-29 09:36
回复 楼主 swchvs
程序代码:
#include<stdio.h>
#include<conio.h>
int main(void)
   {
       char a[1000];
       printf(" 输入为 * 结束");
       printf("\n");
       int i=0;
       while(true){
               a[i]=getch();
               if(a[i]=='*')
                   break;
               putchar('*');
               i++;
       }
       printf("\n");
       /*显示*/
      printf("输入的是:\n");
       for(int j=0;j<i;j++){
           printf("%c",a[j]);
       }
       printf("\n");
       return 0;
     }

c语言好实现
1