扫描程序更改字符串不起作用 - Java

时间:2016-08-12 10:25:34

标签: java if-statement

无论我输入" Y"或" N"进入控制台它总是跳进else语句,扫描仪设置错误或者这个脚本有什么问题?

import java.util.*;
import java.lang.*;
import java.io.*;

class StartingClass {
public static void main(String arg[] ){

    System.out.println("The Best Encryption Bot 2016"); 

    StringBuffer passWord = new StringBuffer(); 
    Scanner input = new Scanner(System.in); 
    System.out.println("Please enter the phrase you want to encrypt: ");
    passWord.append(input.nextLine());
    System.out.println("Original: " + passWord);

    for(int i = 0; i < passWord.length(); i++) {
        int temp = 0; 
       temp = (int)passWord.charAt(i); 
       temp = (int) (temp * 4); 
       passWord.setCharAt(i, (char)temp);
    }   
    System.out.println("Encrypted: " + passWord); 

    System.out.println("Do you want to unencrypt a phrase ? Y/N");
    Scanner Encrypt = new Scanner(System.in); 
    String unencrypt = Encrypt.nextLine();


    if (unencrypt == "Y" ) {
         StringBuffer deCrypt = new StringBuffer(); 
            Scanner deinput = new Scanner(System.in); 
            System.out.println("Please enter the phrase you want to Decrypt: ");
            deCrypt.append(deinput.nextLine());
            System.out.println("Original: " + deCrypt);

            for(int i = 0; i < passWord.length(); i++) {
                int temp = 0; 
               temp = (int)deCrypt.charAt(i); 
               temp = (int) (temp / 4); 
               deCrypt.setCharAt(i, (char)temp);
               }
            System.out.println("UnEncrypted: " + passWord); 
            } else { 
                System.out.println("Ok"); 
            }

    }

}

问题出在if语句中,其中unncrypt ==&#34; Y&#34;

2 个答案:

答案 0 :(得分:0)

原因是因为那不是你比较字符串的方式:

用它来比较:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

'This line stops the worksheet updating on every change, it only updates
'when cell G1 is touched

If Not Intersect(Target, Range("G1")) Is Nothing Then Exit Sub

'Set the Variables to be used
Dim pt As PivotTable
Dim Field As PivotField
Dim NewCat As String

'Here you amend to suit your data
Set pt = Worksheets("Executive Summary").PivotTables("ServiceCostAnalysis")
Set Field = pt.PivotFields("Cost Centre")
NewCat = Worksheets("Executive Summary").Range("G1").Value

'This updates and refreshes the PIVOT table
With pt
    Field.ClearAllFilters
    Field.CurrentPage = NewCat
    pt.RefreshTable
End With

End Sub

答案 1 :(得分:0)

尝试使用unencrypt.equals("Y")代替==