0
package supreme;

import java.util.Arrays;
import java.util.Scanner;
public class Main {
    static   byte[] tempCrypt;
    static  short [] crypting;
    static  char [] password;
    public static void main(String args[]) {
        menu();
    }
    //[S@7225790e
    public static  void menu() {
        Scanner in = new Scanner(System.in);
        int choice;

        System.out.print("1.Crypt\n2.Uncrypt\n3.Exit\nPlease make your choice: ");
        switch(choice = in.nextInt()) {
            case 1:
            {
                System.out.println("Please enter your string which will be crypted: ");
                String line = in.next();
                tempCrypt = line.getBytes();
                crypting = new short[tempCrypt.length];
                for (int i = 0; i < tempCrypt.length; i++ ) {
                    crypting[i] = tempCrypt[i];
                }
                System.out.println("Please enter your password(it can be any number): ");
                int tempPassw = in.nextInt();
                String temp = Integer.toBinaryString(tempPassw);
                System.out.print("Password is: ");
                System.out.println(temp);
                password = temp.toCharArray();
                for (int i =  0; i < crypting.length; i++) {
                    System.out.println(crypting[i]);
                }

//                ____CRYPTING
                int counter = 0;
                for (int i = 0; i < crypting.length; i++) {
//                    System.out.println("Length is:" + crypting.length);
//                    System.out.println("Counter is:" + counter);
                    if(counter == crypting.length || counter > crypting.length) counter = 0;

                    if((password[counter] == '1') && (password[counter+1] == '0')) {
                        crypting[i] = (short)(crypting[i] << 1);
//                        System.out.println(crypting[i]);
//                        System.out.println("<<");
                    }
                    if(password[counter] == '0' && password[counter+1] == '1') {
                        crypting[i] = (short)(crypting[i] >> 1);
//                        System.out.println(crypting[i]);
//                        System.out.println(">>");
                    }
                    if(password[counter] == '1' && password[counter+1] == '1') {
                        crypting[i] = (short)(~crypting[i]);
//                        System.out.println(crypting[i]);
//                        System.out.println("~");
                    }
                    counter+=2;
                }
                System.out.println("After crypt:");
//                for( int i = 0; i <crypting.length; i ++ ) {
//                    System.out.println("Element " + i +" is: " +  crypting[i]);
//                }
                String temparory = new String();
                for (int i = 0; i < crypting.length; i++ ) {
                    temparory  +=  Short.toString(crypting[i]);
                }
                System.out.println(temparory.getBytes());
                String news = new String(temparory);
                System.out.println(news);
                System.out.println(temparory);

                _________________________________________________________
                !!!Вот этот кусок почему то игнорируется программой!!!!!!
                System.out.println("Exit program?(y/n)");
                String answer = in.next();
                if(answer=="yes") {
                    break;
                } else {
                    menu();
                }


            }
            case 2:
            {
                break;
            }
            case 3: {
                break;
            }
            default:
                System.out.println("Please, choose one of the possible variants:");
                menu();
        }
    }

}
5
  • Попробуйте answer.equals("yes")
    – danilshik
    15 окт 2018 в 5:37
  • Может, потому, что в вопросе (y/n), а не (yes/no)? Что вы вводите?
    – gvozd1989
    15 окт 2018 в 5:51
  • Спасибо,помогло. 15 окт 2018 в 5:57
  • Дело в том, что сравнение вида "==" для примитивов (int, byte, boolean и прочие не-классовые) сравнивает их по содержимому, то есть то, чего мы интуитивно хотим. String - это класс, объект, но не примитив. "==" для объектов сравнивает, ссылаются ли две ссылки на один объект. Когда вы хотите сравнить два объекта на содержимое, используется метод .equals(), наследуемый от Object. Теперь на вашем примере подробней: вы создали ссылку answer типа String, записали в нее ввод с консоли. В условии вы также создали строку "yes" без ссылки. == сравнило ссылки и дало ответ: они разные. Притом всегда. 15 окт 2018 в 9:18
  • Рекомендую прочитать Oracle Certified Associate 7 (OCA 7), там в главе, кажется, 5 (глава о работе со строками) этот вопрос очень доступно и качественно раскрывается. 15 окт 2018 в 9:27

0

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Посмотрите другие вопросы с метками или задайте свой вопрос.