Java 我的字符串名称在循环中重复?

Java 我的字符串名称在循环中重复?,java,string,ascii,Java,String,Ascii,问题:通过删除基本ASCII字符打印字符串 这是打印代码表-> 显然,问题是,如果ASCII数是素数,这意味着您必须删除字符。这意味着如果我提供输入 输入 4 MEHTA Mehta HELLO hello 输出必须为 MEHTA Mht HELL hllo 好的,最后我希望你能理解上面的问题。。就我所尝试的代码而言: package lesson.practice; import java.util.*; public class MainProgram { public st

问题:通过删除基本ASCII字符打印字符串

这是打印代码表->

显然,问题是,如果ASCII数是素数,这意味着您必须删除字符。这意味着如果我提供输入

输入

4
MEHTA
Mehta
HELLO
hello
输出必须为

MEHTA
Mht
HELL
hllo
好的,最后我希望你能理解上面的问题。。就我所尝试的代码而言:

package lesson.practice;

import java.util.*;

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

        Scanner in=new Scanner(System.in);
        System.out.println("Enter your number:");
        int num=in.nextInt();
        String name = null;

        System.out.println("Enter name");
        for(int i=0; i<=num; i++){

            name=in.nextLine();
        }

        String str1=name.replace(new String(Character.toChars(97)), "").
                replace(new String(Character.toChars(101)), "").
                replace(new String(Character.toChars(103)), "").
                replace(new String(Character.toChars(107)), "").
                replace(new String(Character.toChars(107)), "").
                replace(new String(Character.toChars(113)), "").
                replace(new String(Character.toChars(117)), "").
                replace(new String(Character.toChars(119)), "").
                replace(new String(Character.toChars(67)), "").
                replace(new String(Character.toChars(71)), "").
                replace(new String(Character.toChars(73)), "").
                replace(new String(Character.toChars(79)), "").
                replace(new String(Character.toChars(81)), "").
                replace(new String(Character.toChars(83)), "").
                replace(new String(Character.toChars(87)), "").
                replace(new String(Character.toChars(89)), "");

        System.out.println("PRIME ASCII ARE REMOVED:");
        for(int i=1; i<=num; i++){
            System.out.println(str1);
        }
    }
}

您的代码应该如下所示:

