System.out.printni qanday qilib o'chirishim va noyob qaytishim bor

Men g'alati bir muammo bor. Men turli System.out.print to'plash va dastur oxirida yagona String qaytishni yaratish kerak.

Buni qanday qilishim mumkin?

Dastur quyidagi "Tsezarning shifrlari" dir:

    public static String cipher(String input2, Integer number) {
    String expression = input2;
    int k = number;
    int lowerBoundlc = 97;
    int upperBoundlc = 122;
    int lowerBounduc = 65;
    int upperBounduc = 90;
    for (int i = 0; i < expression.length(); i++) {
        char c = expression.charAt(i);
        int j = (int) c;
        if ((lowerBoundlc <= j && j <= upperBoundlc) || (j == 45)) {
            if (j == 45) {
                System.out.print(Character.toString((char) j));
            } else {
                int addk = j + k;
                if (addk > upperBoundlc) {
                    int lowercase = addk % upperBoundlc;
                    int resultlc = lowerBoundlc + lowercase -1;
                    System.out.print(Character.toString((char) resultlc));
                } else {
                    System.out.print(Character.toString((char) addk));
                }
            }

        } else {
            if ((lowerBounduc <= j && j <= upperBounduc) || (j == 45)) {
                int addnewk = j + k;
                if (addnewk > upperBounduc) {
                    int uppercase = addnewk % upperBounduc;
                    int resultuc = lowerBounduc + uppercase -1;
                    System.out.print(Character.toString((char) resultuc));
                } else {
                    System.out.print(Character.toString((char) addnewk));
                }

            }

        }

    }
    return null;    
}

Rahmat.

0
Belgilarni birlashtirish uchun StringBuilder dan foydalaning.
qo'shib qo'ydi muallif Elliott Frisch, manba
@ElliottFrisch: taklifingiz uchun rahmat. Men buni qildim!
qo'shib qo'ydi muallif Clash, manba

6 javoblar

Character.toString ((char) variable ) qatlamini o'rniga, bu qatorlarni birgalikda + = yoki muqobil ravishda StringBuilder sinfiga qo'shishingiz mumkin.

Bunga misol bo'la oladi

String s = Character.toString((char) j);
System.out.print(s);

String p = Character.toString((char) resultC);
s += p;
System.out.print(p);

Barcha satrlarni birlashtirib bo'lgach, siz oxirigacha stringni qaytarishingiz mumkin.

0
qo'shib qo'ydi

Biz xabarlarni boshqa manbalarga chiqarish uchun System.out.println() dan foydalanishimiz mumkin. Buni amalga oshirish uchun standart chiqishni System.setOut (PrintStream p) yordamida qayta tayinlashimiz kerak; Tizim klassi usuli

Sizning ehtiyojingiz asosida System.setOut() ni o'rnatish;

ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos);

PrintStream old = System.out;

// use your special stream
System.setOut(ps);

// All System.out.Println will goes to special stream
System.out.println("This will be written to the string!");

System.out.flush();
System.setOut(old);

String content = new String(baos.toByteArray(), StandardCharsets.UTF_8);
System.out.println("Here: " + content); 
0
qo'shib qo'ydi

You can achieve your goal by carrying out the following steps:
1. Create an empty StringBuilder object at the beginning of the cipher method (this object will hold the sequence of characters that forms the final String to be returned by that method).
2. Replace each code line of the form (where x represents an int variable)

System.out.print(Character.toString((char) x));

(a) s yuqoridagi 1-bandda eslatilgan StringBuilder ob'ektini ifodalaydi va (b) ushbu chiziq x ning s ning sxemasi uchun char ko'rinishini qo'shadi:

s.append((char) x);

3. StringBuilder ketma-ketligi tugallangandan so'ng, ushbu uslubning String tavsifini quyidagi usul bilan oling (bu erda StringBuilder ):

s.toString();

Quyida yuqorida keltirilgan usuldan foydalanib, shifrlangan usulning o'zgartirilgan versiyasi mavjud:

