我为凯撒密码创建了一个代码,其移位为25个字符。我的代码不是对字母的前半部分进行编码,而是对后半部分进行编码。我不知道为什么请帮忙。
例如,如果我尝试编码“ abcdefghijklmnopqrstuvwxyz” ,则显示为“ abcdefghijklmmlkjihgfedcba” 。如您所见,仅字母的后半部分被编码。我应该怎么做才能使其工作并编码下半部分?
import java.util.Scanner;
public class Project1 {
public static void main(String[] args) {
//creating alphabet and coded alphabet arrays
char alphabet[] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',' '};
char codedAlphabet[] = {'z','y','x','w','v','u','t','s','r','q','p','o','n','m','l','k','j','i','h','g','f','e','d','c','b','a',' '};
//prompting user for encoding or decoding, and for the message
Scanner scan = new Scanner(System.in);
System.out.println("Enter the message: ");
String s = scan.nextLine();
s.toLowerCase();
StringBuffer message = new StringBuffer( s );
Scanner input = new Scanner(System.in);
System.out.println("Enter 1 to encode message, 2 to decode message: ");
int choice = input.nextInt();
if(choice == 1){
encode(message, alphabet, codedAlphabet);
System.out.println(message);
} else if(choice == 2) {
}
}
//method for encoding
public static void encode(StringBuffer message, char a[], char b[]){
for(int i = 0; i<message.length(); i++){
for(int j = 0; j<a.length; j++){
if( message.charAt(i) == a[j]){
message.setCharAt(i, b[j]);
}
}
}
}
}
答案 0 :(得分:0)
您的寻找正确字符的检查是正确的。但是,当您替换字符时,必须中断该内部循环。否则,如果适合所选的加密配对,则将多次替换同一字符位置。在这种情况下,您可以将第一个字符object
替换为using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace MCVE
{
class GroupBox : System.Windows.Forms.GroupBox
{
const int WM_ERASEBKGND = 0x14;
const int WM_PRINTCLIENT = 0x318;
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case WM_ERASEBKGND:
base.WndProc(ref m);
using (var g = Graphics.FromHdc(m.WParam))//CASE 1
//using (var e = new PaintEventArgs(Graphics.FromHdc(m.WParam), ClientRectangle))//CASE 2
{
var e = new PaintEventArgs(g, ClientRectangle);//CASE 1
var r = new Rectangle(2, 12, Width - 4, Height - 2);
using (var b = new LinearGradientBrush(r, BackColor, SystemColors.Window, LinearGradientMode.Vertical))
{
e.Graphics.FillRectangle(b, r);//Draw the gradient.
}
}
m.Result = new IntPtr(1);//Signal that no further drawing of the background is necessary by WM_PAINT.
return;
case WM_PRINTCLIENT:
DefWndProc(ref m);//Bypass GroupBox's internal handling so that actual painting is handled by Windows.
return;
}
base.WndProc(ref m);//Default processing of the rest of the messages.
}
};
class Label : System.Windows.Forms.Label
{
const int WM_ERASEBKGND = 0x14;
const int WM_PAINT = 0xF;
[DllImport("user32.dll")] static extern IntPtr BeginPaint(IntPtr hWnd, out PAINTSTRUCT lpPaint);
[DllImport("user32.dll")] static extern IntPtr EndPaint(IntPtr hWnd, ref PAINTSTRUCT lpPaint);
//Ask Windows to send a message to the parent to draw it's background in the current device context.
[DllImport("uxtheme.dll")] extern static int DrawThemeParentBackground(IntPtr hWnd, IntPtr hdc, ref Rectangle pRect);
[StructLayout(LayoutKind.Sequential)]
struct PAINTSTRUCT
{
public IntPtr hdc;
public bool fErase;
public Rectangle rcPaint;
public bool fRestore;
public bool fIncUpdate;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)] public byte[] rgcReserved;
};
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case WM_ERASEBKGND:
var r = ClientRectangle;
DrawThemeParentBackground(Handle, m.WParam, ref r);
m.Result = new IntPtr(1);//Signal that no further drawing of the background is necessary by WM_PAINT.
return;
case WM_PAINT:
PAINTSTRUCT ps;
var hdc = BeginPaint(Handle, out ps);
EndPaint(Handle, ref ps);//Don't paint any text so that the gradient remains visible.
m.Result = IntPtr.Zero;
return;
}
base.WndProc(ref m);//Default processing of the rest of the messages.
}
};
static class Program
{
[STAThread] static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var form = new Form() { BackColor = SystemColors.Highlight };
var groupbox = new GroupBox() { Anchor = (AnchorStyles)15, FlatStyle = FlatStyle.System, Location = new Point(10, 10), Text = "groupBox1" };
form.Controls.Add(groupbox);
groupbox.Controls.Add(new Label() { FlatStyle = FlatStyle.System, Location = new Point(50, 50) });
Application.Run(form);
}
};
}
,这是正确的,但是然后再次替换此新字符gcloud compute project-info describe
(返回a
),这是错误的。
z
答案 1 :(得分:-1)
根据您的问题代码,我认为这将是答案。
public static void encode(StringBuffer message, char a[], char b[]){
for(int i = 0; i<message.length(); i++){
for(int j = 0; j<a.length; j++){
if( message.charAt(i) == a[j]){
message.setCharAt(i, b[j]);
break;
}
}
}
}
public static void decode(StringBuffer message, char a[], char b[]){
for(int i = 0; i<message.length(); i++){
for(int j = 0; j<a.length; j++){
if( message.charAt(i) == b[j]){
message.setCharAt(i, a[j]);
break;
}
}
}
}