从 for 循环中获取结果并将其添加到每次迭代的另一个变量中
Get result from for loop and add it to another variable for every iteration
我有一个作业,我应该编写一个程序,使用用户输入的 "encryption key" 来加密和解密用户输入的单词。我想要做的是检查输入键中每个字母的值,与字母表(例如:a = 1,b = 2,c = 3)进行比较,然后添加字母的值,然后用于移动他们分配的单词中的字符。
这是我目前的作业代码:
/* Validity Check from
*
*
*
*
*/
import java.util.*;
public class SecretDecoderFinal
{
public static void main (String [] args)
{
optInput();
}
public static int optInput()
{
int opt = 0;
do {
String word = "";
String key = "";
System.out.println("Welcome to Seegson Security secure transmission terminal");
System.out.println("");
System.out.println("Enter an option for the terminal");
System.out.println("(1) to encrypt a word");
System.out.println("(2) to decrypt a word");
System.out.println("(0) to exit the terminal");
opt = In.getInt();
System.out.println("You selected: Option " +opt);
System.out.println("");
switch(opt)
{
case 1:
encryptWord(word, key);
break;
case 2:
decryptWord(word, key);
break;
case 0:
System.out.println("Thank you for using the encryption/decryption program");
break;
default:
System.err.println("Invalid option. Select 1 for encryption, 2 for decryption, or 0 to exit");
System.out.println("");
break;
}
}while (!isExit(opt));
return opt;
}
public static String keyInput(String key)
{
do
{
System.out.println("Enter the key you want for encryption/decryption");
key = In.getString();
System.out.println("Your key is: " +key);
System.out.println("");
}while (!isValid(key));
//printEncrypted(key);
return key;
}
public static String wordInput(String word)
{
do
{
System.out.println("Enter the word you want decrypted/encrypted");
word = In.getString();
System.out.println("You entered: " +word);
System.out.println("");
} while (!isValid(word));
//printEncrypted(word);
return word;
}
public static void encryptWord(String word, String key)
{
// String alphabet1 = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";
wordInput(word);
keyInput(key);
System.out.println("The word from the encryptWord metod " +word);
printEncrypted(word, key);
}
public static void decryptWord(String w, String k)
{
String alphabet1 = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";
wordInput(w);
keyInput(k);
}
这部分代码来自我的朋友
public static String printEncrypted(String word, String key)
{
System.out.println("This is the word from the printEncrypted method: " +word);
int shift = 0;
//Uses the key length to determine how much the alphabet will shift
for (int x = 0; x < key.length(); x++)
shift += key.charAt(x) - 96;
shift = shift % 26;
//Creates an array to perform the shift
char [] y = word.toCharArray();
for (int x = 0; x < y.length; x++)
//Uses the shift to shift the alphabet to decrypt the word.
for (int d = 0; d < shift; d++)
if (y[x] == 'z')
y[x] = 'a';
else {
y[x]--;
}
String newWord = new String(y);
System.out.println("Encrypted is " +newWord);
return newWord;
}
public static boolean isValid (String s)
{
String strSpecialChars="!@#$%&*()_+=-|<>?{}[]~";
//String alphabet2 = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";
boolean upCase = false;
boolean isDigit = false;
boolean spChar = false;
if (s.matches(".+[A-Z].+")){
upCase = true;
}
if (s.matches(".+[1-9].+")){
isDigit = true;
}
if (strSpecialChars.contains(s)){
spChar = true;
}
if (upCase|| isDigit || spChar)
{
System.err.println("The string cannot contain capital letters, special characters or numbers. Try again.");
System.out.println("");
return false;
}
else
{
return true;
}
}
public static boolean isExit (int option)
{
if (option == 0)
{
return true;
}
else
{
return false;
}
}
}
这就是我要为我的性格转变做的事情:
public class LetterTester
{
public static void main (String []args)
{
String input="craig";
final String alphabet="abcdefghijklmnopqrstvwxyz";
int finalValue = 0;
int[] numbers;
for(int i=0;i<input.length();i++){
finalValue=(alphabet.indexOf(input.charAt(i))+1);
System.out.print(finalValue);
}
}
}
但是,我不知道如何创建变量并让我的 for 循环在每次运行时将其输出添加到变量中。
LetterTester 仅用于测试。在我的实际任务中,输入将从另一个方法中获取,然后进行测试。因此,例如,如果键是"abc",它将确定其每个字母的值,因此a = 1,b = 2,c = 3。然后我希望将它们加在一起成为a我可以使用的变量。
因此,当输入的计算完成时,变量应等于 6。
此外,不确定我是否应该为此提出第二个问题,但在我的作业代码中,我无法通过各自的方法(keyInput 和 wordInput)传递我的单词和键输入的值) 到一个名为 encryptWord 的方法,当我尝试从 encryptWord 方法测试它时,它显示该词为空白。
如果有人想知道我为输入做了什么,我使用的是 In class,这是我从这里得到的:http://www.ecf.utoronto.ca/~jcarter/
到目前为止,我的老师通过教我们使用 In class 从一开始就教 class。
OP 的问题太多了,无法完全回答。以下代码是解决方案的指南 space.
- 将单词和键的输入与其他任何操作分开收集。
getWord()
和 getKey
方法需要使用 OP 指出的 In
class 。这里的重点是这些方法不需要任何参数,有一个 return,并且只收集信息(因此将输入与处理分开)。
encryptWord(String, String)
方法采用收集的输入并以某种方式对其进行转换,return 进行转换。它 不应该 做任何输出(同样,将 I/O 与处理分开)。我没有尝试复制加密算法。
decryptWord(String, String)
方法还采用收集的输入并按照算法对其进行转换。这里没有尝试实现任何东西,但本质上它是加密的逆过程。
- 并不是所有的东西都应该是静态的,但它遵循了 OP 的方法。
isValid(String)
似乎仅限于小写字母。显然,如果该假设不正确,可以对其进行调整。
//
// is valid checks to see if only lower case characters
//
private static boolean isValid(final String chkWord)
{
// returns true only if the match is lower case a-z
return chkWord.matches("^[a-z]+$");
}
private static String encryptWord(String word, String key)
{
// TODO: implement the encryption algorithm;
// The algorithm would use the key to do the encryption;
// here will just add to character as quick example
char[] wordChars = word.toCharArray();
for (int i = 0; i < wordChars.length; ++i) {
char c = wordChars[i];
if (c >= 'a' && c <= 'm') { c += 13; }
else if (c >= 'n' && c <= 'z') { c -= 13; }
wordChars[i] = c;
}
return new String(wordChars);
}
private static String decryptWord(String word, String key)
{
// TODO: implement the decryption algorithm
return "NEED TO IMPLEMENT";
}
private static String getWord()
{
// the word should be gathered from the user in some fashion
// using the In class that is provided
return "helloworld";
}
private static String getKey()
{
// the key should be gathered from the user in some fashion
// using the In class that is provided
return "doit";
}
public static void main(String[] args)
{
final String word = getWord();
final String key = getKey();
boolean validWord = isValid(word);
System.out.printf("Is valid [%s]: %b%n", word, validWord);
if (! validWord) {
System.err.println("Not a good word!");
return;
}
String encrypted = encryptWord(word, key);
System.out.printf("Encrypted %s: %s%n", word, encrypted);
String decrypted = decryptWord(word, key);
System.out.printf("Encrypted %s: %s%n", word, decrypted);
}
isValid()
的快速测试:
Is valid [Hello]: false
Is valid [hello]: true
Is valid [specialchar*]: false
我有一个作业,我应该编写一个程序,使用用户输入的 "encryption key" 来加密和解密用户输入的单词。我想要做的是检查输入键中每个字母的值,与字母表(例如:a = 1,b = 2,c = 3)进行比较,然后添加字母的值,然后用于移动他们分配的单词中的字符。 这是我目前的作业代码:
/* Validity Check from
*
*
*
*
*/
import java.util.*;
public class SecretDecoderFinal
{
public static void main (String [] args)
{
optInput();
}
public static int optInput()
{
int opt = 0;
do {
String word = "";
String key = "";
System.out.println("Welcome to Seegson Security secure transmission terminal");
System.out.println("");
System.out.println("Enter an option for the terminal");
System.out.println("(1) to encrypt a word");
System.out.println("(2) to decrypt a word");
System.out.println("(0) to exit the terminal");
opt = In.getInt();
System.out.println("You selected: Option " +opt);
System.out.println("");
switch(opt)
{
case 1:
encryptWord(word, key);
break;
case 2:
decryptWord(word, key);
break;
case 0:
System.out.println("Thank you for using the encryption/decryption program");
break;
default:
System.err.println("Invalid option. Select 1 for encryption, 2 for decryption, or 0 to exit");
System.out.println("");
break;
}
}while (!isExit(opt));
return opt;
}
public static String keyInput(String key)
{
do
{
System.out.println("Enter the key you want for encryption/decryption");
key = In.getString();
System.out.println("Your key is: " +key);
System.out.println("");
}while (!isValid(key));
//printEncrypted(key);
return key;
}
public static String wordInput(String word)
{
do
{
System.out.println("Enter the word you want decrypted/encrypted");
word = In.getString();
System.out.println("You entered: " +word);
System.out.println("");
} while (!isValid(word));
//printEncrypted(word);
return word;
}
public static void encryptWord(String word, String key)
{
// String alphabet1 = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";
wordInput(word);
keyInput(key);
System.out.println("The word from the encryptWord metod " +word);
printEncrypted(word, key);
}
public static void decryptWord(String w, String k)
{
String alphabet1 = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";
wordInput(w);
keyInput(k);
}
这部分代码来自我的朋友
public static String printEncrypted(String word, String key)
{
System.out.println("This is the word from the printEncrypted method: " +word);
int shift = 0;
//Uses the key length to determine how much the alphabet will shift
for (int x = 0; x < key.length(); x++)
shift += key.charAt(x) - 96;
shift = shift % 26;
//Creates an array to perform the shift
char [] y = word.toCharArray();
for (int x = 0; x < y.length; x++)
//Uses the shift to shift the alphabet to decrypt the word.
for (int d = 0; d < shift; d++)
if (y[x] == 'z')
y[x] = 'a';
else {
y[x]--;
}
String newWord = new String(y);
System.out.println("Encrypted is " +newWord);
return newWord;
}
public static boolean isValid (String s)
{
String strSpecialChars="!@#$%&*()_+=-|<>?{}[]~";
//String alphabet2 = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";
boolean upCase = false;
boolean isDigit = false;
boolean spChar = false;
if (s.matches(".+[A-Z].+")){
upCase = true;
}
if (s.matches(".+[1-9].+")){
isDigit = true;
}
if (strSpecialChars.contains(s)){
spChar = true;
}
if (upCase|| isDigit || spChar)
{
System.err.println("The string cannot contain capital letters, special characters or numbers. Try again.");
System.out.println("");
return false;
}
else
{
return true;
}
}
public static boolean isExit (int option)
{
if (option == 0)
{
return true;
}
else
{
return false;
}
}
}
这就是我要为我的性格转变做的事情:
public class LetterTester
{
public static void main (String []args)
{
String input="craig";
final String alphabet="abcdefghijklmnopqrstvwxyz";
int finalValue = 0;
int[] numbers;
for(int i=0;i<input.length();i++){
finalValue=(alphabet.indexOf(input.charAt(i))+1);
System.out.print(finalValue);
}
}
}
但是,我不知道如何创建变量并让我的 for 循环在每次运行时将其输出添加到变量中。 LetterTester 仅用于测试。在我的实际任务中,输入将从另一个方法中获取,然后进行测试。因此,例如,如果键是"abc",它将确定其每个字母的值,因此a = 1,b = 2,c = 3。然后我希望将它们加在一起成为a我可以使用的变量。 因此,当输入的计算完成时,变量应等于 6。
此外,不确定我是否应该为此提出第二个问题,但在我的作业代码中,我无法通过各自的方法(keyInput 和 wordInput)传递我的单词和键输入的值) 到一个名为 encryptWord 的方法,当我尝试从 encryptWord 方法测试它时,它显示该词为空白。
如果有人想知道我为输入做了什么,我使用的是 In class,这是我从这里得到的:http://www.ecf.utoronto.ca/~jcarter/
到目前为止,我的老师通过教我们使用 In class 从一开始就教 class。
OP 的问题太多了,无法完全回答。以下代码是解决方案的指南 space.
- 将单词和键的输入与其他任何操作分开收集。
getWord()
和getKey
方法需要使用 OP 指出的In
class 。这里的重点是这些方法不需要任何参数,有一个 return,并且只收集信息(因此将输入与处理分开)。 encryptWord(String, String)
方法采用收集的输入并以某种方式对其进行转换,return 进行转换。它 不应该 做任何输出(同样,将 I/O 与处理分开)。我没有尝试复制加密算法。decryptWord(String, String)
方法还采用收集的输入并按照算法对其进行转换。这里没有尝试实现任何东西,但本质上它是加密的逆过程。- 并不是所有的东西都应该是静态的,但它遵循了 OP 的方法。
isValid(String)
似乎仅限于小写字母。显然,如果该假设不正确,可以对其进行调整。// // is valid checks to see if only lower case characters // private static boolean isValid(final String chkWord) { // returns true only if the match is lower case a-z return chkWord.matches("^[a-z]+$"); } private static String encryptWord(String word, String key) { // TODO: implement the encryption algorithm; // The algorithm would use the key to do the encryption; // here will just add to character as quick example char[] wordChars = word.toCharArray(); for (int i = 0; i < wordChars.length; ++i) { char c = wordChars[i]; if (c >= 'a' && c <= 'm') { c += 13; } else if (c >= 'n' && c <= 'z') { c -= 13; } wordChars[i] = c; } return new String(wordChars); } private static String decryptWord(String word, String key) { // TODO: implement the decryption algorithm return "NEED TO IMPLEMENT"; } private static String getWord() { // the word should be gathered from the user in some fashion // using the In class that is provided return "helloworld"; } private static String getKey() { // the key should be gathered from the user in some fashion // using the In class that is provided return "doit"; } public static void main(String[] args) { final String word = getWord(); final String key = getKey(); boolean validWord = isValid(word); System.out.printf("Is valid [%s]: %b%n", word, validWord); if (! validWord) { System.err.println("Not a good word!"); return; } String encrypted = encryptWord(word, key); System.out.printf("Encrypted %s: %s%n", word, encrypted); String decrypted = decryptWord(word, key); System.out.printf("Encrypted %s: %s%n", word, decrypted); }
isValid()
的快速测试:
Is valid [Hello]: false
Is valid [hello]: true
Is valid [specialchar*]: false