public static String cipher(String input2, Integer number) {
        StringBuilder builder = new StringBuilder(input2.length());
        int k = number;
        int lowerBoundlc = 97;
        int upperBoundlc = 122;
        int lowerBounduc = 65;
        int upperBounduc = 90;
        for (int i = 0; i < input2.length(); i++) {
            char c = input2.charAt(i);
            int j = (int) c;
            if ((lowerBoundlc <= j && j <= upperBoundlc) || (j == 45)) {
                if (j == 45) {
                    builder.append((char) j);
                } else {
                    int addk = j + k;
                    if (addk > upperBoundlc) {
                        int lowercase = addk % upperBoundlc;
                        int resultlc = lowerBoundlc + lowercase -1;
                        builder.append((char) resultlc);
                    } else {
                        builder.append((char) addk);
                    }
                }

            } else {
                if ((lowerBounduc <= j && j <= upperBounduc) || (j == 45)) {
                    int addnewk = j + k;
                    if (addnewk > upperBounduc) {
                        int uppercase = addnewk % upperBounduc;
                        int resultuc = lowerBounduc + uppercase -1;
                        builder.append((char) resultuc);
                    } else {
                        builder.append((char) addnewk);
                    }
                }
            }
        }
        return builder.toString();    
    }
0
qo'shib qo'ydi

Bu muammoni hal qilish uchun bir nechta echim bor. Ammo bu muammoni hal qilish uchun ushbu echimdan foydalangan holda kodingizdagi eng kam o'zgarishlarni hal qilishingiz mumkin ...

Birinchidan, siz standart chiqdi oqimini qayta yo'naltirishingiz kerak, keyin mantiqni bajarishingiz kerak, keyin chiqqani ko'rsatuv chiqish oqimida chop etishingiz kerak. Asosiy usulingizni shu bilan yangilang.

public static void main(String arg[]){
    OutputStream os=System.out;
    //Create piped streams
    PipedInputStream pis=new PipedInputStream(2000);
    PipedOutputStream pos=new PipedOutputStream(pis);
    //Change default output stream
    System.setOut(pos);
    //Logic..
    cipher("stack",4);
    //Change default output stream to console
    System.setOut(os);
    //Read output from PipedOutput Stream & print on console
    Scanner sc=new Scanner(System.in);
    String ct=sc.next();
    System.out.print(ct);
}

Endi standart shriftda standart chiqish oqimida chop etsangiz, to'g'ridan-to'g'ri konsolda chop etilmaydi.

cipher usulida hech qanday o'zgarishsiz dasturingizdagi chiqishni ham yozishingiz mumkin.

0
qo'shib qo'ydi

you can simply append answers to the string using concatenation ('+' operator) & use that string. you can use String Builder to improve the performance.

0
qo'shib qo'ydi
public static String cipher(String input2, Integer number) {

    String expression = input2;
    int k = number;
    int lowerBoundlc = 97;
    int upperBoundlc = 122;
    int lowerBounduc = 65;
    int upperBounduc = 90;

    StringBuilder result = new StringBuilder();

    for (int i = 0; i < expression.length(); i++) {
        char c = expression.charAt(i);
        int j = (int) c;

        if ((lowerBoundlc <= j && j <= upperBoundlc) || (j == 45)) {
            if (j == 45) {
                StringBuilder line = new StringBuilder(Character.toString((char) j));
                result.append(line);
            } else {
                int addk = j + k;
                if (addk > upperBoundlc) {
                    int lowercase = addk % upperBoundlc;
                    int resultlc = lowerBoundlc + lowercase - 1;
                    StringBuilder appendresultlc = new StringBuilder(Character.toString((char) resultlc));
                    result.append(appendresultlc);
                } else {
                    StringBuilder appendaddk = new StringBuilder(Character.toString((char) addk));
                    result.append(appendaddk);
                }
            }

        } else {
            if ((lowerBounduc <= j && j <= upperBounduc) || (j == 45)) {
                int addnewk = j + k;
                if (addnewk > upperBounduc) {
                    int uppercase = addnewk % upperBounduc;
                    int resultuc = lowerBounduc + uppercase - 1;
                    StringBuilder appendresultuc = new StringBuilder(Character.toString((char) resultuc));
                    result.append(appendresultuc);
                } else {
                    StringBuilder appendaddnewk = new StringBuilder(Character.toString((char) addnewk));
                    result.append(appendaddnewk);

                }

            }

        }

    }
    return result.toString();
}
0
qo'shib qo'ydi