public static void main(String[] args){

    Scanner in = new Scanner(System.in);
    System.out.println("Enter your number:");
    int num = in.nextInt();
    in.nextLine();

    System.out.println("Enter name");
    String[] result = new String[num];
    for (int i = 0; i < num; i++) {
        String name = in.nextLine();
        String str1 = name.replace(new String(Character.toChars(97)), "")
                .replace(new String(Character.toChars(101)), "")
                .replace(new String(Character.toChars(103)), "")
                .replace(new String(Character.toChars(107)), "")
                .replace(new String(Character.toChars(107)), "")
                .replace(new String(Character.toChars(113)), "")
                .replace(new String(Character.toChars(117)), "")
                .replace(new String(Character.toChars(119)), "")
                .replace(new String(Character.toChars(67)), "")
                .replace(new String(Character.toChars(71)), "")
                .replace(new String(Character.toChars(73)), "")
                .replace(new String(Character.toChars(79)), "")
                .replace(new String(Character.toChars(81)), "")
                .replace(new String(Character.toChars(83)), "")
                .replace(new String(Character.toChars(87)), "")
                .replace(new String(Character.toChars(89)), "");

        result[i] = str1;

    }

    System.out.println("PRIME ASCII ARE REMOVED:");
    for(int i=0; i<num; i++){
        System.out.println(result[i]);
    }

    in.close();
}
publicstaticvoidmain(字符串[]args){
扫描仪输入=新扫描仪(系统输入);
System.out.println(“输入您的号码:”);
int num=in.nextInt();
in.nextLine();
System.out.println(“输入名称”);
字符串[]结果=新字符串[num];
for(int i=0;i对于(int i=0;i,因为您正在覆盖循环中的
name
str1
变量,所以您的输出是最后一次输入的结果。要保存所有输入,然后转换它们,您必须执行以下操作:

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.println("Enter your number:");
    int num = in.nextInt();
    in.nextLine(); // so we get rid of the enter key after the number was pressed

    System.out.println("Enter name");
    String[] names = new String[num];
    for (int i = 0; i < num; i++) {
        String name = in.nextLine();
        String str1 = name.replace(new String(Character.toChars(97)), "").replace(new String(Character.toChars(101)), "")
                .replace(new String(Character.toChars(103)), "").replace(new String(Character.toChars(107)), "")
                .replace(new String(Character.toChars(107)), "").replace(new String(Character.toChars(113)), "")
                .replace(new String(Character.toChars(117)), "").replace(new String(Character.toChars(119)), "")
                .replace(new String(Character.toChars(67)), "").replace(new String(Character.toChars(71)), "")
                .replace(new String(Character.toChars(73)), "").replace(new String(Character.toChars(79)), "")
                .replace(new String(Character.toChars(81)), "").replace(new String(Character.toChars(83)), "")
                .replace(new String(Character.toChars(87)), "").replace(new String(Character.toChars(89)), "");

        names[i] = str1;

    }

    System.out.println("PRIME ASCII ARE REMOVED:");
    for (String removedName : names) {
        System.out.println(removedName);
    }
}
publicstaticvoidmain(字符串[]args){
扫描仪输入=新扫描仪(系统输入);
System.out.println(“输入您的号码:”);
int num=in.nextInt();
in.nextLine();//因此,我们在按下数字后取消了enter键
System.out.println(“输入名称”);
字符串[]名称=新字符串[num];
for(int i=0;i

此方法将获得名称输入,删除素数字符并将结果保存在名称数组中。之后,我们将数组打印到标准输出。

我想我有一个解决您问题的方法。这是我编写的代码

import java.util.*;
import java.io.*;
class ASC{
public static boolean chkprim(int no){
    for(int i=2;i<no;i++){
        if(no%i==0)
        return false;
    }
    return true;
}
public static void main(String args[])throws IOException{
    int n;
    Scanner sc=new Scanner(System.in);

    System.out.println("Enter no. of values");
    n=sc.nextInt();
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    int i=1;
    String arr[]=new String[n];
    while(i<=n){
        System.out.println("Enter the word:-");
        String st=br.readLine();
        arr[i-1]=st;
        i++;
    }

    for(i=0;i<n;i++){
        String str=arr[i];
        for(int j=0;j<str.length();j++){
            int num=(int)str.charAt(j);
            if(chkprim(num)==false)
            System.out.print(str.charAt(j));
        }
        System.out.println();
    }
}
}
import java.util.*;
导入java.io.*;
ASC类{
公共静态布尔chkprim(整数编号){

对于(int i=2;iYou每次读取输入时都会重写字符串变量“name”。您应该创建一个大小为
num
的数组,并将字符串存储在其中。或者使用一个可以动态增长的
列表。您应该使用算法检查ascii值是否为prime,而不是硬编码。谢谢……但我猜它仍然是如果您想读取用户所需的字符串,则可以不创建数组。但是您的赋值看起来一个字符串就足够了。它引发了一个
异常…
Onething希望从您那里知道…您为什么要在.nextInt()中编写
??…当我删除它时,它显示的是输入数较少的输出..为什么?你能告诉我吗?你的代码中也有它,或者你的意思是
在.nextLine();
?因为如果没有它,那么名字将是用户输入数字的第一行的剩余部分!也就是说,它将是空的(因为
nextLine()
不会返回
\n
)将for循环中的条件更改为
,我感谢您的建议!它起到了神奇的效果。但是告诉我一件事,我的代码执行正确吗?
import java.util.*;
import java.io.*;
class ASC{
public static boolean chkprim(int no){
    for(int i=2;i<no;i++){
        if(no%i==0)
        return false;
    }
    return true;
}
public static void main(String args[])throws IOException{
    int n;
    Scanner sc=new Scanner(System.in);

    System.out.println("Enter no. of values");
    n=sc.nextInt();
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    int i=1;
    String arr[]=new String[n];
    while(i<=n){
        System.out.println("Enter the word:-");
        String st=br.readLine();
        arr[i-1]=st;
        i++;
    }

    for(i=0;i<n;i++){
        String str=arr[i];
        for(int j=0;j<str.length();j++){
            int num=(int)str.charAt(j);
            if(chkprim(num)==false)
            System.out.print(str.charAt(j));
        }
        System.out.println();
    }
}
}