/**
* Write a short description of what the class does (one line)
* @author (your name)
* @version (a version number or a date)
*/
import java.util.*;
public class CaesarShift{//class header
//every open bracket need a closed bracket
//main method below is ignition of a program (not every class has one)
public static void main(String[] args) {
String alphabet = "abcdefghijklmnopqrstuvwxyz";
String letterOfWord = "";
Scanner sc = new Scanner(System.in);
boolean keepCoding = true;
while(keepCoding){
boolean keepPlugging = true;
int counter = 0;
System.out.println("Print in word: ");
String word = sc.next();
int lengthWord = word.length();
System.out.println("How much shifted? ");
int shift = sc.nextInt();
while (keepPlugging){
letterOfWord = word.substring(counter, counter + 1);
int letter = alphabet.indexOf(letterOfWord);
letter = letter + shift;
letter = letter % 26;
letterOfWord = alphabet.substring(letter, letter + 1);
System.out.print(letterOfWord);
counter++;
if(counter > lengthWord - 1){
keepPlugging = false;
}
}
System.out.println("");
System.out.println("Code another word?");
String codeAnother = sc.next();
if(codeAnother.equals("no")){
keepCoding = false;
}
}
}
}
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.